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?
UDF Defaults
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Re: UDF Defaults
That's more related to the code inside the UDF rather the UDF formatting. This is what you have control of:
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.
Code: Select all
defineFunction(function, macro, ignoreOutput, newScope)
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: UDF Defaults
So what would be a good method to make a default value in a UDF?
Let's take an easy example, using a boolean.
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.
Let's take an easy example, using a boolean.
Code: Select all
Macro.Test
_________
[bool = arg(0)]
It is entirely possible that I am missing an absurdly easy solution.
Re: UDF Defaults
Use an [if:] statement to set default values if there is no argument.
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:
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.
Code: Select all
[H, if(argCount()): bool = arg(0); bool = 0]
[H, if(argCount() >= 2): bool2 = arg(1); bool2 = 0]
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
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools