can i pass agruments to a macro when using the macro command which creates a frame?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
busterbluth
Cave Troll
Posts: 45
Joined: Sat Aug 17, 2019 3:49 pm

can i pass agruments to a macro when using the macro command which creates a frame?

Post by busterbluth »

I'm working off this sample code:

Code: Select all

[macro("InvSheet@Lib:Frames"): ""]
I can't find in the wiki where I can pass arguments when calling a macro this way. would this be the syntax?

Code: Select all

[macro("InvSheet@Lib:Frames(" + variableName + )"): ""]

User avatar
aliasmask
RPTools Team
Posts: 9047
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: can i pass agruments to a macro when using the macro command which creates a frame?

Post by aliasmask »

Can be found in wiki, roll options, [macro:]. https://rptools.hyperbooks.com/index.ph ... ll_option)

It's after the :

You also may want to checkout Wiki: defineFunction() to make your own functions. It has advantages over normal macro syntax.

https://rptools.hyperbooks.com/index.ph ... d_Function

Here's an example I use for my own campaigns to automatically define all my functions on a lib token.

Code: Select all

<!-- onCampaignLoad

All macros will be given a UDF name excluding macros with tags and special characters

It is recommended to change the prefix to avoid conflict with other lib tokens. 
   Format is your initials (xx) and maybe a short code (lib) to represent the token.
   Functions can then be called by this format: xx.lib.functionName() 
-->

[H: prefix = ""]
[H: this = getMacroLocation()]

<!-- Define functions here with options other than ignoreOutput = 1 and NewScope = 1 -->
   <!-- Example. Feel free to delete -->
   [H: defineFunction(prefix+"reload","onCampaignLoad@"+this,0,1)]

<!-- default settings for functions. No output and parent variables not accessed without passing.
   Use broadcast() to send output to chat.
-->
[H: allMacros = getMacros("json")]
[H: ignoreOutput = 1]
[H: newScope = 1]

<!-- Define UDFs.  Already defined UDFs will not be redefined. -->
[H, foreach(macroName,allMacros), code: {
   <!-- get rid of any html in macro name -->
   [H: functionName = replace(macroName,"<[^>]*?>","")]
   <!-- remove special characters from macro name. Only numbers, letters, underscore and period allowed -->
   [H: functionName = replace(functionName,"[^a-zA-Z0-9_.]","")]
   <!-- validate new name matches original name -->
   [H: isValid = if(string(functionName) == string(macroName),1,0)]
   <!-- add the prefix for the final function name -->
   [H: functionName = strformat("%{prefix}%{functionName}")]
   [H, if(! isFunctionDefined(functionName) && isValid): 
      defineFunction(functionName,strformat("%{macroName}@"+this),ignoreOutput,newScope)]
}]

busterbluth
Cave Troll
Posts: 45
Joined: Sat Aug 17, 2019 3:49 pm

Re: can i pass agruments to a macro when using the macro command which creates a frame?

Post by busterbluth »

Thanks, Alias. Got a follow-up question. I get that you can only pass a single variable and JSON arrays are great for this. However, I'm passing a JSON array, and the called macro is only getting the first JSON object in that array.

"Items" is a JSON array of item objects, and they're read and manipulated by other macros in my campaign file with no problem.

Code: Select all

[h: arrItems = getProperty("Items", targetName)]

[macro("Pickup@Lib:Frames"):  arrItems]

this code is from the Pickup macro being called

Code: Select all


	      [h: arrItems = arg(0)]
              [h: class = "oddRow"]
              [foreach(item, arrItems, ""), CODE:{
                <tr class="[r:class]">
                 <td>[r: json.get(item, "ItemName")]</td>
i can set the value of arrItems to a global debug on a Lib token and see that arrItems in the called Macro is equal to the first item in my array, not to the array itself. Can you tell me what I need to do to get my JSON array read correctly by the called macro?

User avatar
aliasmask
RPTools Team
Posts: 9047
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: can i pass agruments to a macro when using the macro command which creates a frame?

Post by aliasmask »

Code: Select all

[macro("Pickup@Lib:Frames"):  json.append("[]",arrItems)]
if Pickup() was a UDF, then it would look like this

Code: Select all

[R: Pickup(arrItems)]
Because variables sent to the UDF macro are wrapped in to an array and variables using macro() roll option don't use the arg() function. They are expected to be in a string property format (ie "item1=one; item2=two;"). Using arg(0) just assumes the whole string is the first variable.

Post Reply

Return to “Macros”