Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Digital Lights

Rock Vacirca
riches to rags
Join date: 18 Oct 2006
Posts: 1,093
10-17-2009 00:47
Hi,

I want to have a digital light display of 8 lights, representing 8 digital bits, and incrementing from 0 to 255 in the normal digital way, i.e.

00000000
00000001
00000010
00000011
00000100

etc

Now, I thought (as usual, the long way round) I could do this by having a timer increment a variable X every second, from 0 to 255 then back to zero again, followed by 255 'If' statements, i.e

If X = 3 Then (and set the respective TRUE/FALSE values for each of the 8 Light variables L0 to L7)

and I could then define the status of the lights as being FALSE = Black and TRUE = white (or whatever colors or glow level I choose)


Tell me, there must be a better and more efficient way of achieving all this,

Thanks

Rock
Beverly Ultsch
Registered User
Join date: 6 Sep 2007
Posts: 229
10-17-2009 02:49
This is where you use bit masking.

A quick example to show the idea.

CODE

integer counter = 0;

setLight(integer lightNumber, integer lightState)
{
if(lightState)
{
// do stuff to turn light on
}
else
{
// do stuff to turn light off
}
}

checkLights(integer toCheck)
{
integer temp;
for(temp = 1; temp < 256; temp = temp << 1)
setLight(temp, toCheck & temp);
}


default
{
state_entry()
{
checkLights(counter);
llSetTimerEvent(1.0);
}
timer()
{
++counter;
if(counter > 255)
counter = 0;
checkLights(counter);
}
}
Rock Vacirca
riches to rags
Join date: 18 Oct 2006
Posts: 1,093
10-18-2009 05:05
Many thanks Beverley, that is just the sort of clarity on the problem I was hoping for.

Rock
Indeterminate Schism
Registered User
Join date: 24 May 2008
Posts: 236
10-18-2009 07:43
This is a recursive example where you only need to increment the least-significant bit/lamp and the others are incremented as required. NB: There is no decrement option in this example but it would be trivial to add. I've used linked messages so this could be triggered from an external script with minimal changes. Within the one script a global function would be more efficient.

list LampStates = [FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE];

default{
link_message(integer FromPrim, integer Value, string Message, key Additional){
if (Value < llGetListLength(LampStates)){ // Can adjust for 16-bit, etc.
if (Message == "Increment";){
if (llList2Integer(Value) == FALSE){
// ... TURN LAMP ON HERE ...
} else {
// ... TURN LAMP OFF HERE ...
llMessageLinked(LINK_THIS, ++Value, "Increment", NULL_KEY);}
LampStates = llListReplaceList(LampStates, [!llList2Integer(Value)], Value, Value);}}}

state_entry(){
llSetTimerEvent(1.0);
}

timer(){
llMessageLinked(LINK_THIS, 0, "Increment", NULL_KEY);}
}