Guide to onTokenMove: Difference between revisions
(New page: =Guide to onTokenMove and all related stuff= Recently a great new feature has been added to MapTool. This guide shall aide you to use it. Every time a token is moved (by the user) a spec...) |
No edit summary |
||
Line 1: | Line 1: | ||
''... and all related stuff'' | |||
Recently a great new feature has been added to MapTool. This guide shall aide you to use it. Every time a token is moved (by the user) a specific macro is called. This macro can even cancel that move. | |||
=Events= | |||
Just like {{code|onCampaignLoad}} the new events are macros on a lib:token that have to be named like the event. Note that these events should be only defined once - otherwise can lead to unexpected behaviour. | |||
{{code|onTokenMove}} gets called whenever a token is moved. The token gets its moved path as macro.args. The moved token is the token in context. | |||
{{code|onMultipleTokenMoves}} is called when multiple tokens are moved at once. The macro.args contain a json array with all moved token ids. The is no token context. | |||
Note that, before {{code|onMultipleTokenMoves}} is actually called, {{code|onTokenMove}} is called for each single token. | |||
==Special Variables== | |||
{{code|tokens.denyMove}} has to be set to {{code|1}} to cancel the current movement. | |||
{{code|tokens.moveCount}} contains the number of tokens moved. | |||
=Paths= | |||
In context of these events there will sometimes be specified or returen a path or a list of coordinates. These all follow this specific format. | |||
It is a json array containing json objects for each points. Each json object defines the keys {{code|x}} and {{code|y}} with the map coordinates. | |||
<source lang="mtmacro"> | |||
[h: samplePath = json.append("", | |||
json.set("", "x", 50, "y", 50), | |||
json.set("", "x", 0, "y", 0) | |||
)] | |||
</source> | |||
=Related Functions= | |||
There is a number of functions that are very useful in combination with the onMove-events. | |||
Note that | ==getLastPath()== | ||
{{func|getLastPath}} returns the last path. Note that this returns exactly the same as is contained in {{code|macro.args}} in {{code|onTokenMove}}. | |||
==movedOverPoints(arrayOfCoordinates)== | |||
{{func|movedOverPoints}} returns an array of coordinates with all "hit" cells considering the specified array of coordinates. | |||
==movedOverToken(tokenName, [lastPath])== | |||
{{func|movedOverToken}} returns an array of coordinates with all "hit" cells where the moved token crosses the specified token. | |||
Note that the token must be specified by name - not id. | |||
==getMoveCount()== | |||
{{func|getMoveCount}} returns the calculated move cost according to the selected move metric. | |||
=Examples= | |||
Lets now give you some simple examples for most commons use cases. | |||
== | ==Traps/Teleporting== | ||
TO DO | |||
== | ==Movement cost tracking== | ||
<source lang="mtmacro"> | |||
<!-- this should be in onTokenMove --> | |||
<!-- moved token is in context --> | |||
<!-- get movement --> | |||
[h: mov = getProperty("Movement")] | |||
[h: usedMov = getMoveCount()] | |||
=== | <!-- deny move if not allowed, reduce mov prop otherwise --> | ||
[r, if( mov >= usedMov ), code: { | |||
[h: mov = mov - usedMov] | |||
[h: setProperty("Movement", mov)] | |||
};{ | |||
[h: tokens.denyMove = 1] | |||
<span style="color:red;font-weight:bold;">Move limit exceeded.</span> | |||
}] | |||
</source> | |||
==Exposure of Fog on gridless maps== | |||
TO DO |
Revision as of 19:47, 19 November 2010
... and all related stuff
Recently a great new feature has been added to MapTool. This guide shall aide you to use it. Every time a token is moved (by the user) a specific macro is called. This macro can even cancel that move.
Events
Just like onCampaignLoad
the new events are macros on a lib:token that have to be named like the event. Note that these events should be only defined once - otherwise can lead to unexpected behaviour.
onTokenMove
gets called whenever a token is moved. The token gets its moved path as macro.args. The moved token is the token in context.
onMultipleTokenMoves
is called when multiple tokens are moved at once. The macro.args contain a json array with all moved token ids. The is no token context.
Note that, before onMultipleTokenMoves
is actually called, onTokenMove
is called for each single token.
Special Variables
tokens.denyMove
has to be set to 1
to cancel the current movement.
tokens.moveCount
contains the number of tokens moved.
Paths
In context of these events there will sometimes be specified or returen a path or a list of coordinates. These all follow this specific format.
It is a json array containing json objects for each points. Each json object defines the keys x
and y
with the map coordinates.
[h: samplePath = json.append("",
json.set("", "x", 50, "y", 50),
json.set("", "x", 0, "y", 0)
)]
Related Functions
There is a number of functions that are very useful in combination with the onMove-events.
getLastPath()
getLastPath() returns the last path. Note that this returns exactly the same as is contained in macro.args
in onTokenMove
.
movedOverPoints(arrayOfCoordinates)
movedOverPoints() returns an array of coordinates with all "hit" cells considering the specified array of coordinates.
movedOverToken(tokenName, [lastPath])
movedOverToken() returns an array of coordinates with all "hit" cells where the moved token crosses the specified token.
Note that the token must be specified by name - not id.
getMoveCount()
getMoveCount() returns the calculated move cost according to the selected move metric.
Examples
Lets now give you some simple examples for most commons use cases.
Traps/Teleporting
TO DO
Movement cost tracking
<!-- this should be in onTokenMove -->
<!-- moved token is in context -->
<!-- get movement -->
[h: mov = getProperty("Movement")]
[h: usedMov = getMoveCount()]
<!-- deny move if not allowed, reduce mov prop otherwise -->
[r, if( mov >= usedMov ), code: {
[h: mov = mov - usedMov]
[h: setProperty("Movement", mov)]
};{
[h: tokens.denyMove = 1]
<span style="color:red;font-weight:bold;">Move limit exceeded.</span>
}]
Exposure of Fog on gridless maps
TO DO