Building a variable.

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
User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Building a variable.

Post by Full Bleed »

I'm trying to create some local variables dynamically. Here is an example:

Code: Select all

[h: featNameList = "Anatomist, ArcaneStrike"]

[h: anatomistFeat = 1]
[h: arcaneStrikeFeat = 0]

[FOREACH (featName, featNameList), CODE:
	{
		[featName+"Selection" =  if (eval(featName+"Feat") > 0, 1, 0)]
	}
]
I'm trying to create "AnatomistSelection" and "ArcaneSelection" variables dynamically. But it's not actually adding the 0 or 1 value to it (in the FOREACH) for me to be able to call later.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."


Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: Building a variable.

Post by Craig »

What ever appears to the left of the equals sign can't be an expression, it has to be a variable/property name.
As Wolph mentioned you can use set to get around this

Code: Select all

[set(featName+"Selection", if (eval(featName+"Feat") > 0, 1, 0))]

Alternatively you could use eval as follows

Code: Select all

[h: AnatomistFeat = 3]
[h: ArcaneStrikeFeat = 0]

[h: featNameList = "Anatomist, ArcaneStrike"]

[h: anatomistFeat = 1]
[h: arcaneStrikeFeat = 0]

[h,FOREACH (featName, featNameList), CODE:
   {  
      [val =  if (eval(featName+"Feat") > 0, 1, 0)]
      [var = featName+"Selection"]
      [assignment = var + "=" + val]
      [eval(assignment)]
   }
]

[AnatomistSelection]
[ArcaneStrikeSelection]
but its harder to follow and will be slower (although I doubt in this case make any noticeable difference).

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Building a variable.

Post by Full Bleed »

wolph42 wrote:Capital letters vs non capital, might be an issue
Fortunately, that isn't the problem.
you need to use Wiki: set()
I was able to get it working with set though.

Craig wrote:What ever appears to the left of the equals sign can't be an expression, it has to be a variable/property name.
That's good information to know... Getting it to work is one thing... knowing why it didn't work the way I was trying is another. ;)
As Wolph mentioned you can use set to get around this

Code: Select all

[set(featName+"Selection", if (eval(featName+"Feat") > 0, 1, 0))]
And this looks better than the way I got it working!

Thanks guys.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Building a variable.

Post by Irrlicht »

You could also use a json object for that.

Code: Select all

[h: feats = json.set("", "Anatomist", 1, "ArcaneStrike", 0)]

[h: fields = json.fields(feats)]

[h, FOREACH (field, fields), CODE:
   {
      [set(field+"Selection", json.get(feats, field))]
   }
]

[r: AnatomistSelection], [r: ArcaneStrikeSelection]


"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

Post Reply

Return to “Macros”