UDF Defaults

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
User avatar
Tanthos
Giant
Posts: 176
Joined: Thu Mar 28, 2013 6:55 pm
Location: United States

UDF Defaults

Post by Tanthos »

Is there an eeeeevil trick which would allow me to have default values in UDFs similar to MTscript functions?

For example, on getProperty(), I must enter the property name, but the token ID is optional, and defaults to current token.

Could I do something like that for a UDF?

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

Re: UDF Defaults

Post by aliasmask »

That's more related to the code inside the UDF rather the UDF formatting. This is what you have control of:

Code: Select all

defineFunction(function, macro, ignoreOutput, newScope)
What you're talking about is setting values inside your macro code for the UDF which is unrelated to defining a function.

What token is being impersonated is determine by where the original macro is run, the apply to selected setting of that macro and not the macros/functions being called. Using built in functions like Wiki: currentToken() and Wiki: getSelected() can usually tell you what's going on with which tokens to apply changes to or get data from.

User avatar
Tanthos
Giant
Posts: 176
Joined: Thu Mar 28, 2013 6:55 pm
Location: United States

Re: UDF Defaults

Post by Tanthos »

So what would be a good method to make a default value in a UDF?

Let's take an easy example, using a boolean.

Code: Select all

Macro.Test
_________
[bool = arg(0)]
In the event that the Macro.Test were to be run as Macro.Test() without any variable input, could I configure a way to allow the default to be 0 (True)?

It is entirely possible that I am missing an absurdly easy solution.

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

Re: UDF Defaults

Post by aliasmask »

Use an [if:] statement to set default values if there is no argument.

Code: Select all

[H, if(argCount()): bool = arg(0); bool = 0]
[H, if(argCount() >= 2): bool2 = arg(1); bool2 = 0]
BTW, 0 is False for a boolean and any other number returns True, but classically 1 is True.

I like to create or preface my boolean variables with a verb. For example, these would be good boolean names:

Code: Select all

continue
doLoop
hasProperty
isDone
That way you can answer the question as yes or no. Also, make sure you assign the logic properly where a true means yes. So, I wouldn't end a loop if doLoop is true. I would name it exitLoop or something.

As for setting default values when defining the UDF I use this or a form of it:

http://forums.rptools.net/viewtopic.php ... 56#p209019

When MapTool starts, it creates all the macros as UDFs automatically setting the default ignoreOutput to True and newScope to True.

But if you do this, you probably need to create an output macro using broadcast, which I recommend doing.

Post Reply

Return to “Macros”