findToken: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Added version.)
No edit summary
 
(9 intermediate revisions by 3 users not shown)
Line 3: Line 3:
|trusted=true
|trusted=true
|version=1.3b48
|version=1.3b48
|description=Finds a [[Token:token|token]] on the current [[Map:map|map]] by the [[Token:token|token]] name or GM name and returns its id. If the [[Token:token|token]] is not found then an empty string "" is returned.
|description=Finds a [[Token:token|token]] on the current [[Introduction to Mapping|map]] by the [[Token:token|token]] name, GM name, or ID and returns its id. If the optional ''mapRef'' parameter is supplied that map will be searched instead. If the [[Token:token|token]] is not found then an empty string "" is returned.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
findToken(name)
findToken(tokenRef)
</source>
findToken(tokenRef,mapRef)
</syntaxhighlight>
'''Parameters'''
{{param|name/ID|Either the token [[getSelected|{{code|id}}]] or [[getTokenName|Token Name]] of the token.}}
{{param|mapRef|Optional Name or ID of the map to search for the token on.}}
{{Note|Token IDs are unique, but Token Names can be duplicated. Using Token Name when more than one token has the same name can produce unexpected results.}}


|example=
|example=
<source lang="mtmacro" line>
Search for token on current map
<syntaxhighlight lang="mtmacro" line>
[h: id = findToken("Hero")]
[h: id = findToken("Hero")]
[if (id == "", "Token not found!", "Token found")]
[if (id == "", "Token not found!", "Token found")]
</source>
</syntaxhighlight>
Search for token on current map and then search the map named ''Stash'' if not found.
<syntaxhighlight lang="mtmacro" line>
<!-- Prompts for "TokenName" as it isn't already defined -->
[h: name = TokenName]
[h: id = findToken(name)]
[r, if(id == ""), code: {
Not on current map. Searching Stash.<br>
<!-- Search on the map named "Stash" -->
[h: id = findToken(name, "Stash")]
[r: if(id == "", name + " not found!", name + " found in Stash")]
};{
[r: name] found on map [r: getCurrentMapName()].
}]
</syntaxhighlight>
 
}}
}}
[[Category:Find Function]]
[[Category:Find Function]][[Category:Token Function]]

Latest revision as of 23:59, 13 May 2024

findToken() Function

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

Introduced in version 1.3b48
Finds a token on the current map by the token name, GM name, or ID and returns its id. If the optional mapRef parameter is supplied that map will be searched instead. If the token is not found then an empty string "" is returned.

Usage

findToken(tokenRef)
findToken(tokenRef,mapRef)

Parameters

  • name/ID - Either the token id or Token Name of the token.
  • mapRef - Optional Name or ID of the map to search for the token on.
Token IDs are unique, but Token Names can be duplicated. Using Token Name when more than one token has the same name can produce unexpected results.

Example

Search for token on current map
[h: id = findToken("Hero")]
[if (id == "", "Token not found!", "Token found")]

Search for token on current map and then search the map named Stash if not found.

<!-- Prompts for "TokenName" as it isn't already defined -->
[h: name = TokenName]
[h: id = findToken(name)]
[r, if(id == ""), code: {
	Not on current map. Searching Stash.<br>
	<!-- Search on the map named "Stash" -->
	[h: id = findToken(name, "Stash")]
	[r: if(id == "", name + " not found!", name + " found in Stash")]
};{
	[r: name] found on map [r: getCurrentMapName()].
}]