| 05-28-2003, 01:49 PM | #1 |
I did a quick search, and didn't see anything related to this, so I thought I'd share my method for simulating the equivilant of case. The idea is to use a loop which will be used to represent the "switch" function, and the if statements inside of the loop will be used to represent "case" statements. Also, to simulate performaing a break, use exitwhen true as your last action inside of an if block. Code:
function MyFunction takes nothing returns nothing
loop
if (<condition>) then
<actions>
exitwhen true // break
endif
if (<condition>) then
<actions>
exitwhen true
endif
...
<actions> // Place actions here if you
// want the equivilant of a default case.
exitwhen true // This is necessary to exit
// the loop in case none of the if statements matched.
endloop
<actions> // Actions not related to case
endfunctionWhy use this? To reduce the amount of code that is executed. If you end up having 6 if statements used to perform actions based on some data, but you only want one of them to be performed, this method will help cutdown on the amount of if statements that are checked after the matching ones actions have been performed without needed to skip any actions that you want to perform after the checks. Extremely usefull if you have a lot of checks you wish to perform, but are only expecting to execute one set of actions. For reasons that are hopefully obvious, matches that are expected to happen the most often should be placed at the top of the loop. One could also use this to allow say any of the top three if statements actions to be performed regardless, but to choose to skip the remaining ones if the third matches, etc. |
| 05-29-2003, 12:19 PM | #2 |
Nice pointing this. I actually already use the same method but taking advantage of the return function ex : Code:
function MyFunction takes nothing returns nothing
if ( condition1 ) then
..................
return true
endif
if (condition2 ) then
..................
return true
endif
...
return false
endfunctionThe main difference is i don't use a loop. on the other hand it wasn't required as i only had to save checking too many conditions Note that this can also be achieved with using elses with the if, the problem is the length of the final testing chain Regards, And again, good idea ^^ |
| 05-29-2003, 02:09 PM | #3 |
Thanks. I use returns a lot too, but there have been instances where I either wanted to break out of checks early and still continue to perform some actions afterword, or allow a portion of the checks to perform and skip the rest. All without having to break them up into seperate, uneeded functions, and messy nested if/else statements. I know it is just an example, but couldn't help noticing that you return true and false, yet your function is declared to return nothing. ;) |
| 05-29-2003, 06:21 PM | #4 |
I am a bit distracted nowadays :) This said, i fully understand your point too, your method is very interesting, especially with the loop that would only break if one of the conditions is true and its actions run. As i said, that was nice pointing your method. Thanks again Regards |
| 05-30-2003, 12:05 PM | #5 |
i think using elseif it's less cpu intensive... function MyFunction takes nothing returns nothing loop if (<condition> ) then <actions> exitwhen true // break elseif (<condition> ) then <actions> exitwhen true endif ... <actions> // Place actions here if you // want the equivilant of a default case. exitwhen true // This is necessary to exit // the loop in case none of the if statements matched. endloop <actions> // Actions not related to case endfunction |
| 05-30-2003, 12:24 PM | #6 |
I'm curious as to why you think elseif's are less CPU intensive, especially since the interpreter has to take into account that there is an else/elseif block it needs to know about if the condition fails. The purpose of using this method is to break out of performing checks when you don't want to perform anymore, but still wish to perform some actions after the checks. If I did nested elseif's, the same process occurs, except that 1) It becomes more difficult to control allowing multiple conditions to match, while ignoreing others, without adding more conditions, thus actually adding to the execution time, and 2) when you want to perform a lot of checks, nested if's and elseif's just look messy. All in all, the amount of time it takes to perform conditional checks is not an issue. They pale in comparison to such tasks as rendering special effects and unit movements. For me this method offers me more flexible control, and greatly tidies up my control structure, making it easier to debug and program. |
