In sugarcube 2 (twine 2), how can I evaluate a condition stored as a string in a variable.
Example:
<<set $condition = "$var1 gt 1 or $var2 gt 1">>
<<if $condition>> /* it should be evaluated as <<if $var1 gt 1 or $var2 gt 1>> and NOT as if $condition is blank or not*/
<<print "condition is true, i.e. $var1 is greater than 1 or $var2 is greater than 1"
<</if>>
Essentially, the condition to be used is dynamic, and hence I want to store & set it in a variable instead of hard coding it.
How can this be done?
Thanks in advance for your time and inputs.
Comments
If you can get away with it, you'd be better off simply storing the result of the condition. For example: At that point, $condition will be set to the boolean result of the logical expression and you'd simply check that value as normal. For example:
If you cannot do that due to temporal reasons—i.e. the values of the variables will change between point where you setup the condition and the <<if>> where you need to check it—then you may evaluate your TwineScript code string like so: n.b. If you do this and need to use strings as part of your conditional, pay attention to your quotes.