Switch + GetRandom = confusion
by John Olsen · in Torque Game Builder · 01/05/2007 (9:34 pm) · 11 replies
I wanted to randomly select one of four results, so I tried doing this:
To save everyone else some pain and grief, don't do that. The script engine evaluates the expression for each case one at a time, meaning you get a different random number for each case. That means it can easily miss every case and do nothing at all inside the switch statement.
A good way to get the expected result is:
Thank goodness for the Torsion debugger. It would have taken me forever to figure out what was going on if I hadn't been able to single step through the function.
John
switch(GetRandom(0,3))
{
...
}To save everyone else some pain and grief, don't do that. The script engine evaluates the expression for each case one at a time, meaning you get a different random number for each case. That means it can easily miss every case and do nothing at all inside the switch statement.
A good way to get the expected result is:
%item = GetRandom(0,3);
switch(%item)
{
...
}Thank goodness for the Torsion debugger. It would have taken me forever to figure out what was going on if I hadn't been able to single step through the function.
John
#2
I'm not sure where, but I'm finding a place to put this into my code.
01/09/2007 (7:04 am)
This just makes me laugh. I'm completely sympathize with your efforts to figure this one out...we've all been there.I'm not sure where, but I'm finding a place to put this into my code.
#3
I was thinking about using it too. I'll drop it in to some obscure AI algorithm or something.
There's one way to make your code completely unreadable. =D
01/09/2007 (7:37 am)
I sympathize too. I guess that's why it seems so funny to me.I was thinking about using it too. I'll drop it in to some obscure AI algorithm or something.
There's one way to make your code completely unreadable. =D
#4
01/09/2007 (8:42 am)
That is wonky to the max! Never thought twice about how switch worked. Thanks for the note.
#5
Did never think that switch actually does dynamically recheck the value for each state instead of saving an intermedia value when checking the first case and compare against that one.
01/09/2007 (11:32 am)
Thanks for the note from over here as well.Did never think that switch actually does dynamically recheck the value for each state instead of saving an intermedia value when checking the first case and compare against that one.
#6
I think that many languages do actually just check once and branch on it - but I can't recall ever writing code that had anything in the condition of the switch than a variable, or something that would always return the same.
Wonky!
01/09/2007 (11:41 am)
Interesting implementation detail... :)I think that many languages do actually just check once and branch on it - but I can't recall ever writing code that had anything in the condition of the switch than a variable, or something that would always return the same.
Wonky!
#7
01/15/2007 (9:42 pm)
You said you wanted random. You got random!
#8
01/19/2007 (10:16 pm)
I think that functionality makes switch more useful, personally X_X
#9
Thank you, John, for giving an insight into the TorqueScript interpreter. I will add this to my list of gotchas (long and growing).
One of my favorites is the return value of ++:
Oh, I so enjoy my gotchas!
02/16/2007 (5:48 am)
People have said that TorqueScript's implementation of switch() was no more efficient than if () else if () ..., and John Olsen's experience seems to indicate why. There is no difference at all: switch( f( ) ) {case a: ; case b: ; default : ; }is simply interpreted asif ( f( ) == a ) {} else if ( f() == b ) {} else {}So, if the argument of the switch() statement has a side effect (as getRandom() does), then the outcome will be different depending on how many case : statements follow on. Fascinating.Thank you, John, for giving an insight into the TorqueScript interpreter. I will add this to my list of gotchas (long and growing).
One of my favorites is the return value of ++:
==>$i=0; ==>echo($i++); 1All you dyed-in-the-wool C weenies out there know the difference between the return value of ++i and i++, right? Well, it looks like whoever wrote the TorqueScript interpreter limited it to the syntax of postincrement while giving it the semantics of preincrement. ;-P
Oh, I so enjoy my gotchas!
#10
02/16/2007 (7:57 am)
This is not a secret. It is well documented that the postfix operator does not work the same way as many more formal languages. It is not uncommon for scripting languages to have these little nuances to the way they work. Also, you've stated it backwards, they've given it the semantics of preincrement and the syntax of postincrement (on purpose).
#11
(I know why I don't use ++ on scripting languages anymore, took too many hours in other languages already, not worth trying it again just for lazyness)
I don't have problems with it, but it shouldn't mention its familiarity to C if it breaks in common C assumptions taken as granted by more experienced users.
02/16/2007 (8:09 am)
Yes but those don't explicitely mention that the language is C like and break with a basic behavior of it :) (I know why I don't use ++ on scripting languages anymore, took too many hours in other languages already, not worth trying it again just for lazyness)
I don't have problems with it, but it shouldn't mention its familiarity to C if it breaks in common C assumptions taken as granted by more experienced users.
Torque Owner Thomas Buscaglia