RPTools.net

Discussion and Support

Skip to content

It is currently Thu Dec 14, 2017 12:56 am 






Reply to topic  [ 99 posts ]  Go to page Previous  1 ... 3, 4, 5, 6, 7

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Great Wyrm
 
Joined: Tue Aug 23, 2011 10:41 am
Posts: 1134
Location: Cornwall, UK
 Post subject: Re: Macro Best Practices
PostPosted: Sat Oct 15, 2011 9:10 am 
That's what I thought at first Rumble, which is why I edited my post. Unfortunately that edit came between your replies it appears. I'll run some tests:

macro"UDF"
Code:
[h: varX = "UDF"]

macro"Macro Clicked"
Code:
[h: varX = "default"]
[h: runUDF()]
[varX]


Remember, defineFunction is in the format defineFunction(function, macro, ignoreOutput, newScope).

when [h: defineFunction("runUDF","UDF@this",0,0)] ; varX = "UDF"
when [h: defineFunction("runUDF","UDF@this",0,1)] ; varX = "default"
when [h: defineFunction("runUDF","UDF@this",1,0)] ; varX = "UDF"
when [h: defineFunction("runUDF","UDF@this",1,1)] ; varX = "default"

So the controlling variable in the question is indeed newScope. I'm going to edit my original reply back to be correct.

_________________
How to get around the two code nest limit in MapTool (and MOTE)


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7967
Location: Bay Area
 Post subject: Re: Macro Best Practices
PostPosted: Sat Oct 15, 2011 12:20 pm 
davout wrote:
Barrodin wrote:
I chose to do the whole eval and assign deal because I specifically didn't want it to require me to remember the name of variables -inside- the function when I was writing code that called the function. By passing a variable name and evaluating it inside the function (and vice versa on the return), I can avoid either side having to have "magic names" for the input and output variable.


:shock: Oh my that is major mind shift from other languages.

Thanks.

Yeah, I find it best to avoid "magic names" too. What I also do with macros that share a scope with the parent is "scope" the variable names by putting local.varName or something similar so I know this variable is from there and stays there. Other magic variable that may appear would stand out. Also, even if I can use variables directly from the parent, I usually pass it. But in regards to this thread, we're talking about when it's more economical to reference them indirectly by name.

Code:
...
[
H: bigVarObject = {...}]
[
H: bigVarName = "bigVarObject"]
[
H: am.xx.processBigVarUDF(bigVarName)]
<!--
 bigVarObject would be updated by this line --> 

Code:
[H: local.varName = arg(0)]
[
H: set("local.varValue",eval(local.varName))]
<!--
 do stuff with local.varValue -->
...
<!--
 now update original object -->
[
H: set("local.varName",local.varValue)] 

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7967
Location: Bay Area
 Post subject: Re: Macro Best Practices
PostPosted: Sat Oct 15, 2011 12:28 pm 
Bone White wrote:
That's what I thought at first Rumble, which is why I edited my post. Unfortunately that edit came between your replies it appears. I'll run some tests:

macro"UDF"
Code:
[h: varX = "UDF"]

macro"Macro Clicked"
Code:
[h: varX = "default"]
[h: runUDF()]
[varX]


Remember, defineFunction is in the format defineFunction(function, macro, ignoreOutput, newScope).

when [h: defineFunction("runUDF","UDF@this",0,0)] ; varX = "UDF"
when [h: defineFunction("runUDF","UDF@this",0,1)] ; varX = "default"
when [h: defineFunction("runUDF","UDF@this",1,0)] ; varX = "UDF"
when [h: defineFunction("runUDF","UDF@this",1,1)] ; varX = "default"

So the controlling variable in the question is indeed newScope. I'm going to edit my original reply back to be correct.

Here's a more complicated version of my onCampaignLoad:

Code:
@@ @onCampaignLoad
[H: '<!-- All functions will be given a UDF name excluding tags and special characters -->']
[
H: prefix = "am.play."]
[
H: this = getMacroLocation()]

[
H: '<!-- Macro names ending with .html will allow output. "local" and "sub" also sets newScope to 0. -->']
[
H: outputExtensions = json.append("","html","local","out","output","sub","css")]

[
H: '<!-- Define functions here with options other than ignoreOutput = 1 and NewScope = 1 -->']
[
H: defineFunction(prefix+"jsonToVars","jsonToVars@"+this,1,0)] 
[H: defineFunction(prefix+"varsToJson","varsToJson@"+this,1,0)] 
[H: defineFunction("math.mod","mod@"+this,1)] 
[H: defineFunction("math.div","div@"+this,1)] 
[H: defineFunction("math.isOdd","isOdd@"+this,1)] 

[H: '<!-- List functions names here to exclude from UDF definition -->']
[
H: excludeList = json.append("","(new)","mod","div","isOdd")]
[
H: tableFunctions = json.difference(getMacros("json"),excludeList)]

[
H: '<!-- Define UDFs.  Already defined UDFs will not be redefined. -->']
[
H, foreach(fn,tableFunctions), code: {
   [H: fName = replace(fn,"<[^>]*?>","")]
   [H: fName = replace(fName,"[^a-zA-Z0-9_.]","")]
   [H: cleanFN = strformat("%{prefix}%{fName}")]
   [H: extension = listGet(cleanFN,listCount(cleanFN,".")-1,".")]
   [H: newScope = 1 - listContains("local,sub",extension)]
   [H: ignoreOutput = 1 - json.contains(outputExtensions,extension)]
   [H: exclude = listContains("x",extension)]
   [H, if(! isFunctionDefined(cleanFN) && ! exclude): defineFunction(cleanFN,strformat("%{fn}@"+this),ignoreOutput,newScope)]
}]

[
H: am.play.setupServer()]

!!
 

This is really convenient when I want to set the type of UDF options just by naming the macro a certain way.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Cave Troll
 
Joined: Wed Oct 12, 2011 1:28 am
Posts: 78
 Post subject: Re: Macro Best Practices
PostPosted: Sat Oct 15, 2011 4:18 pm 
aliasmask wrote:
What I also do with macros that share a scope with the parent is "scope" the variable names by putting local.varName or something similar so I know this variable is from there and stays there.


How far deep does this "no new scope" go? If you had UDF A call UDF B which calls UDF C, all defined with not having new scope. Could UDF C modify variables in UDF A? If yes, you may have to go beyond local.varName since it's possible other UDFs could have that. I guess you might have to use functionName.varName

For me, coming from Object-Oriented Programming where data encapsulation is held as sacred, having methods have access to variables of the caller warps my fragile little mind and gives me the heebee jeebees :)

_________________
My Stuff:
-- Easy navigation Maptool macro function doc
-- Bracket Checker
-- Notepad++ code completion for maptool macro language


Ramblings of a Dead Marshal: http://louis-davout.blogspot.com/


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7967
Location: Bay Area
 Post subject: Re: Macro Best Practices
PostPosted: Sat Oct 15, 2011 5:18 pm 
Yes, if you're doing more than one level you'll have to consider your scope variables. I never found the need to do that though.

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Cave Troll
 
Joined: Wed Oct 12, 2011 1:28 am
Posts: 78
 Post subject: Re: Macro Best Practices
PostPosted: Fri Dec 30, 2011 8:43 am 
Aliasmask,

On your onCampaignLoad how does outputExtensions work?

I created two macros both with the following line of code:
Code:
[R: macro.return = 1d20]


One is named: TestAutoDef and the other is TestAutoDef.output

When I call:
Code:
[H: ld_FC_TestAutoDef()]

The above does not produce any output as expected.

When I call:
Code:
[H: ld_FC_TestAutoDef.output()]

The above also does not produce any output, not as expected.

Am I missing something?

Thanks.

BTW ld_FC_ is my prefix.

_________________
My Stuff:
-- Easy navigation Maptool macro function doc
-- Bracket Checker
-- Notepad++ code completion for maptool macro language


Ramblings of a Dead Marshal: http://louis-davout.blogspot.com/


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7967
Location: Bay Area
 Post subject: Re: Macro Best Practices
PostPosted: Fri Dec 30, 2011 9:29 am 
The extensions I use are for Wiki: defineFunction() purposes. If you call TestAutoDef.output, that is the macro that should have the output.

So, given what you have, change it in the following way.
Code:
[H: macro.return = 1d20]

Code:
[R: ld_FC_TestAutoDef()]

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Cave Troll
 
Joined: Wed Oct 12, 2011 1:28 am
Posts: 78
 Post subject: Re: Macro Best Practices
PostPosted: Fri Dec 30, 2011 11:16 am 
OK, my example was a poor example. But your comment did help me come up with what I was expecting.

Code:
@@ @TestAutoDef
@PROPS@ fontColor=black;autoExecute=true;fontSize=1.00em;sortBy=;color=default;playerEditable=false;applyToSelected=false;group=Init;tooltip=;minWidth=
<html><table><tr><td>[R: 1d20]</td></tr></table></html>

!!
@@ @TestAutoDef.output
@PROPS@ fontColor=black;autoExecute=true;fontSize=1.00em;sortBy=;color=default;playerEditable=false;applyToSelected=false;group=Init;tooltip=;minWidth=
<html><table><tr><td>[R: 1d20]</td></tr></table></html>

!!


Code:
<!-- This produces NO output -->
[R:ld_FC_TestAutoDef()]

<!-- This produces output -->
[R: ld_FC_TestAutoDef.output()]


Thanks

_________________
My Stuff:
-- Easy navigation Maptool macro function doc
-- Bracket Checker
-- Notepad++ code completion for maptool macro language


Ramblings of a Dead Marshal: http://louis-davout.blogspot.com/


Top
 Profile  
 
 Offline
Kobold
 
Joined: Tue Apr 07, 2015 5:40 pm
Posts: 11
 Post subject: Re: Macro Best Practices
PostPosted: Sat Apr 18, 2015 7:59 pm 
Veggiesama wrote:
And... USE HTML COMMENTS on anything even remotely strange, and don't slack on the description. You'll thank yourself later when you put down the project for a month and try to jump back into it.


While I have yet to write anything for rptools, comments are always a good idea. Never wait too long, you will forget. I guarantee it. If you don't like comments for yourself, other people will want to read them if you share your framework.

_________________
--
Jim
AD&D first edition site and maps http://crestofastar.drivein-jim.net/ villages, cities, nations. No adverts either site.
My Traveller site, some deck plans http://travellergame.drivein-jim.net/ 1096 planet maps


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 99 posts ]  Go to page Previous  1 ... 3, 4, 5, 6, 7

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:

Who is online

In total there are 3 users online :: 1 registered, 0 hidden and 2 guests (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: Bing [Bot] and 2 guests





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman