Guide to onTokenMove

From RPTools Wiki
Jump to navigation Jump to search

... 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

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

onMultipleTokenMoves is only 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.

If you dont know what event to use you probably want to use onTokenMove.

If you use both events at the same time it is recommended that you use the tokens.moveCount variable in onTokenMove and abort if >1 and let onMultipleTokensMove handle it.

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