Macro to increase counter on token

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
HeronMarkedBlade
Kobold
Posts: 7
Joined: Sun Jan 13, 2013 11:41 am

Macro to increase counter on token

Post by HeronMarkedBlade »

Hi! I've been enjoying MapTool for a couple of years now (mostly for the dynamic lighting and map-building capabilities) and have been using macros at a pretty basic level. I have yet to master libraries, json, or the more advanced functions. Right now I'm trying to create a macro that will interact with states to change a number counter on a player token up or down. For example, clicking the macro at the beginning of the round will change the number on the token from '1' to '2' or from '9' to '10', etc. I have token states numbered _0-_9, and then 1_, 2_, 3_, etc., the combination of which '1_' and '_2' would show the number '12' on the token.

I've created a macro that works fine with numbers 0-10, but am having trouble getting it to work right past that point. In addition, what I've come up with so far seems a bit laborious. Is there a more streamlined means of creating such a macro, or a thread I've missed in my search that discusses that? Thanks!

Code: Select all

[if(state.Count09 == 1), CODE:
{
[H: state.Count09 = 0]
[H: state.Count10 = 1]
[H: state.Count00 = 1]
};
{
}]

[if(state.Count08 == 1), CODE:
{
[H: state.Count08 = 0]
[H: state.Count09 = 1]
};
{
}]

[if(state.Count07 == 1), CODE:
{
[H: state.Count07 = 0]
[H: state.Count08 = 1]
};
{
}]

[if(state.Count06 == 1), CODE:
{
[H: state.Count06 = 0]
[H: state.Count07 = 1]
};
{
}]

[if(state.Count05 == 1), CODE:
{
[H: state.Count05 = 0]
[H: state.Count06 = 1]
};
{
}]

[if(state.Count04 == 1), CODE:
{
[H: state.Count04 = 0]
[H: state.Count05 = 1]
};
{
}]

[if(state.Count03 == 1), CODE:
{
[H: state.Count03 = 0]
[H: state.Count04 = 1]
};
{
}]

[if(state.Count02 == 1), CODE:
{
[H: state.Count02 = 0]
[H: state.Count03 = 1]
};
{
}]

[if(state.Count01 == 1), CODE:
{
[H: state.Count01 = 0]
[H: state.Count02 = 1]
};
{
}]

[if(state.Count00 == 1 && state.Count10 == 0), CODE:
{
[H: state.Count00 = 0]
[H: state.Count01 = 1]
};
{
}]

User avatar
wolph42
Deity
Posts: 9779
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Macro to increase counter on token

Post by wolph42 »

should be something like this:

Code: Select all

<!-- initialize -->
[
curr10Num    = 0]
[
curr1Num    = 0]

<!--
 get 10 digit -->
[
count(10), if(getState("Count"+roll.count+"0")): curr10Num = roll.count]

<!--
 increase 1 digit and if need be the 10 digit -->
[
count(10), if(getState("Count0"+roll.count)): curr1Num = roll.count]
[
setState("Count0"+currNum, 0)]
[if(
curr1Num == 9), CODE:{
    [setState("Count"+curr10Num+"0", 0)]
    [setState("Count"+(curr10Num+1)+"0", 1)]
    [state.Count00 = 1]
};{
    [setState("Count0"+(curr1Num+1), 1)]
}]
 


obviously, this will break when you hit 100.

UNTESTED!!

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Macro to increase counter on token

Post by Akodo Makama »

Rule 1 about (modern) programming: Keep information storage separate from information manipulation separate from information display.

By trying to store the information about the current 'count' in the states, which are primarily for display purposes, you're making things very difficult on yourself.

Store the information about the current 'value' in a token property. Then display the proper states when the internal value is changed. This way, you'll always have the correct value and the correct display, and changing the code responsible for one part won't require a change in the other.

Code: Select all

[value = getProperty("STATE_COUNT")]
[
IF (isNumber(value)): ""; value = 0]

[
value = value + 1]
[IF(
value > 99): abort("STATE_COUNT Too Large"); ""]
[
SetProperty("STATE_COUNT",value)]

[FOR(
ones, 0, 9): setState("_" + ones, 0)]
[FOR(
tens, 0, 9): setState(tens + "_", 0)]

[
tens = floor(value / 10)]
[
ones = value - 10 * tens]

[
setState("_" + ones, 1)]
[
setState(tens + "_"1)]
 


** Code is not tested for use, but is meant as an explaination of the general structure

User avatar
wolph42
Deity
Posts: 9779
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Macro to increase counter on token

Post by wolph42 »

@Akodo (wondering whether the OP ever going to visit back): true, funny thing cause that was the other method I was thinking of, and its much more cleaner. I only got stuck on the 'tens' and 'ones' which I wanted to do with string functions (and thats a mess) it never occurred to me to do it numerically (even with having a framework riddled with EXACTLY that method (1d100 system)) go figure...

anyway tiny remark:

Code: Select all

[IF (isNumber(value)): ""; value = 0]
and
[IF(value > 99): abort("STATE_COUNT Too Large"); ""]

its a bit neater to do

Code: Select all

[IF (!isNumber(value)): value = 0]
and
[assert(value < 100, "STATE_COUNT Too Large", 0)]

Akodo Makama
Giant
Posts: 249
Joined: Mon Apr 20, 2009 9:31 pm

Re: Macro to increase counter on token

Post by Akodo Makama »

wolph42 wrote:its a bit neater to do

Code: Select all

[IF (!isNumber(value)): value = 0]
and
[assert(value < 100, "STATE_COUNT Too Large", 0)]


I always find negative logic harder to follow (and sometimes harder to notice), so I try to avoid excess use of the ! operator. As assert(x...) is pretty much the same as if(!x, abort(...)), I tend to stay away from it as well. Also, I have a vague recollection of [IF:] leaving stray marks around if they didn't have an else clause, but it's been a long time.

User avatar
aliasmask
Deity
Posts: 8603
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Macro to increase counter on token

Post by aliasmask »

This is my take on your function which should be done as a UDF with output suppressed.

Code: Select all

@@ @incrementStateCount
<!-- incrementStateCount(step) 
   step 
- The increment to change the state counter
            Counter only goes from 0 to 99
            Step increment goes from 
-10 to 10
            A step of 0 clears the states and counter

   This function will take the current 
"count" and update the states on token.
-->
[
H, if(argCount()): countStep = arg(0); countStep = 1]

<!--
 check to clear counter if step is 0 or if counter is 0. Enforce step range and number type -->
[
H, if(isNumber(countStep)): countStep = min(10,max(-10,floor(countStep)))]
[
H, if(! countStep): clearStates = 1; clearStates = 0]

<!--
 get current state count value -->
[
H: currentCount = getProperty("stateCount")]
[
H, if(json.isEmpty(currentCount)): currentCount = 0]
[
H: newCount = min(99,max(0,currentCount + countStep))]

<!--
 clear existing count states -->
[
H, for(i,0,10), code: {
   [H: setState("Count0"+i,0)]
   [H: setState("Count"+i+"0",0)]
}]

<!--
 set new states -->
[
H, if(! clearStates && newCount), code: {
   [H: stateTens = floor(newCount/10)]
   [H: stateOnes = newCount - (10 * stateTens)]
   [H, if(stateTens): setState("Count"+stateTens+"0")]
   [H: setState("Count0"+stateOnes)]
};{
   [H: newCount = 0]
}]

<!--
 save counter -->
[
H: setProperty("stateCount",newCount)]

!!
 

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Macro to increase counter on token

Post by JamzTheMan »

Akodo Makama wrote:
wolph42 wrote:its a bit neater to do

Code: Select all

[IF (!isNumber(value)): value = 0]
and
[assert(value < 100, "STATE_COUNT Too Large", 0)]


I always find negative logic harder to follow (and sometimes harder to notice), so I try to avoid excess use of the ! operator. As assert(x...) is pretty much the same as if(!x, abort(...)), I tend to stay away from it as well. Also, I have a vague recollection of [IF:] leaving stray marks around if they didn't have an else clause, but it's been a long time.


FYI, Abort doesn't take a "Message" parameter like Assert does, it just aborts or doesn't based on 0 or non-zero value passed. Also, for loop only goes 0-8 so changed it to 10 so it counts 0-9 (ok, not sure why MT does that, it could be a bug, I never used FOR in MT before)

But I did like the simplicity of it. I actually had a +/- macro for Adversary Points (Pathfinder deck) but only went to 9. So I touched up the code and tested it, posted for others in case they want ready to go code.

I added if conditions to only show state > 0 and hide leading 0's.

[spoiler=Add Points]

Code: Select all

[H: value = getProperty("AdversaryPoints")]
[H, IF (isNumber(value)): ""; value = 0]

[H: value = value + 1]
[H: assert(value <= 99, "Maximum Adversary Points of 99 Reached.", 0)]

[H: setProperty("AdversaryPoints", value)]

[H, FOR(ones, 0, 10): setState("_" + ones, 0)]
[H, FOR(tens, 0, 10): setState(tens + "_", 0)]

[H: tens = floor(value / 10)]
[H: ones = value - 10 * tens]

[H, IF(value > 0): setState("_" + ones, 1); ""]
[H, IF(value > 9): setState(tens + "_", 1); ""]

[r: getName()] Now has [r: AdversaryPoints] Adversary Points.
[/spoiler]

[spoiler=Subtract Points]

Code: Select all

[H: value = getProperty("AdversaryPoints")]
[H, IF (isNumber(value)): ""; value = 0]

[H: value = value - 1]
[H: assert(value >= 0, "Minimum Adversary Points of 0 Reached.", 0)]

[H: setProperty("AdversaryPoints", value)]

[H, FOR(ones, 0, 10): setState("_" + ones, 0)]
[H, FOR(tens, 0, 10): setState(tens + "_", 0)]

[H: tens = floor(value / 10)]
[H: ones = value - 10 * tens]

[H, IF(value > 0): setState("_" + ones, 1); ""]
[H, IF(value > 9): setState(tens + "_", 1); ""]

[r: getName()] Now has [r: AdversaryPoints] Adversary Points.
[/spoiler]
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

Post Reply

Return to “Macros”