assert: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Added examples.)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{stub|Examples of usage.}}
{{MacroFunction
{{MacroFunction
|name=assert
|name=assert
|version=1.3b49
|version=1.3b49
|description=
|description=
Halts execution and prints a custom error message if a condition is {{false}}.  Note that the error message will be displayed in chat even if the command itself is in a [H: ] block.
Halts execution and prints a custom error message if a condition is {{false}}.  Note that the error message will be displayed in chat even if the command itself is in a {{code|[H:&nbsp;]}} block.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
assert(condition, message)
assert(condition, message)
</source>
</syntaxhighlight>
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
assert(condition, message, prefix)
assert(condition, message, prefix)
</source>
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
{{param|condition|The test condition that must evaluate to {{true}} for the macro to continue.}}
{{param|condition|The test condition that must evaluate to {{true}} for the macro to continue.}}
{{param|message|The custom error message that is presented if the macro is halted due to the {{false}} condition.}}
{{param|message|The custom error message that is presented if the macro is halted due to the {{false}} condition.}}
{{param|prefix|Determines if the error message should have the message prefix {{code|"Macro defined error: "}}. Defaults to {{true}}, set to {{false}} if you do not wish your custom error message to have the message prefix.}}
{{param|prefix|Determines if the error message should have the message prefix {{code|"Macro defined error: "}}. Defaults to {{true}}, set to {{false}} if you do not wish your custom error message to have the message prefix.}}
|also=
{{func|abort}}
|changes=
{{change|1.3b51|Added {{code|prefix}} parameter option.}}


|examples=
|examples=
Checks to see if a player is a GM, and if they are not halts execution of the macro and displays output.
Checks to see if a player is a GM, and if they are not halts execution of the macro and displays output.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: assert(isGM(),"This macro is for GM use only.",0)]
[h: assert(isGM(),"This macro is for GM use only.",0)]
</source>
</syntaxhighlight>
Chat output (if player is not GM): {{code|This macro is for GM use only.}}
Chat output (if player is not GM): {{code|This macro is for GM use only.}}




Halts execution of the macro if var is a number.  This is useful for making sure the variables you work with are the variable type you expect.
Halts execution of the macro if {{code|var}} is a number.  This is useful for making sure the variables you work with are the variable type you expect.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: assert(! isNumber(var), "The variable is a number.",1)]
[h: assert(! isNumber(var), "The variable is a number.",1)]
</source>
</syntaxhighlight>
Chat output (if var is a number): {{code|Macro defined error: The variable is a number.}}
Chat output (if {{code|var}} is a number): {{code|Macro defined error: The variable is a number.}}


Bear in mind, isNumber(var) returns {{true}} when var is a number, but assert continues when condition is {{true}}.  Using {{code|! isNumber(var)}} inverts the boolean value.
Bear in mind, {{code|isNumber(var)}} returns {{true}} when var is a number, but assert continues when condition is {{true}}.  Using {{code|! isNumber(var)}} inverts the boolean value.
 
===Usage Notes===
When aborting a macro called from another macro (for example, a library token), all macros are aborted, not just the one executing.
Since 1.5.0 you can change that behaviour by using macro.catchAssert.  


|also=
|also=
{{func|isGM}},
{{ func|isGM}}
{{func|isNumber}}
{{ func|isNumber}}
{{ func|abort}}
[[ macro.catchAssert|macro.catchAssert]]
 
|changes=
{{change|1.3b51|Added {{code|prefix}} parameter option.}}
{{change|1.5.0|catch an assert with macro.catchAssert}}
 


}}
}}
[[Category:Miscellaneous Function]]
[[Category:Miscellaneous Function]]

Latest revision as of 23:59, 14 March 2023

assert() Function

Introduced in version 1.3b49
Halts execution and prints a custom error message if a condition is false(0). Note that the error message will be displayed in chat even if the command itself is in a [H: ] block.

Usage

assert(condition, message)
assert(condition, message, prefix)

Parameters

  • condition - The test condition that must evaluate to true(1) for the macro to continue.
  • message - The custom error message that is presented if the macro is halted due to the false(0) condition.
  • prefix - Determines if the error message should have the message prefix "Macro defined error: ". Defaults to true(1), set to false(0) if you do not wish your custom error message to have the message prefix.

Examples

Checks to see if a player is a GM, and if they are not halts execution of the macro and displays output.
[h: assert(isGM(),"This macro is for GM use only.",0)]

Chat output (if player is not GM): This macro is for GM use only.


Halts execution of the macro if var is a number. This is useful for making sure the variables you work with are the variable type you expect.

[h: assert(! isNumber(var), "The variable is a number.",1)]

Chat output (if var is a number): Macro defined error: The variable is a number.

Bear in mind, isNumber(var) returns true(1) when var is a number, but assert continues when condition is true(1). Using ! isNumber(var) inverts the boolean value.

Usage Notes

When aborting a macro called from another macro (for example, a library token), all macros are aborted, not just the one executing.

Since 1.5.0 you can change that behaviour by using macro.catchAssert.

See Also

Version Changes

  • 1.3b51 - Added prefix parameter option.
  • 1.5.0 - catch an assert with macro.catchAssert