Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

binary switch

Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
04-19-2006 11:55
This is probably an incredibly obvious question, but my brain is just not coming up with the solution. I have a switch which I want to turn something on and off. I think the best way to do it is to have a global variable hold on to TRUE/FALSE. The best way I know to do it is:

CODE

integer status = FALSE;
.
.
.
status++;
if (status>1)
status -= 2;


Is there any easier way of handling this?

The only other thing I can think of is checking to see status first, then subtracting or adding one depending on what it is.

Baron Hauptmann
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
04-19-2006 11:56
status = !status
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
04-19-2006 12:06
I KNEW there had to be an obvious way!

Thanks.
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
04-19-2006 12:07
CODE

//this defaults to ON, I trust you can reverse it on your own, if you desire the opposite behavior ;)
default
{

state_entry()
{
llSay(0, "Wax on...");
}

touch_start(integer total)
{
state off;
}

}

state off
{

state_entry()
{
llSay(0, "Wax off.");
}


touch_start(integer total)
{
state default;
}

}

yetihehe Saarinen
Registered beast
Join date: 19 Feb 2006
Posts: 40
04-19-2006 12:12
From: Ordinal Malaprop
status = !status

status = 1 - status do the same
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
04-19-2006 12:58
I had considered states, too, but I assume that this one global variable makes it shorter,faster,smoother in the long run. Am I mistaken on this? Trying to make the script as inobtrusive as possible.

Baron
Eggy Lippmann
Wiktator
Join date: 1 May 2003
Posts: 7,939
04-19-2006 13:24
*shrug*
This issue comes up now and then. I don't really care what you do with your code.
As a matter of personal preference I like to code LSL using the language's mechanisms, as I believe it was meant to be used. Lots of scripts working together (the UNIX way), add states whenever concepts are mutually exclusive, or map well in general to a finite state machine.
States are pretty much the same as a global variable... for the 1 case it's pretty trivial to maintain the coherence of your global state.
For the N case... well, my overconfidence has burned me enough times that I distrust myself explicitly. You forget to set a variable in the middle of god knows how many KLOC and then everything crashes and you have no idea why :)
I try to code using a zero-knowledge approach so my code will be readable and maintainable by a complete noob, or by myself when I've long forgotten its internal structure and logic.
One thing about states though, if you use global variables you cannot have state-specific events. Your code will receive all events sent to it regardless of the state it's in. This can often force you to take the state approach.