ID:268066
 
say you're wanting to use a switch for picking a random number between 1-20

so you do
switch(1,20)
if(5 to 6)
blah blah blah


is this efficient?

compared to just doing this

proc/blah
var/randomnumber=rand(1,20)
if(randomnumber>=5 && randomnumber <=6)

????

is switch ok to use?
Jon Snow wrote:
switch(1,20)
if(5 to 6)
blah blah blah

Considering that would crash the procedure, I'd say the latter is more efficient.

If you ment 'switch(rand(1,20))', then I'm not sure. Declaring a variablt, setting, and checking? Or the way 'switch' is been programmed. If I've seen BYOND's source code I could give you a direct answer.

Try a check like this...
mob/verb
Check1()
for(var/I=0 to 10000)
var/Test=rand(1,20)
if(Test >= 5 && Text <= 6) continue
else continue
src << "Done!"
Check2()
for(var/I=0 to 10000)
switch(rand(1,20))
if(5 to 6) continue
else continue
src << "Done!"
See what is faster.
In response to Yota
hmm well the problem is I'm going to be using one or the other quite a few hundred times throughout the game... and lately I've been having problems with code that I was thinking might relate to switch so I was hoping someone would post something about what to do and what not to do with switch lol :)
In response to Jon Snow
OK. switch has only one paramiter.
switch(val)
Only give it the one. It can be any value.

Next, the following block are all if() (and maybe an else at the end) statements.
switch(val)
if(1) // val is 1.
if(2) // val is 2.
else // Not one or two.
Unlike normal if() statements, these can be given different types of values. They also should not use comparison.
switch(val)
if(1,3) // val is 1 or 3.
if(4 to 8) // val is between 4 and 8.
if("") // val is an empty string.
if(1>2) // =P No no!
else // None of the above.
That should be it. If I'm forgetting something that switch can do, someone follow me up. Otherwise, don't do anything else.
In response to Jon Snow
if(rand(1,20) <= 5)

This doesn't require a variable to be called, you can use less code by excusing the switch statement and the preceding if statements, and it checks the value too. The only problem is that you can't check more than one value.

For example. This would not work.

if(rand(1,20) == 5 || 6)

In order for that to work you would have to define a variable.

var/rand = rand(1,20)
if(rand == 5 || rand == 6)