Custom Robust eval Function: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
mNo edit summary
Line 20: Line 20:
</source>
</source>
<hr>
<hr>
<br>
Alternately, this version also handles the empty string ("") gracefully.
<hr>'''evalFunction'''
<source lang="mtmacro">
<!--
[H: assert( argCount() == 1, "eval() requires one parameter.")]
[H, IF( arg(0) == 0 ): macro.return = 0]
[H, IF( arg(0) == "" ): macro.return = ""]
[H, IF( arg(0) != 0 && arg(0) != ""): macro.return = oldFunction( arg(0) )]
-->
</source>
[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 15:23, 17 June 2009

 Note: This function can only be used in a Trusted Macro

This user defined function redefines the standard eval() function, allowing it to be given a zero and not throw an exception.

Macros

Place both of these macros on the same library token.


onCampaignLoad

[defineFunction("eval", "evalFunction@this")]



evalFunction

[h: assert( argCount() == 1, "eval() requires one parameter.")]
[h, if ( arg(0) == 0 ), code:
{
    [h: macro.return = 0]
};{
    [h: macro.return = oldFunction( arg(0) ) ]
}]


Alternately, this version also handles the empty string ("") gracefully.


evalFunction

<!--
[H: assert( argCount() == 1, "eval() requires one parameter.")]
[H, IF( arg(0) == 0 ): macro.return = 0]
[H, IF( arg(0) == "" ): macro.return = ""]
[H, IF( arg(0) != 0 && arg(0) != ""): macro.return = oldFunction( arg(0) )]
-->