Using eval to retrieve info from an object

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
nirkedar
Cave Troll
Posts: 52
Joined: Tue Sep 21, 2010 3:13 pm

Using eval to retrieve info from an object

Post by nirkedar »

I need to dynamically pass a formula that involves "json.get(Object, Field)" to eval. How do I it?

Example: I need to know what level of Wizard the token is.
eval('json.get({"Barbarian":0,"Bard":0,"Cleric":0,"Druid":1,"Fighter":0,"Monk":0,"Paladin":0,"Ranger":0,"Rogue":0,"Sorcerer":0,"Warlock":0,"Wizard":2}, "Wizard")')

This will generate an error.

[H: Data = json.set("{}", "Barbarian", 0,"Bard", 0, "Cleric", 0, "Druid", 1, "Fighter", 0, "Monk", 0, "Paladin", 0, "Ranger", 0, "Rogue", 0, "Sorcerer", 0, "Warlock", 0, "Wizard", 2)]
[H: Formula = concat("json.get(", Data, ", 'Wizard')")]

[H: broadcast(json.type(Data) + ". " + Formula)]
outputs: OBJECT. json.get({"Barbarian":0,"Bard":0,"Cleric":0,"Druid":1,"Fighter":0,"Monk":0,"Paladin":0,"Ranger":0,"Rogue":0,"Sorcerer":0,"Warlock":0,"Wizard":2}, 'Wizard')
[H: Value = eval(string(Formula))]
generates an error

What am I doing wrong?

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Using eval to retrieve info from an object

Post by aliasmask »

It's hard to tell what you're trying to accomplish but it seems like you're making it more complicated than it needs to be. Perhaps you want to use Wiki: evalMacro() instead and create a string that looks like macro code.

nirkedar
Cave Troll
Posts: 52
Joined: Tue Sep 21, 2010 3:13 pm

Re: Using eval to retrieve info from an object

Post by nirkedar »

I know that. However in this case I need to evaluate it dynamically. I was just detailing the crux of the issue, the rest is far too lengthy.


Post Reply

Return to “Macros”