As a scripting newbie, I was looking for a good way to build a switch. Simple on/off or stepping (such as Off, Low, Medium & High). I was looking at If-Else statements which seemed the most popular choice in the little bit of code that I have analyzed. Then I found a tutorial using states in a way that amounted to on and off. (it was at http://www.instructables.com/id/Getting-started-in-LSL-scripting-in-Second-Life/ ).
Here is the script I ended up using for my on/off switch:
default
{
state_entry()
{
llSetColor(<0,0,0>, ALL_SIDES);
}
touch_start(integer not_used)
{
state on;
}
}
state on
{
state_entry()
{
llSetColor(<1,1,1>, ALL_SIDES);
}
on_rez(integer not_used)
{
state default;
}
touch_start(integer not_used)
{
state default;
}
}
I don't see any reason why this can't be extended for however many steps I need in any switch. Although I did add a step to reset to off if the object was rezzed in the "on" state.
My question is: would it be more efficient from a programming point of view to use the If-Else option to do the switching? Or maybe I should say from a system point of view. Which would run better in the system?
It may be one of those things that doesn't really matter or more depends on the particular functions that might be contained at each point in the script.
A couple ancillary questions: is state persistent? That is, if on object is taken into inventory, will it always be the state it was in at the time it was taken into inventory whenever it is rezzed in-world again? Or is that not a function of state, but the way any script would work.
A second question: Did I just get lucky with the code (since it does in fact work) or is it correct to have two events set together and let the program branch based on which event occurs?
As noted in the title, these are newbie questions. I might as well try to learn the best way if I am going to try to learn this stuff.
