RPTools.net

Discussion and Support

Skip to content

It is currently Fri Oct 20, 2017 11:19 am 






Reply to topic  [ 6 posts ] 

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: [switch()] or [if()] - looking to speed up macro
PostPosted: Fri Sep 01, 2017 3:25 pm 
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:

Code:
[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:
[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

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Fri Mar 20, 2009 4:40 am
Posts: 9411
Location: Netherlands
 Post subject: Re: [switch()] or [if()] - looking to speed up macro
PostPosted: Fri Sep 01, 2017 6:00 pm 
irc switch is slightly faster

_________________
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC

My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent and
DPI.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7886
Location: Bay Area
 Post subject: Re: [switch()] or [if()] - looking to speed up macro
PostPosted: Fri Sep 01, 2017 7:46 pm 
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:
[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>")]

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: [switch()] or [if()] - looking to speed up macro
PostPosted: Sat Sep 02, 2017 8:08 am 
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

Image


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7886
Location: Bay Area
 Post subject: Re: [switch()] or [if()] - looking to speed up macro
PostPosted: Sat Sep 02, 2017 8:35 am 
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:
[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,"")]

_________________
Downloads:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Wed Aug 31, 2011 8:49 pm
Posts: 495
Location: Somewhere between Heaven and Hell
 Post subject: Re: [switch()] or [if()] - looking to speed up macro
PostPosted: Sun Sep 03, 2017 3:35 pm 
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

Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 6 posts ] 

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 2 users online :: 1 registered, 0 hidden and 1 guest (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 1 guest





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

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman