getMatchingLibProperties: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Initial write-up.)
 
No edit summary
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{stub|Corrections to incomplete or possibly inaccurate information. Examples of usage.}}
{{MacroFunction
{{MacroFunction
|name=getMatchingLibProperties
|name=getMatchingLibProperties
Line 7: Line 6:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
getMatchingLibProperties(pattern)
getMatchingLibProperties(pattern)
</source>
<source lang="mtmacro" line>
getMatchingLibProperties(pattern, lib)
getMatchingLibProperties(pattern, lib)
</source>
<source lang="mtmacro" line>
getMatchingLibProperties(pattern, lib, delim)
getMatchingLibProperties(pattern, lib, delim)
</source>
</syntaxhighlight >
<source lang="mtmacro" line>
getMatchingLibProperties(pattern, lib, delim, id)
</source>
'''Parameters'''
'''Parameters'''
{{param|pattern|A regular expression(regex) that represents the pattern the properties should match.}}
{{param|pattern|A regular expression(regex) that represents the pattern the properties should match.}}
{{param|lib|The name of the [[Library Token]] that is checked for properties that match, defaults to the [[Library Token]] the macro is running on.}}
{{param|lib|The name of the [[Library Token]] that is checked for properties that match.  It defaults to the [[Library Token]] the macro is running on.}}
{{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. It defaults to {{code|","}}.  The return value is 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}} }}
|examples=
Assuming that you have a [[Library Token]] named {{code|Lib:Items}} that contained a list of all the items and their detail in your campaign stored as [[Token]] properties names with the following format {{code|''Type:Item Name''}} (for example {{code|Weapon:Longsword)}}, you could use the following code to loop through all the weapons:
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingLibProperties("Weapon:.*", "Lib:Items")): {
    <!-- Do something really exciting here -->
}]
</syntaxhighlight >


Or use the following to loop through all the armor:
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingLibProperties("Armor:.*", "Lib:Items")): {
    <!-- Do something really exciting here -->
}]
</syntaxhighlight >
Or even all the armor and all the shields:
<syntaxhighlight lang="mtmacro" line>
[foreach(item, getMatchingLibProperties("(Armor|Shield):.*", "Lib:Items")): {
    <!-- Do something really exciting here -->
}]
</syntaxhighlight >
|also=
|also=
{{func|getMatchingProperties}}
{{func|getMatchingProperties}}
{{func|getLibPropertyNames}}
|changes=
'''Updated in 1.11:'''
MTscript functions for acting on library tokens have been modified to also work on [[MTscript_functions_related_to_Add-on_libraries|Add-on libraries]].
This modification allows the namespace of the add-on library to be used where the {{code|Lib:}} token name would normally appear.  For example,


<syntaxhighlight lang="mtmacro" line>
[h: getMatchingLibProperties("(Armor|Shield):.*", "com.myname.mt.my-first-addon")]
</syntaxhighlight >
}}
}}
[[Category:Property Function]]
[[Category:Token Library Function]]
[[Category:Add-on Library Functions]]

Latest revision as of 23:59, 23 February 2024

getMatchingLibProperties() Function

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

Usage

getMatchingLibProperties(pattern)
getMatchingLibProperties(pattern, lib)
getMatchingLibProperties(pattern, lib, delim)

Parameters

  • pattern - A regular expression(regex) that represents the pattern the properties should match.
  • lib - The name of the Library Token that is checked for properties that match. It defaults to the Library Token the macro is running on.
  • delim - The delimiter used in the String List that is returned. It defaults to ",". The return value is a JSON Array if "json" is specified.

Examples

Assuming that you have a Library Token named Lib:Items that contained a list of all the items and their detail in your campaign stored as Token properties names with the following format Type:Item Name (for example Weapon:Longsword), you could use the following code to loop through all the weapons:
[foreach(item, getMatchingLibProperties("Weapon:.*", "Lib:Items")): {
    <!-- Do something really exciting here -->
}]

Or use the following to loop through all the armor:

[foreach(item, getMatchingLibProperties("Armor:.*", "Lib:Items")): {
    <!-- Do something really exciting here -->
}]

Or even all the armor and all the shields:

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

See Also

Version Changes

Updated in 1.11: MTscript functions for acting on library tokens have been modified to also work on Add-on libraries. This modification allows the namespace of the add-on library to be used where the Lib: token name would normally appear. For example,

[h: getMatchingLibProperties("(Armor|Shield):.*", "com.myname.mt.my-first-addon")]