getTokens: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
(Updated for b55 changes and created examples for the function.)
Line 1: Line 1:
{{stub|Examples that use the newest additions.}}
{{stub|Needs more examples.}}
{{MacroFunction
{{MacroFunction
|name=getTokens
|name=getTokens
Line 34: Line 34:
*** {{code|from}} - A number specifying the minimum range that a token needs to be from the source.
*** {{code|from}} - A number specifying the minimum range that a token needs to be from the source.
*** {{code|upto}} - A number specifying the maximum range that a token can be from the source.
*** {{code|upto}} - A number specifying the maximum range that a token can be from the source.
*** {{code|metric}} - The distance metric to use, if it is not specified the default from the users preferences is used.
** {{code|area}} - A JSON object containing specific area information.
** {{code|area}} - A JSON object containing specific area information.
*** {{code|token}} - An optional field that contain the name or id of the token that resides at the center of the area. Defaults to the current token.
*** {{code|token}} - An optional field that contain the name or id of the token that resides at the center of the area. Defaults to the current token.
Line 39: Line 40:
**** {{code|x}} - The relative {{code|x}} position of the cell in relation to the {{code|token}} field. Measured in cells.
**** {{code|x}} - The relative {{code|x}} position of the cell in relation to the {{code|token}} field. Measured in cells.
**** {{code|y}} - The relative {{code|y}} position of the cell in relation to the {{code|token}} field. Measured in cells.}}
**** {{code|y}} - The relative {{code|y}} position of the cell in relation to the {{code|token}} field. Measured in cells.}}
The movement metric in range specifies the movement metric use, the metric can be one of the following strings
* {{code|NO_GRID}} - The grid is ignored and straight line distance between the tokens is returned.
* {{code|ONE_TWO_ONE}} - First Diagonal movement costs 1, second 2, and so on (Square grid only).
* {{code|ONE_ONE_ONE}} - Diagonal movement costs a single square (Square grid only).
* {{code|MANHATTAN}} - Diagonal movement costs 2 (Square grid only).
* {{code|NO_DIAGONALS}} - No diagonal movement is allowed (Square grid only).


|example=
|example=
Line 46: Line 55:
[foreach(id, ids, "<br>"): id]
[foreach(id, ids, "<br>"): id]
</source>
</source>
Find and return a [[JSON Array]] containing all NPC tokens that are with 2 squares or hexes.
<source lang="mtmacro" line>
[h: cond = '{ range: {upto:2, useDistance:0}, npc:1 }']
[h: ids = getTokens("json", cond)]
</source>
Modifying the above example to exclude dead tokens.
<source lang="mtmacro" line>
[h: cond = '{ range: {upto:2, useDistance:0}, npc:1, unsetStates:["Dead"] }']
[h: ids = getTokens("json", cond)]
</source>
Get all of the non dead NPC tokens in the square above, below, left, and to the right of the token, using the {{code|area}} option.
<source lang="mtmacro" line>
[h: areaOffsets = '[ {x:1, y:0}, {x:0, y:1}, {x:-1, y:0}, {y:-1, x:0}]']
[h: area = json.set("{}", "offsets", areaOffsets)]
[h: cond = json.set("{}", "area", area, "npc", 1, "unsetState", "['Dead']")]
[h: ids = getTokens("json", cond)]
</source>
The same could be achieved using the {{code|range}} option with {{code|NO_DIAGONALS}} metric.
<source lang="mtmacro" line>
[h: cond = '{ range: {upto:1, useDistance:0, metric:"NO_DIAGONALS"}, npc:1, unsetStates:["Dead"] }']
[h: ids = getTokens("json", cond)]
</source>


|changes=
|changes=
{{change|1.3b49|Added {{code|json}} delimiter option.}}
{{change|1.3b49|Added {{code|json}} delimiter option.}}
{{change|1.3b51|Added {{code|conditions}} parameter.}}
{{change|1.3b51|Added {{code|conditions}} parameter.}}
{{change|1.3b55|Added {{code|metric}} option to {{code|range}} option in {{code|conditions}} parameter.}}
}}
}}
[[Category:Find Function]]
[[Category:Find Function]]

Revision as of 07:42, 14 April 2009

 This article is a stub, you can help the RPTools Wiki project by contributing content to expand this article.
 This article needs: Needs more examples.

getTokens() Function

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

Introduced in version 1.3b48
Gets a list containing the ids of all the tokens on the current map, or all the tokens that match the specified conditions. The type of the value returned depends on the delimiter parameter.

Usage

getTokens()
getTokens(delim)
getTokens(delim, conditions)

Parameters

  • delim - The delimiter used to sepearate the values in the String List that is returned, defaults to ",". If "json" is specified, a JSON array is returned instead of a String List.
  • conditions - A JSON object that contains various conditions that the tokens must fullfill. All conditions are optional.
    • setStates - A JSON array of states the token must have.
    • unsetStates - A JSON array of states the token must not have.
    • npc - If the token must be a NPC, set to true(1) or false(0).
    • pc - If the token must be a PC, set to true(1) or false(0).
    • selected - If the token must be selected, set to true(1) or false(0).
    • impersonated - If the token must be impersonated, set to true(1) or false(0).
    • current - If the token must be the current token, set to true(1) or false(0).
    • owned - If the token must be owned by a player, set to true(1) or false(0).
    • visible - If the token must be visible to players, set to true(1) or false(0).
    • range - A JSON object with range conditions, all range conditions are optional.
      • token - The id or name of the source token that the distance is measured from, defaults to the current token.
      • distancePerCell - If the Distance Per Cell multiplier should be used, set to true(1) or false(0).
      • from - A number specifying the minimum range that a token needs to be from the source.
      • upto - A number specifying the maximum range that a token can be from the source.
      • metric - The distance metric to use, if it is not specified the default from the users preferences is used.
    • area - A JSON object containing specific area information.
      • token - An optional field that contain the name or id of the token that resides at the center of the area. Defaults to the current token.
      • offsets - A JSON array of JSON objects that specify each individual cell that make up the area.
        • x - The relative x position of the cell in relation to the token field. Measured in cells.
        • y - The relative y position of the cell in relation to the token field. Measured in cells.


The movement metric in range specifies the movement metric use, the metric can be one of the following strings

  • NO_GRID - The grid is ignored and straight line distance between the tokens is returned.
  • ONE_TWO_ONE - First Diagonal movement costs 1, second 2, and so on (Square grid only).
  • ONE_ONE_ONE - Diagonal movement costs a single square (Square grid only).
  • MANHATTAN - Diagonal movement costs 2 (Square grid only).
  • NO_DIAGONALS - No diagonal movement is allowed (Square grid only).

Example

You can use the following code to print out the ids of all of the tokens on the current map.
[h: ids = getTokens()]
[foreach(id, ids, "<br>"): id]

Find and return a JSON Array containing all NPC tokens that are with 2 squares or hexes.

[h: cond = '{ range: {upto:2, useDistance:0}, npc:1 }']
[h: ids = getTokens("json", cond)]

Modifying the above example to exclude dead tokens.

[h: cond = '{ range: {upto:2, useDistance:0}, npc:1, unsetStates:["Dead"] }']
[h: ids = getTokens("json", cond)]

Get all of the non dead NPC tokens in the square above, below, left, and to the right of the token, using the area option.

[h: areaOffsets = '[ {x:1, y:0}, {x:0, y:1}, {x:-1, y:0}, {y:-1, x:0}]']
[h: area = json.set("{}", "offsets", areaOffsets)]
[h: cond = json.set("{}", "area", area, "npc", 1, "unsetState", "['Dead']")]
[h: ids = getTokens("json", cond)]

The same could be achieved using the range option with NO_DIAGONALS metric.

[h: cond = '{ range: {upto:1, useDistance:0, metric:"NO_DIAGONALS"}, npc:1, unsetStates:["Dead"] }']
[h: ids = getTokens("json", cond)]


Version Changes

  • 1.3b49 - Added json delimiter option.
  • 1.3b51 - Added conditions parameter.
  • 1.3b55 - Added metric option to range option in conditions parameter.