Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Safe and Efficient Flow Control

Miriel Enfield
Prim Junkie
Join date: 12 Dec 2005
Posts: 389
03-27-2007 16:34
Argh. The tab key is going to be the death of me, I swear. Anyway, here's the entire post:

I'm scripting something, and it's occurred to me that I can make some of my if-else statements shorter. That is, when I've got a situation like this:

CODE
if(test)
{
do stuff
state NextState;
}
else
{
do other stuff
}


I can turn it into this:

CODE
if(test)
{
do stuff
state NextState;
}
do other stuff


My question is, is this safe? (Or any more efficient than just using else?) I don't want the script doing the "other stuff" even though the test is true. It seems like it should be okay, but I've had other things that ought to have been safe turn out not to be.
_____________________
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
03-27-2007 17:07
Yes, your change is safe and would be the way I would code that piece.

Of course, why not just:

CODE

if(code) state nextstate;
//do other stuff


And put your initial 'do stuff' into the state_entry() portion of your next state? You're headed there anyway...

My suggestion has NOTHING to do with performance or anything...just an extra throw in for organization.
_____________________
--AeonVox--

Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
Ravanne Sullivan
Pole Dancer Extraordinair
Join date: 10 Dec 2005
Posts: 674
03-27-2007 18:56
Not really safe. Without the else it will do stuff and then when finished it will do other stuff.
_____________________
Ravanne's Dance Poles and Animations

Available at my Superstore and Showroom on Insula de Somni
http://slurl.com/secondlife/Insula de Somni/94/194/27/
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
03-27-2007 19:13
From: Ravanne Sullivan
Not really safe. Without the else it will do stuff and then when finished it will do other stuff.
It shouldn't do "other stuff".... but only because there is a state change. When execution returns to the default state from the NextState, it does not pick up where it left off.
ed44 Gupte
Explorer (Retired)
Join date: 7 Oct 2005
Posts: 638
03-28-2007 03:56
Great! Your state change is acting as a virtual return!

iow
CODE

if (something_is_true) {doit (); return;}
if (something_else_is_true) {doit2 (); return;}

It would not pass Programming 101 but it helps the script fit inside the compiler's stack limits!