This code works by grabbing each macro from the token, and evaluating the GROUP property. Any group that ends with "functions" will have all macros in that group defined as functions for the campaign. Capitalization of the macro name, specifically the word "function" will not matter as I convert the name to lower case for the test as you will see in the code. It is important that the group name end in function, otherwise if you want it to start with that, you can change the code to Wiki: startsWith() instead of Wiki: endsWith(), and can change the trigger name if you so wish.
The name of the function is the same as the macro, or you can add a prefix (as I have done) or suffix to uniquely identify them as yours. I am adding a prefix of "saw.fw." to the functions as you will see in the code. For others that may be new, as I am, I have heavily commented the code (each line has a comment), much more commenting than I normally do, but it helped reinforce the steps to myself, it is basically a pseudocode.
EDIT - Also, you will throw errors if you try to define a function with the same name as another function on the same token. For example, if on token Lib:Race_ClassDefinitions in group RaceFunctions you have a macro called "getNames" and in ClassFunctions you copied that token (probably inadvertantly) you will get "getMacroProps(): first argument must be a number." You will have to find the offending macros on your own unfortunately. If, by chance, you have a macro named identically on two different tokens, such as Lib:RaceDef with a macro getNames, and Lib:ClassDef with a macro getNames, the last macro defined will win, and you will get some odd results later in your code. I would recommend to include a unique identifier for each token. I chose to use "saw.fw." to identify my functions, but also added additional information to the macro name (and thereby carried to the function name). So, keeping with the thought process of making the first word of the defined macro a verb, I have getRaceName and getClassName as macros which are then defined as functions saw.fw.getRaceNameand saw.fw.getClassName.
Spoiler
Code: Select all
<!-- Grab the list of macros on this token -->
[h: allMacros = getMacros("json")]
<!-- Start a blank list of macros to be declared as functions -->
[h: functionNames=""]
<!-- Loop through the list of macros -->
[foreach(object, allMacros, ""), code:
{
<!-- Get the index of the current macro -->
[h: macroIndex=getMacroIndexes(object)]
<!-- Get the properties of the macro, stored in a JSON object -->
[h: macroDetails=getMacroProps(macroIndex,"json")]
<!-- Get the name of the macro -->
[h: macroName=json.get(macroDetails, "label")]
<!-- Get the name of the group and convert to lower case -->
[h: groupName=lower(json.get(macroDetails, "group"))]
<!-- Check to see if the group name ends in "functions" -->
[if(endsWith(groupName,"functions")), code:
{
<!-- If it does, add it to the list of macros to be declared as functions -->
[h: functionNames=json.append(functionNames,macroName)]
};{}
]
}
]
<!-- Loop through the list of macros to be declared as functions -->
[foreach(object,functionNames,""), code:
{
<!-- Add a prefix "saw.fw." to the macro name to create the function name-->
[h: defFunctionName="saw.fw."+object]
<!-- Define the macro to be called by using the macro name followed by "@". -->
<!-- Get the location (token name) of the token that contains the macro to -->
<!-- complete the name of the macro to be called by the function -->
[h: defMacroLocation=object+"@"+getMacroLocation()]
<!-- Use the defineFunction command to create the function -->
[h: defineFunction(defFunctionName,defMacroLocation)]
}
]