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);

}
}

Second Life Tutorial: Communication Between Two Different Objects

Comments 5 Standard

Finished Lego HouseFor my summer science research project I needed to build a lego house that would interact with a controller mounted on a working elevator. In order to get the controller communicating with the blocks I had to tune them both to the same channel. Once they were listening to the same channel I could send messages between the blocks and the controller without problem.

Controller Code

default
{
     state_entry()
     {
          //listen to this specific channel
          llListen(1361, "",NULL_KEY, "");
          llListen(1360, "",NULL_KEY, "");
          llSetText("Play.", , 1);
     }

     touch_start(integer total_number)
     {
          llSay(0, "Playing!");
          llSay(1360, "Hi there!");
     }

     //listen for something to happen
     listen(integer channel, string name, key id, string message)
     {
          if(message = "3 White")
          {
               llSay(0,"Three piece white lego pressed!");
          }
     }
}

Block Code

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, "");
          //you can say something like this
          llSay(1361,"Pressed");
     }
     
     //listen for something to happen
     listen(integer channel, string name, key id, string message)
     {
          if(channel == 1360)
          {
               llSay(0,"Hahahaha! That tickles!!");
          }
     }

     //or whisper something like this and still
     //send a message to the controller
     llWhisper(1361, "3 White");

     }
}

SecondLife Tutorial: Working Elevator

Comments 9 Standard

I found code from another person in the SL wiki and tweaked it to my liking. It had some issues with coming back down after the SL avatar was on top of the elevator platform. I’ve also changed it so you can touch the elevator to move it and once the elevator is has moved to it’s new position you can move the avatar around instead of being glued to the center of it.

// lift script, v 1.0 by timeless montreal
// modified by Jade Battery
//put it in any object to transform it into an elevator
//touch the object to move it up/downinteger

liftAmount = 4;//how far to move up

integer isUp = FALSE; //current position of the object

movePlatform()
{
//makes you appear to be standing
llStartAnimation("stand");

if(!isUp)
{
//move the elevator up
llSetPos(llGetPos() + <0, 0, liftAmount>);
llSay(0, "Going up!");
isUp = TRUE;
key avataronsittarget = llAvatarOnSitTarget();
llUnSit(avataronsittarget);
}
else
{
//move the elevator back down
llSetPos(llGetPos() + <0, 0, -1*(liftAmount)>);
llSay(0, "Going down!");
isUp = FALSE;
key avataronsittarget = llAvatarOnSitTarget();
llUnSit(avataronsittarget);
}
}

default
{
state_entry()
{
llSitTarget(<0,0,1>,<0,0,0,1>);
llSetTouchText("");
llSetSitText("Move");
}

//touch to move the elevator
touch_start(integer num)
{
key avataronsittarget = llAvatarOnSitTarget();

if( avataronsittarget != NULL_KEY )
{
if ((llGetPermissions() & PERMISSION_TRIGGER_ANIMATION)
&& llGetPermissionsKey() == avataronsittarget)
{
llStopAnimation("sit");
movePlatform();
}
else
llRequestPermissions(avataronsittarget, PERMISSION_TRIGGER_ANIMATION);
}
} //end touch

changed(integer change)
{
if(change & CHANGED_LINK)
{
key avataronsittarget = llAvatarOnSitTarget();
if( avataronsittarget != NULL_KEY )
{
if ((llGetPermissions() & PERMISSION_TRIGGER_ANIMATION) && llGetPermissionsKey() == avataronsittarget) {
llStopAnimation("sit");
movePlatform();
} else {
llRequestPermissions(avataronsittarget, PERMISSION_TRIGGER_ANIMATION);
}
}
}
}

//request the permissions if need be
run_time_permissions(integer perm)
{
if(perm)
{
llStopAnimation("sit");
movePlatform();
}
} //end runtime permission
} //end default

SecondLife Tutorial: Walk Through Walls (Phantom Objects)

Leave a comment Standard

Sometimes you really need an object to appear solid but you need to be able to walk through it for one reason or another. The good news is this is really easy to do, just set the phantom status of the object.

//make the object so anyone can walk through it
state_entry()
{
     llSetStatus( STATUS_PHANTOM, TRUE);
}

Real World SecondLife Crimes: Virtual Rape & Pornography

Comments 3 Standard

I found a really interesting article in the Washington Post and I thought I’d share it. Apparently there have been an increasing number of crimes in second life. This article talks about a virtual “rape” that happened as well as child pornography and how local authorities are responding to it. The article debates on whether or not SL should be policed and if users should be subject to real law. How far is too far?

SecondLife Tutorial: Setting Linked Objects A Certain Color

Comment 1 Standard

For objects with multiple parts in SecondLife you need to use LlSetLinkColor. What this does is goes through the object and picks up anything its linked to. This way it will change the color of all other things its connected to. I also figured out how to change the alpha values of the objects so they appear and disappear when clicked.

if (llGetColor(ALL_SIDES) == white) //the block is already white
{
      llSetColor(black, ALL_SIDES);
      llSetLinkColor(LINK_ALL_CHILDREN, black, ALL_SIDES);//change transparency
      llSetAlpha(.5, ALL_SIDES);
      llSetLinkAlpha(LINK_ALL_CHILDREN, .5, ALL_SIDES);
}
else
{
      llSetColor(white, ALL_SIDES);
      llSetLinkColor(LINK_ALL_CHILDREN, white, ALL_SIDES);//change transparency
      llSetAlpha(1, ALL_SIDES);
      llSetLinkAlpha(LINK_ALL_CHILDREN, 1, ALL_SIDES);
}