getMatchingProperties: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Conversion script moved page getMatchingProperties to GetMatchingProperties without leaving a redirect: Converting page title to first-letter uppercase)
No edit summary
 
(7 intermediate revisions by 3 users not shown)
Line 6: Line 6:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
getMatchingProperties(pattern)
getMatchingProperties(pattern)
getMatchingProperties(pattern, delim)
getMatchingProperties(pattern, delim)
getMatchingProperties(pattern, delim, id)
getMatchingProperties(pattern, delim, tokenRef)
getMatchingProperties(pattern, delim, id, mapname)
getMatchingProperties(pattern, delim, tokenRef, mapRef)
</source>
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
{{param|pattern|A [[Macros:regular_expression|regular expression(regex)]] that represents the pattern the properties should match.}}
{{param|pattern|A [[Macros:regular_expression|regular expression(regex)]] that represents the pattern the properties should match.}}
{{param|delim|The delimiter used in the [[String List]] that is returned, defaults to {{code|","}}.  Returns a [[JSON Array]] if {{code|"json"}} is specified.}}
{{param|delim|The delimiter used in the [[String List]] that is returned, defaults to {{code|","}}.  Returns a [[JSON Array]] if {{code|"json"}} is specified.}}
{{param|id|The token {{code|id}} of the token that is checked for properties that match the given pattern, defaults to the [[Current Token]]. {{TrustedParameter}} }}
{{param|tokenRef|Either the token [[getSelected|{{code|id}}]] or [[getTokenName|Token Name]] of the token that is checked for properties that match the given pattern, defaults to the [[Current Token]]. {{TrustedParameter}} }}
{{param|mapname|The name of the map to find the token.  Defaults to the current map.}}
{{param|mapRef|The Name or ID of the map to find the token.  Defaults to the current map.}}
{{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.}}


|examples=
|examples=
Line 22: Line 23:


Then to loop through all of the inventory properties you could use  
Then to loop through all of the inventory properties you could use  
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingProperties("Inv:.*")): {
[foreach(item, getMatchingProperties("Inv:.*")), code: {
     <!-- Do something really exciting here -->
     <!-- Do something really exciting here -->
}]
}]
</source>
</syntaxhighlight>


Or the following to loop through all the weapons
Or the following to loop through all the weapons
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingProperties("Inv:Weapon:.*")): {
[foreach(item, getMatchingProperties("Inv:Weapon:.*")), code: {
     <!-- Do something really exciting here -->
     <!-- Do something really exciting here -->
}]
}]
</source>
</syntaxhighlight>


Or even all the armor and all the shields.
Or even all the armor and all the shields.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingProperties("Inv:(Armor|Shield):.*")): {
[foreach(item, getMatchingProperties("Inv:(Armor|Shield):.*")), code: {
     <!-- Do something really exciting here -->
     <!-- Do something really exciting here -->
}]
}]
</source>
</syntaxhighlight>





Latest revision as of 23:59, 13 May 2024

getMatchingProperties() Function

Introduced in version 1.3b54
Returns a String List or JSON Array with names of the Token Properties on a specific Token that match the given pattern.

Usage

getMatchingProperties(pattern)
getMatchingProperties(pattern, delim)
getMatchingProperties(pattern, delim, tokenRef)
getMatchingProperties(pattern, delim, tokenRef, mapRef)

Parameters

  • pattern - A regular expression(regex) that represents the pattern the properties should match.
  • delim - The delimiter used in the String List that is returned, defaults to ",". Returns a JSON Array if "json" is specified.
  • tokenRef - Either the token id or Token Name of the token that is checked for properties that match the given pattern, defaults to the Current Token.

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

  • mapRef - The Name or ID of the map to find the token. Defaults to the current map.
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.

Examples

Say you wanted to keep an inventory list for the Token you could prefix all of your inventory properties with Inv:Category:. For example Inv:Weapon:Longsword.

Then to loop through all of the inventory properties you could use

[foreach(item, getMatchingProperties("Inv:.*")), code: {
    <!-- Do something really exciting here -->
}]

Or the following to loop through all the weapons

[foreach(item, getMatchingProperties("Inv:Weapon:.*")), code: {
    <!-- Do something really exciting here -->
}]

Or even all the armor and all the shields.

[foreach(item, getMatchingProperties("Inv:(Armor|Shield):.*")), code: {
    <!-- Do something really exciting here -->
}]

See Also

Version Changes

  • 1.5.4 - Added mapname parameter option.