Evaluating Expressions

Developer discussion regarding MapTool 1.4

Moderators: dorpond, trevor, Azhrei

Forum rules
Posting now open to all registered forum users.
Post Reply
Lord_Ashes
Kobold
Posts: 15
Joined: Tue Jun 18, 2019 7:43 pm

Evaluating Expressions

Post by Lord_Ashes »

I have a framework which stores a numbers of framework attributes. In a lot of cases instead of storing totals I store expressions which help identify where the total came from. For example, instead of "16" the attribute contents may be "10+4+2". As such when I am viewing these attributes I run them through an eval() to obtain the final value.

Initially this did not work because it worked for actual expressions but not for numeric values. Thus "10+4+2" would evaluate to "16" but "16" would give an evaluation error. I got around this by prefixing the contents with "0+". Thus when the contents are only a number, the number becomes an expression such as "0+16". If the contents are an expression then adding 0 to it does not change the value.

The problem that I am running into is that I have a helper macro that dumps all of the currently set attributes to the GM. This works great as long as the attributes are of one of the types mentioned above (i.e. numeric or expression). However, I also have a couple of expressions which are text. When I evaluate these using the same process as above, MapTools treats the text as a variable and (because it is not set) prompts the user for it.

Is there a way to determine if a string is text, number of expression? I know there is a isNumeric which identifies pure numeric entries but I have not found a good way to differentiate between string expressions and actual text strings (i.e. so that I can branch and not eval() them).

I would like to do something like:

Code: Select all

[r, if(isExpression(propVal)): prop + " is " + eval(propVal) ; prop + " is " + propVal ]
Is there any easy way to do this or do I need to write a custom function for this?

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

Re: Evaluating Expressions

Post by aliasmask »

You can still evaluate them to get a result, you just have to make sure any evaluated variables are also in the token type so it is defined for that token. You may also have to switchToken() so it gets the right value from that token.

Post Reply

Return to “MapTool 1.4”