Confusion with the CODE: option

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
taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Confusion with the CODE: option

Post by taustinoc »

(This is 1.4.0.0, but I have the same results on all newer versions, including the latest beta)

I am trying to set an initiative value to all tokens based on the value of a property of the token.

When I do this:

Code: Select all

[h: ids = getTokens()]
[foreach(id, ids, "<br>"): id]
I get a nice, orderly list of token IDs:

Code: Select all

0000000087143829B915000000000000
0000000088143829D715000000000000
000000008A143829F515000000000000
000000007A1438292114000000000000
00000000931438299F16000000000000
000000008B1438291716000000000000
801438291315000000000000
But I need to do more than print the IDs, obviously, so I added in a CODE: option:

Code: Select all


[h: ids = getTokens()]
[foreach(id, ids), code:
{
<br>[id]
}]
and it gives me the same list of token IDs, but it puts a comma at the end of each line:

Code: Select all

0000000087143829B915000000000000,
0000000088143829D715000000000000,
000000008A143829F515000000000000,
000000007A1438292114000000000000,
00000000931438299F16000000000000,
000000008B1438291716000000000000,
801438291315000000000000
And I do not understand why, or how to get rid of it, because when I do SwitchToken it errors out, I think because of the comma. Except if I do an indexOf to check for the property in the token ID variable, it says -1, or there's no comma there.

I do not understand what is going on.

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

Re: Confusion with the CODE: option

Post by aliasmask »

The , is generated by the foreach when you don't have [h,] in front. You can put "" for the 3rd parameter to get rid of it. The reason you get the error is because the last id is in the form of a number rather than a 32 character string. This is a known issue and can be a pain to deal with. The easiest solution is to copy the offending token, delete it and then paste it back giving it a new id.

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Confusion with the CODE: option

Post by taustinoc »

aliasmask wrote:The , is generated by the foreach when you don't have [h,] in front. You can put "" for the 3rd parameter to get rid of it.
That's actually in the documentation, too. Sigh. Thank you.
aliasmask wrote: The reason you get the error is because the last id is in the form of a number rather than a 32 character string. This is a known issue and can be a pain to deal with. The easiest solution is to copy the offending token, delete it and then paste it back giving it a new id.
Or I could use GetTokenNames, I suppose, but there doesn't seem to be any guarantee that will always be unique.

The bug, from what I recall, is to strip off all the leading zeros when there's no letters in the ID, right? So the not-32 character IDs will always be the real ID, only without leading zeros?

Is there a reason I can't just generate a string of zeros of the appropriate length to put in front of the ID, and use that in SwitchToken? It was a royal pain in the donkey, because the string of 0s kept turning into a single one, but it does seem to work.

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

Re: Confusion with the CODE: option

Post by aliasmask »

It's an MT thing when trying to convert data to the correct type. So, if it looks like a number, it becomes a number.

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Confusion with the CODE: option

Post by taustinoc »

And it is very, very persistent about it. Even the string() function didn't work. But I got it, and my initiative macro works as I want it to.

Thanks for the help.

Post Reply

Return to “Macros”