Get token property from Initiative Json list

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Get token property from Initiative Json list

Post by AriesBreath »

This time I'm trying to write a macro for rerolling the initiative at the end of every round.
The idea behind this is that I press the macro every turn and when it comes to the end of the round, it reroll the initiative for every token in the initiative panel, it orders them and then it starts the new round.
In order to rolling the initiative, it must before get some properties (Dexterity) from those tokens and this is where it comes the trouble. If I delete the lines to get the property it works fine, but I need that (and others) properties and I can't understand why it doesn't work. This is the code:

Code: Select all

[r, IF(getCurrentInitiative() +1 == initiativeSize()), CODE:
{
 [h: roundNumber = getInitiativeRound()]
 [h: setCurrentInitiative(-1)]
 [h, IF(roundNumber==-1): roundNumber = 0; roundNumber = roundNumber + 1]
 [h: setInitiativeRound(roundNumber)]
 [h: initList = getInitiativeList()]
 [h: tokensdata = json.get(initList,"tokens")]
 [h, foreach(token, tokensdata), CODE: 
 {
  [h: switchToken(json.get(token,"tokenId"))]
  [h: Dex = getProperty("Dexterity")]
  [h: Dex = if(isNumber(Dex),floor((Dex-10)/2),0)]
  [h: setInitiative(1d6)]
 }]
 [h: sortInitiative()]
 [h: nextInitiative()]
 };
{
 [h: nextInitiative()]
}]
I even tried using "getProperty("Initiative", token)" and "getProperty("Initiative", getCurrent())" but it doesn't work. Can you please help me? Thanks

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

I don't see you using Dex in your calculation. Also, use [if:] instead of Wiki: if() because if Dex is not a number it will still try to calculate the values and possibly cause an error. Using the roll option won't do that.

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

Yeah, sorry, I didn't implement it in the calculation, it should be "setInitiative(1d20+Dex)" but that's not the problem because running this macro gives me this error and it just doesn't work: "Illegal argument type java.lang.String, expecting java.math.BigDecimal".

If I delete these two lines

Code: Select all

[h: Dex = getProperty("Dexterity")]
[h: Dex = if(isNumber(Dex),floor((Dex-10)/2),0)]
it works but I can't get the properties and that's not what I need.

Also in this line if Dexterity is not a number it should just return 0, shouldn't it?

Code: Select all

[h: Dex = if(isNumber(Dex),floor((Dex-10)/2),0)]

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

Try this:

Code: Select all

[H: if(isNumber(Dex)): DexMod = floor((Dex -10)/2); DexMod = 0]

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

Thanks, I'll try that, but the problem is that I can't get the property from the token.
getProperty doesn't seem to work and I don't know how to retrieve the value I need from the token that the macro is analyzing.

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

Is your macro on a lib token? Are you the GM? You'll need permission to get info from tokens either through ownership or a lib token which gives you permission. The macro needs to have Edit by players unchecked.

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

Is a campaign macro. Yes, I am the GM. Edit by players is already unchecked.

getProperty works if I select the tokens, but I need that to automatically take the tokens on the Initiative panel, get some information from them, roll and set their new initiative and order the panel. Getting those information is the part that doesn't work

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

There could be some bad token ids which could prevent you from accessing the token via the id. If the token is all numbers or ends in E followed by 0s, then it may not be switching to the token to get the info. Other than that your above code looks fine.

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

But if I try to get the property by manually selecting a token and using getProperty it does work, so I think the problem is in the method by which I try to get the property from the json info that getInitiativeList returns to me

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

verify by putting this line in there:

Code: Select all

[dialog("D"):{<pre>[R: json.indent(getInitiativeList())]</pre>}]

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

it returns this

Code: Select all

{
    "round": 65,
    "map": "Grasslands",
    "current": -1,
    "tokens":     [
                {
            "holding": false,
            "initiative": "5",
            "tokenId": "2E039EB8887C4BA6809ECE46182961FC"
        },
                {
            "holding": false,
            "initiative": "2",
            "tokenId": "C87875F4DDDF44EE896A5951ED25B622"
        },
                {
            "holding": false,
            "initiative": "1",
            "tokenId": "5D8ACFBC904447828F1292AB3A620B77"
        }
    ]
}

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

Well, I'm not seeing the problem. Everything seems to be in order. If you want to share your campaign file I can try running the macro on my system.

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

Yes sure, thanks for the help
Attachments
MacroInProgress.cmpgn
(516.46 KiB) Downloaded 25 times

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Get token property from Initiative Json list

Post by aliasmask »

Ah, missed the : where it should be a , after the if roll option.

Code: Select all

[r, IF(getCurrentInitiative() +1 == initiativeSize()), CODE: {
   [h: roundNumber = getInitiativeRound()]
   [h: setCurrentInitiative(-1)]
   [h, IF(roundNumber == -1): roundNumber = 0; roundNumber = roundNumber + 1]
   [h: setInitiativeRound(roundNumber)]
   [h: initList = getInitiativeList()]
   [h: tokensdata = json.get(initList,"tokens")]
   [h, foreach(token, tokensdata), CODE: {
      [H: tokenId = json.get(token,"tokenId")]
      [h: switchToken(tokenId)]
      [h: Dex = getProperty("Dexterity")]
      [h, if(isNumber(Dex)): DexMod = floor((Dex -10)/2); DexMod = 0]
      [h: setInitiative(1d20+DexMod)]
   }]
   [h: sortInitiative()]
   [h: nextInitiative()]
};{
   [h: nextInitiative()]
}]

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Re: Get token property from Initiative Json list

Post by AriesBreath »

Yes! It works, many thanks aliasmask, I really appreciate that.

I think this were the missing lines

Code: Select all

[H: tokenId = json.get(token,"tokenId")]
[h: switchToken(tokenId)]

Post Reply

Return to “General Discussion”