Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] Round Tracker

December 22 (11 years ago)

Edited January 07 (11 years ago)
A simple script that looks for a token on the turn order tracker with its initiative value set to Round X. It then increments the number at the end by one. This can easily be combined with my initiative tracker script as well.

Update 2013.12.29 (Dec 29th): Added in Steven C's fix to prevent the script from breaking when adding tokens via &{tracker}
Update 2013.12.31 (Dec 31st): Added AnnounceNewRound true/false flag to turn on/off new round announcements in chat
Update 2014.01.07 (Jan 7th): Added a check after the turn order manipulation to exit if a non-token / custom item is used instead of a token.

Video Link: https://plus.google.com/109659591453191902524/posts/Faxd1eJKuor

on("change:campaign:turnorder", function(obj, prev) {
    // AnnounceNewRound - Set to TRUE if you want the script to announce
    // the beginning of each new round.
    var AnnounceNewRound = false;
    
    if (!Campaign().get("turnorder")) return;
    var turn_order = JSON.parse(Campaign().get("turnorder"));
    if (!turn_order.length) return;
    if (typeof turn_order[0].pr == "string") {
        if (turn_order[0].pr.substring(0, 5) == "Round") {
            var RoundTracker = turn_order[0].pr;
            var CurrentRound = parseInt(RoundTracker.substring(5));
            turn_order[0].pr = "Round " + (CurrentRound + 1);
            Campaign().set({turnorder: JSON.stringify(turn_order)});
            if(AnnounceNewRound == true) {
                sendChat("", "/desc ---- " + turn_order[0].pr + " ----");
            }
        }
    }
    
    // Exit script if custom item on turn order tracker instead of a token...
    if (turn_order[0].id == -1) return;
});

December 22 (11 years ago)

Edited December 31 (11 years ago)
on("change:campaign:turnorder", function(obj, prev) {
    // AnnounceNewRound - Set to TRUE if you want the script to announce
    // the beginning of each new round.
    var AnnounceNewRound = false;
    
    if (!Campaign().get("turnorder")) return;
    var turn_order = JSON.parse(Campaign().get("turnorder"));
    if (!turn_order.length) return;
    if (!turn_order[0].id == -1) return;
    if (typeof turn_order[0].pr == "string") {
        if (turn_order[0].pr.substring(0, 5) == "Round") {
            var RoundTracker = turn_order[0].pr;
            var CurrentRound = parseInt(RoundTracker.substring(5));
            turn_order[0].pr = "Round " + (CurrentRound + 1);
            Campaign().set({turnorder: JSON.stringify(turn_order)});
            if(AnnounceNewRound == true) {
                sendChat("", "/desc ---- " + turn_order[0].pr + " ----");
            }
        }
    }

    var current_token = getObj("graphic", turn_order[0].id);
    var initiative_highlighter = findObjs({name: "InitiativeHighlight", pageid: current_token.get("pageid")}, {caseInsensitive: true})[0];
    
    if (initiative_highlighter == undefined) {
        sendChat("ERROR", "/w gm Cannot find an initiative highlight token on this page.");
        return;
    }
    
    if (initiative_highlighter.get("layer") == "gmlayer" && current_token.get("layer") != "gmlayer") {
        initiative_highlighter.set({
            "top": current_token.get("top"),
            "left": current_token.get("left"),
            "height": current_token.get("height"),
            "width": current_token.get("width")
        });
        setTimeout(function() {
            initiative_highlighter.set({
                "layer": current_token.get("layer")
            });    
        }, 500);
    } else {
        initiative_highlighter.set({
            "layer": current_token.get("layer"),
            "top": current_token.get("top"),
            "left": current_token.get("left"),
            "height": current_token.get("height"),
            "width": current_token.get("width")
        });   
    }
    toFront(current_token);
});
December 22 (11 years ago)
Here's the token image I use for the highlight:
December 27 (11 years ago)

Edited December 27 (11 years ago)
so im havin issues with this script.

if i myself, or the players manually enter their initiatives it works great, but if i create a macro that updates the tracker to the new initiative #, the script breaks

here is the macro i use for the initiative
/roll 1d20+@{DEX Bonus} +@{Improved Initiative} +@{Trait Initiative} &{tracker}

here is the script api error
TypeError: Object 3 has no method 'substring'
at evalmachine.<anonymous>:7:26
at eval (


any way to edit the script to allow this sort of thing to work or is it manual only?
December 27 (11 years ago)
I will take a look at it later and try to break it the same way you have.
December 27 (11 years ago)
It's a rather easy fix. Just check to make sure the pr value of the turnorder object is a string before calling substring on pr.

One additional line before the call to substring fixes it:
on("change:campaign:turnorder", function(obj, prev) {
    if (!Campaign().get("turnorder")) return;
    var turn_order = JSON.parse(Campaign().get("turnorder"));
    if (!turn_order.length) return;
    if (!turn_order[0].id == -1) return;
    if (typeof turn_order[0].pr == "string") {
        if (turn_order[0].pr.substring(0, 5) == "Round") {
            var RoundTracker = turn_order[0].pr;
            var CurrentRound = parseInt(RoundTracker.substring(5));
            turn_order[0].pr = "Round " + (CurrentRound + 1);
            Campaign().set({turnorder: JSON.stringify(turn_order)});
            return;
        }
    }
    
    var current_token = getObj("graphic", turn_order[0].id);
    var initiative_highlighter = findObjs({name: "InitiativeHighlight", pageid: current_token.get("pageid")}, {caseInsensitive: true})[0];
    
    if (initiative_highlighter == undefined) {
        sendChat("ERROR", "/w gm Cannot find an initiative highlight token on this page.");
        return;
    }
    
    if (initiative_highlighter.get("layer") == "gmlayer" && current_token.get("layer") != "gmlayer") {
        initiative_highlighter.set({
            "top": current_token.get("top"),
            "left": current_token.get("left"),
            "height": current_token.get("height"),
            "width": current_token.get("width")
        });
        setTimeout(function() {
            initiative_highlighter.set({
                "layer": current_token.get("layer")
            });    
        }, 500);
    } else {
        initiative_highlighter.set({
            "layer": current_token.get("layer"),
            "top": current_token.get("top"),
            "left": current_token.get("left"),
            "height": current_token.get("height"),
            "width": current_token.get("width")
        });   
    }
    toFront(current_token);
});


Pretty nice script Honeybadger. Will be sure to use it for my campaigns!
December 27 (11 years ago)
Cool. Thanks! :)
December 28 (11 years ago)
Holy crap I can't wait to try this. Thanks in advance!
December 30 (11 years ago)
TypeError: Object 1 has no method 'substring'
at Sandbox.<anonymous> (evalmachine.<anonymous>:7:26)
at eval (

new error when using the code steven posted
December 30 (11 years ago)
I just got back from being out of town for the weekend. I'll take a look at it tomorrow. What exactly are you doing to generate the error? Step by step...
December 30 (11 years ago)
manually clickin on each token to add turn and then manually inputting the #s in works. its when this &{tracker} through a macro adds the initiative #s is what seems to break it
December 30 (11 years ago)
also breaks when you drag an initiative roll to the tracker instead of typing it in yoursel
TypeError: Object 23 has no method 'substring'
at evalmachine.<anonymous>:7:26
at eval (

December 31 (11 years ago)
I don't know what to tell you, but it works for me. I just tested it. I can't get it to break with &{tracker}.
December 31 (11 years ago)
Hi Fulcrum, I suspect the script change was not registered by the sandbox somehow. Did you copy paste the updated script and press save? After this the Output console should state:
Restarting sandbox due to script changes...
Spinning up new sandbox...

If you use HoneyBadger's original script you would recieve your stated errors, but not with my minor tweak. I just tested all your scenarios as well.

Hope you get it to work, otherwise send me a PM and I could help you get it working.

Happy new years!
December 31 (11 years ago)
I updated the original post with the changes... so I dunno.
December 31 (11 years ago)
Oh yes, you sure did - I didn't notice that. Well, in that case I'm stumped. It should work anyway.
January 03 (11 years ago)
I had another API running a trap detector, I disabled this and I havent had issues with the round tracker after that
thanks for the help guys/gals
January 03 (11 years ago)
Cool. :)
January 05 (11 years ago)
I think this thing works pretty well, as long I take out the text it outputs to chat for the error when there's no highlight object, since I have no idea what to name that thing or set it as to get the script to recognize it. But I don't really need it either, just the round tracker.

However, is there a way I can write a macro to put in the tracker object's initiative as "Round 0" automatically? Like, set some token-sheet attribute to that and then just put it in via {&tracker}?
January 05 (11 years ago)
No, there's no way to my knowledge outside of the API, to create a macro that would let you do that.

This thread tells you how to use the Initiative Highlighter: https://app.roll20.net/forum/post/447239/script-highlight-token-at-top-of-initiative
January 07 (11 years ago)

Edited January 10 (11 years ago)
Any chance you could make this work with "Round 0" added as a custom item to initiative? So we don't have to use a token for it.

EDIT: Awesome, thanks!
January 07 (11 years ago)
Hrm, thought it did...
January 07 (11 years ago)
Just want to mention; Player in my group wrote a quick edit to the script so that the Round 0 is added as a value for a new item every time you open the tracker for a new combat. Works quite well once the kinks are ironed out.
January 07 (11 years ago)
Ah, I wouldn't use that myself... I also use the turn order tracker for out of combat exploration. On a side note, I have updated the initial post with an updated version that works with or without a token in the turn order tracker. It will work with a blank custom item now.
January 18 (11 years ago)

Edited January 18 (11 years ago)
Don't know what I'm doing wrong here...I added the script in the API window. When I go into roll 20, highlight tokens (including one that I've named "Round") and press Ctrl-U. When I cycle through the turns, nothing happens.

Is there a command I need to use to kick this off?
January 18 (11 years ago)
You have to enter "Round 0" as the initiative value. So where you would put whatever the players rolled, enter "Round 0" instead, without the quotes.

https://plus.google.com/109659591453191902524/post...

There's a video on that post that shows how it works.
February 26 (11 years ago)

Edited February 26 (11 years ago)
is there a way to get the turn order to reroll after the first round without losing the current round number if so this api would be perfect for my game
February 26 (11 years ago)
Uh... that is certainly doable... but it would require some major revisions and additions to the script. I'll take a look at doing that, but I can't promise when or if it will amount to anything anytime soon. My power cards script and work on my D&D campaign are taking all my free time at the moment.