[switch()] or [if()] - looking to speed up macro

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
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

[switch()] or [if()] - looking to speed up macro

Post by Xaelvaen »

I recently read Wolph42's 'Speeding up your Macros' section, and while I found the different if comparisons, I didn't find one comparing switch and if. Here's the code as I use it so far:
The Code Spam

Code: Select all

[h, switch(getFatigue) :
	case 0: fatigueCheck="";
	case 1: fatigueCheck="checked='checked'"]

[h, switch(getRepaired) :
	case 0: repairedCheck="";
	case 1: repairedCheck="checked='checked'"]

[h, switch(getIncapacitated) :
	case 0: sleepCheck="";
	case 1: sleepCheck="checked='checked'"]

[h, switch(getBleed) :
	case 0: bleedCheck="";
	case 1: bleedCheck="checked='checked'"]

[h, switch(getWound) :
	case 0: woundCheck="";
	case 1: woundCheck="checked='checked'"]

[h, switch(getHealed) :
	case 0: healedCheck="";
	case 1: healedCheck="checked='checked'"]

[h, switch(getParalyzed) :
	case 0: paralyzedCheck="";
	case 1: paralyzedCheck="checked='checked'"]

[h, switch(getBlind) :
	case 0: blindCheck="";
	case 1: blindCheck="checked='checked'"]

[h, switch(getDisarm) :
	case 0: disarmCheck="";
	case 1: disarmCheck="checked='checked'"]

[h, switch(getSunder) :
	case 0: sunderCheck="";
	case 1: sunderCheck="checked='checked'"]

[h, switch(getHaste) :
	case 0: hasteCheck="";
	case 1: hasteCheck="checked='checked'"]

[h, switch(getPower) :
	case 0: powerCheck="";
	case 1: powerCheck="checked='checked'"]

[h, switch(getFlight) :
	case 0: flightCheck="";
	case 1: flightCheck="checked='checked'"]

[h, switch(getMarked) :
	case 0: markedCheck="";
	case 1: markedCheck="checked='checked'"]

[h, switch(getDead) :
	case 0: deadCheck="";
	case 1: deadCheck="checked='checked'"]

[h, switch(getDazed) :
	case 0: dazedCheck="";
	case 1: dazedCheck="checked='checked'"]

[h, switch(getCharged) :
	case 0: chargedCheck="";
	case 1: chargedCheck="checked='checked'"]

[h, switch(getDefense) :
	case 0: defenseCheck="";
	case 1: defenseCheck="checked='checked'"]

[h, switch(getProne) :
	case 0: proneCheck="";
	case 1: proneCheck="checked='checked'"]

[h, switch(getCursed) :
	case 0: cursedCheck="";
	case 1: cursedCheck="checked='checked'"]

[h, switch(getSlowed) :
	case 0: slowedCheck="";
	case 1: slowedCheck="checked='checked'"]

[h, switch(getStaggered) :
	case 0: staggeredCheck="";
	case 1: staggeredCheck="checked='checked'"]

[h, switch(getDisabled) :
	case 0: disabledCheck="";
	case 1: disabledCheck="checked='checked'"]

[h, switch(getInjured) :
	case 0: injuredCheck="";
	case 1: injuredCheck="checked='checked'"]

[h, switch(getImmobile) :
	case 0: immobileCheck="";
	case 1: immobileCheck="checked='checked'"]

[h, switch(getPoison) :
	case 0: poisonCheck="";
	case 1: poisonCheck="checked='checked'"]
I'm wondering if anyone has some immediate knowledge as to whether this, or basic if statements would work better/faster.

Code: Select all

[h: poisonCheck=if(getPoison>0,"checked='checked'","")]
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy


User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: [switch()] or [if()] - looking to speed up macro

Post by aliasmask »

Classically the immediate if (if function, not roll option) is faster, but the difference should be negligible. The slow down comes from the parser, so the more lines it has to parse or the more internal checks it has to make then the longer it will take. But again, very negligible.

Personally, I don't use the if function very often because of readability. Also, it executes both sides of the true/false so that can be an issue depending on what you're doing. I also rarely use the switch preferring other methods of a list chooser or series of if statements.

FYI, it looks like you're setting the initial states of checkboxes. You don't have to do checked='checked'. You can simply put checked without the rest.

I would do something like this:

Code: Select all

[H: checkStates = "Fatigued,Repaired,Incapacitated,Bleed,Wound,Healed,Paralyzed,Blind,Disarm,Sunder,Haste,Power,Flight,Marked,Dead,Dazed,Charged,Defense,Prone,Cursed,Slowed,Staggered,Disabled,Injured,Immobile,Poison"]
[H, foreach(state,checkStates), code: {
   [H, set("check."+state,if(getProperty("get"+state),"CHECKED",""))]
}]

<!-- use this in your form -->
[H, foreach(state,checkStates), code: {
   [H: output = json.append(output,strformat(evalMacro('<input type="checkbox" name="%{state}" value="1" %{check.[r: state]}>%{state}</input>')))]
}]

<!-- printing out form -->
[R: json.toList(output,"<br>")]

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: [switch()] or [if()] - looking to speed up macro

Post by Xaelvaen »

Well, I had previously used [h, token(tokenID), CODE:{}] to run my macros that weren't about which tokens were targeted at the time, and switched to switchToken() based on the speeding up your macros information, and I did notice a pretty significant reduction in load time for my onChangeSelection character sheet, so I don't have to wait at all to move tokens after selecting them anymore. At the moment, I'm just looking to clean it up to the point of 'perfection'.

On that note, as to your suggestion of the foreach Alias, I'd wonder if that'd be viable for me. I arrange the states into four columns, with a checkbox at the bottom of the column, so I can mark states direction from the character sheet. I'd have to run the foreach 4 times with 4 separate lists, unless I'm missing a trick of how to arrange columns with a foreach. That seems like it'd be just as many lines of code as the switch or if, in my particular case. Feel free to educate me to the contrary hehe.
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: [switch()] or [if()] - looking to speed up macro

Post by aliasmask »

All you need to do is check the counter and insert you code to make a column. foreach sets roll.count each time it loops.

So, if you have a table.

Code: Select all

[H: output = "<table><tr><td>"]
[H, foreach(state,checkStates), code: {
   [H: output = json.append(output,strformat(evalMacro('<input type="checkbox" name="%{state}" value="1" %{check.[r: state]}>%{state}</input>')))]
   [H, if(! math.mod(roll.count+1,7)): output = json.append(output,"</td><td>"); output = json.append(output,"<br>")]
}]
[H: output = json.append(output,"</td></tr></table>")]

<!-- change output to no breaks -->
[R: json.toList(output,"")]

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: [switch()] or [if()] - looking to speed up macro

Post by Xaelvaen »

Took me a good long while to break that one down to understand it, but did the trick - thanks again Wolph and Alias!
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Post Reply

Return to “Macros”