token (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Corrected a small typo)
No edit summary
 
(6 intermediate revisions by 3 users not shown)
Line 10: Line 10:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[token(identifier): single_statement]
[token(identifier): single_statement]
</source>
</syntaxhighlight>
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[token(identifier), code: {
[token(identifier), code: {
   statement1
   statement1
   statement2
   statement2
}]
}]
</source>
</syntaxhighlight>


The ''identifier'' is the name of the token.  The search first checks to see if the specified string is the Name of any token (i.e. matches [[token.name]]), then it checks GMName (i.e. matches [[token.gm_name]]), and last it checks the unique [[TokenID|token id]] (this is a 32-character string generated by MapTool).
The ''identifier'' is the name of the token.  The search first checks to see if the specified string is the Name of any token (i.e. matches [[token.name]]), then it checks GMName (i.e. matches [[token.gm_name]]), and last it checks the unique [[Glossary#T|token id]] (this is a 32-character string generated by MapTool).


If there are multiple tokens with the same name, such as '''Goblin''', then the ''GM Name'' field of the token should be unique so that it can be used to select tokens.  Note that the [[Macros:Functions:getAllTokens()|getAllTokens()]] and related functions return a list of token ids.
If there are multiple tokens with the same name, such as '''Goblin''', then the ''GM Name'' field of the token should be unique so that it can be used to select tokens.  Note that the [[getTokens|getTokens()]] and related functions return a list of token ids.
|examples=
|examples=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h, token("Goblin 21"): health = floor(100*HP/HPmax)]
[h, token("Goblin 21"): health = floor(100*HP/HPmax)]
The healthbar of <b>Gobling 21</b> should be at [r: health]%.
The healthbar of <b>Goblin 21</b> should be at [r: health]%.
</source>
</syntaxhighlight>
If '''Goblin 21''' has 32 hit points and a maximum hit points of 40 (and these numbers are stored in '''HP''' and '''HPmax''' properties, respectively), the output produced will be:
If '''Goblin 21''' has 32 hit points and a maximum hit points of 40 (and these numbers are stored in '''HP''' and '''HPmax''' properties, respectively), the output produced will be:



Latest revision as of 20:50, 20 April 2023

[token():] Roll Option

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

* Introduced in version 1.3.b48

token is a macro function that allows an associated code block to be executed as though it were executing with a specified token as the context. This allows the code block to use properties of the token without special coding.

This "context" function lasts only for the given code block. References to variables within the code block will still be available when the code block finishes. (Variables belong to the macro, while properties belong to the token.)

Usage

[token(identifier): single_statement]
[token(identifier), code: {
   statement1
   statement2
}]

The identifier is the name of the token. The search first checks to see if the specified string is the Name of any token (i.e. matches token.name), then it checks GMName (i.e. matches token.gm_name), and last it checks the unique token id (this is a 32-character string generated by MapTool).

If there are multiple tokens with the same name, such as Goblin, then the GM Name field of the token should be unique so that it can be used to select tokens. Note that the getTokens() and related functions return a list of token ids.

Examples

[h, token("Goblin 21"): health = floor(100*HP/HPmax)]
The healthbar of <b>Goblin 21</b> should be at [r: health]%.

If Goblin 21 has 32 hit points and a maximum hit points of 40 (and these numbers are stored in HP and HPmax properties, respectively), the output produced will be:

The healthbar of Goblin 21 should be at 80%.

See Also

currentToken(), switchToken(), floor().