SecondLife Tutorial: Single Controller For Multiple Objects

Leave a comment Standard

Create a controller object with on button and an off button. Assign the on button code to the on button, the off button code to the off button and the block code to the object you want to effect with the controller. Each object should have a number in its description field. The object with number 1 will be the first object to appear. The object with number 2 will be the second, so forth and so on.

On Button Code:

//**************************
// Script Name: Block Controller 1.0
// Author: Jade Battery
// Purpose: turn the controller on
//**************************

integer controllerOn = FALSE; //have the other things respond
integer index = 1; //show the correct block

//**************************
// Purpose: Turn the controller on
// Precondition: none
// Postcondition: make sure all the blocks are off,
//   show the appropriate block above the controller
//**************************
default
{
state_entry()
{
//listen to this specific channel
llListen(1361, "",NULL_KEY, "");
llListen(1360, "",NULL_KEY, "");
llSetText("On", <1,1,1>, 1);
}

touch_start(integer total_number)
{

//they want the controller to start responding
if (controllerOn == FALSE)
{
controllerOn == TRUE;
llSay(0, "Online");

//tell all the other controller objects to start responding
llSay(1361, "controllerOn");

//set the index to zero
llSay(1361, "restart");

//hide all the blocks again
llSay(1360, "allOff");
index = 0;
}
}

//listen to see if they've touched a block
listen(integer channel, string name, key id, string message)
{

//check to see which block they touched
if (message == "Pressed")
{
//display that block above the controller
}
}
}//end default

Off Button Code

//**************************
// Script Name: Block Controller 1.0
// Author: Jade Battery
// Purpose: stop the controller from responding
//**************************

integer respond = FALSE; //have the other things respond
integer index = 0; //show the correct block

//**************************
// Purpose: Turn the controller on
// Precondition: none
// Postcondition: make sure all the blocks are off,
//   show the appropriate block above the controller
//**************************
default
{
state_entry()
{
//listen to this specific channel
llListen(1361, "",NULL_KEY, "");
llListen(1360, "",NULL_KEY, "");
llSetText("Off", <1, 1, 1>, 1);
}

touch_start(integer total_number)
{
//send a message to turn all the blocks off
if (respond == TRUE)
{
llSay(0, "Offline");
llSay(1360, "allOff");
llSay(1361, "controllerOff");
}
}

//listen to see if the controller is on or off
listen(integer channel, string name, key id, string message)
{
if (message == "controllerOn")
respond = TRUE;
if (message == "controllerOff")
respond = FALSE;
}
}//end default

Block Code

//**************************
// Script Name: Block Controller 1.0
// Author: Jade Battery
// Purpose: modify the appearance of the block based on actions
//     sent by the controller
//**************************

vector blockcolor = <1.0, 1.0, 1.0>; //color of the block
vector flashcolor = <1.0, 0.0, 1.0>; //color of the flashing block
integer isOn = TRUE; //the block is set to being off
integer i;

//**************************
// Purpose: Toggle the state of the block
//   if the block is on, show it
//   otherwise don't show it
// Precondition: none
// Postcondition: if block was showing it isn't anymore
//**************************
blockstate(integer flash)
{
//turn the block on
if(isOn == TRUE)
{
//change transparency
llSetAlpha(1, ALL_SIDES);
llSetLinkAlpha(LINK_ALL_CHILDREN, 1, ALL_SIDES);

//flash the block
if (flash == TRUE)
flashblock();

//turn phantom status off
llSetStatus( STATUS_PHANTOM, FALSE);

//change the color
llSetColor(blockcolor, ALL_SIDES);
llSetLinkColor(LINK_ALL_CHILDREN, blockcolor, ALL_SIDES);

}

//turn the block off
else if (isOn == FALSE)
{
//turn phantom status on
llSetStatus( STATUS_PHANTOM, TRUE);

//change transparency
llSetAlpha(0, ALL_SIDES);
llSetLinkAlpha(LINK_ALL_CHILDREN, 0, ALL_SIDES);
}
}

//**************************
// Purpose: Flash the block once it shows up
// Precondition: Block is visible
// Postcondition: if block was showing it isn't anymore
//**************************
flashblock()
{

for (i = 0; i < 100; i++)
{
//show the flashing color
if (i % 10 == 0)
{
llSetColor(flashcolor, ALL_SIDES);
llSetLinkColor(LINK_ALL_CHILDREN, flashcolor, ALL_SIDES);
}
else //show the normal color
{
llSetColor(blockcolor, ALL_SIDES);
llSetLinkColor(LINK_ALL_CHILDREN, blockcolor, ALL_SIDES);
}
}//end for loop
}//end flashing block

//**************************
// Purpose: Have the block listen for the controller
// Precondition: block description has been set to ID number
// Postcondition: block is listening and responding to
//   any message sent from the controller for this blockid
//**************************
default
{
state_entry()
{
//make it so they can walk through this
llSetStatus( STATUS_PHANTOM, TRUE);

//listen to this specific channel
llListen(1361, "",NULL_KEY, "");
llListen(1360, "",NULL_KEY, "");
llSay(1361,"Pressed" + (string)llGetObjectDesc());
}

//listen for something to happen
listen(integer channel, string name, key id, string message)
{

//listen for a message to turn all blocks off
if (message == "allOff")
isOn = FALSE;

//listen for a message to turn all blocks on
if (message == "allOn")
isOn = TRUE;

//the controller is calling to this block
if (message == llGetObjectDesc() && isOn == FALSE)
isOn = TRUE;
else if (message == llGetObjectDesc())
isOn = FALSE;

//update the current state
if (message == llGetObjectDesc())
blockstate(TRUE);
else
blockstate(FALSE);

}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s