Introduction
In last week's post I talked about creating objects in SL and tried to create some complicated ones. I did not however try to create objects requested for my degree, instead I took the opportunity to take a look into second life and programming in SL. Well from this week on I will try to create several objects starting with the very basic objects such as a chair. This post will cover:
- Building a chair on which the avatar can sit properly
- Building an object that responds to commands
- Building an object that can send emails
Building a chair
Building a chair in SL is not very hard but aligning objects is. To create the chair I decided to create a cubic prim to create a chair leg. Then I extended the prim to a thin and long cuboid form to resemble a chair leg. I made four copies of the prim and tried to align them. Aligning in SL is difficult and requires some patience. There are external tools that can be used to create and align objects in SL but I chose to stick to the standard SL viewer tools.
The picture below shows a chair leg.
This picture shows four chair legs being aligned.
Creating the bottom of the chair is easier. Only one flat prim is required and is placed on the aligned legs. The back of the chair is made up of another flat prim placed vertically on the chair base. All object are then linked together. Finally I chained the chair colour to brown.
The picture below shows the last version of the chair.
The chair has also some scripts to allow the avatar to sit properly on it. To create the script I used the select face feature that allow the user to select a face on a group of linked prims. On this face I created the script below.
default
{
state_entry()
{
llSitTarget(<0.0, 0.0, 0.1>, ZERO_ROTATION);
}
}
The llSitTarget() function requires a vector where the avatar will sit and the rotation of the avatar. With the given parameters the avatar sits correctly i the middle of the chair. The picture below shows the avatar sitting on a chair.
Building an object that reacts to commands
In last weeks post I created a car that moves forward and backward when the avatar touches it. This week I decided to make something easier but something that works as it should. For this purpose I decided to create a light bulb that when touched lights on and off.
The bulb is made of a cylinder to recreate the bulb base and a sphere to form the bulb top. Both prims are linked together. The prims have a plain texture different from that of the chair. The sphere part has a 36% transparency to make the bulb look more real. Both objects have scripts to light the top part of the bulb.
The script for the sphere part:
default
{
state_entry()
{
llListen(989,"Cylinder",NULL_KEY,"On");
llSetColor(<0.5,0.5,0.5>, ALL_SIDES);
}
listen( integer channel, string name, key id, string message )
{
state On;
}
touch_start(integer total_number)
{
state On;
}
}
state On
{
state_entry()
{
llListen(989,"Cylinder",NULL_KEY,"On");
llSetColor(<1.0,1.0,0.0>, ALL_SIDES);
}
listen( integer channel, string name, key id, string message )
{
state default;
}
touch_start(integer total_number)
{
state default;
}
}
The Prim has two states, the default state and the on state. Both states have similar code. On state entry in the default state, the script starts listening on channel 989 for an object called Cylinder to say the message On. In case this occurs the listen method is invoked. This method sets the prim's state to On. The function llSetColor(<0.5,0.5,0.5>, ALL_SIDES); sets the prims colour. It takes two parameter, the first is a vector that represent the colour in RGB form, and the second parameter is the side number or all sides. The vector used for colour does not take the RGB colours are the normal integers we are normally used to, but uses a sum to render the integer to floats less than 1.0. To change an RGB colour to this type, you need to divide it by 255. The vector <0.5,0.5,0.5> is the LSL colour equivalent to (128,128,128) in the normal RGB format where the 0.5 is rounded from 0.502. The state has another event called touch start that changes the state to On.
The State On is similar to the default state, but instead changes the colour to yellow rather than grey. this state also listens for messages thrown by the Cylinder Object. When the listen event is invoked, the default state is triggered and the bulb is switch off and set back to its greyish colour. Touching the bulb in the On state also switches it off (by setting the default state).
The code for the Cylindrical part is simple. Up on touch the object throws the llSay method to send the message On. The message is sent on the channel 989, which is the same channel the cylindrical object is listening on. This way a message is sent between the two prims and the bulb is switched on and off.
default
{
state_entry()
{
}
touch_start(integer total_number)
{
llSay(989,"On");
}
}
The default bulb state. The bulb is off.
The default bulb state. The bulb is off.
Building an object that can send emails
For this exercise I decided to create a flat prim that listens to what avatars say. Depending on what they say a mail can be sent. The code below shows the script for the prim.
default
{
state_entry()
{
llListen(0,"",NULL_KEY,"");
}
listen( integer channel, string name, key id, string message )
{
string messagetype = llGetSubString(message,0,4);
string messagestring = llGetSubString(message,5,-1);
if (messagetype == "mail:")
{
llEmail( "mail@hotmail.com", "Message From SL", messagestring);
}
}
}
This prim has only one state. On state entry the prim starts listening for what avatars say. When an avatar says something the message starts listening. When the message starts with "mail:", the rest of the message is sent by email. In the listen event, the function llGetSubString(message,0,4) gets the first four characters of the messages the avatars say. This string is put in the variable messagetype. The rest of the string is put in the variable called messagestring. If the message type is equal to "mail:" the message string is sent by mail. E-mails can be sent using the llEmail() function. This function takes mail address, subject and message as parameters.
Conclusion
Scripting in LSL is not easy, especially when dealing with rotation and vectors. Fortunately a lot of examples can be found on the Second Life Wiki. A lot of material is available online and examples can be found for practically everything. I find LSL to be most interesting part of SL. This week I focused more on the listen function as it is very interesting how objects can interact with other objects and with avatars.
In general Building object in SL takes time. Aligning and resizing objects is not easy as it should be. Building the object visuals takes a lot of time but with the help of external tools, these procedures can be made easier. With the addition of scripts SL become a powerful social tool where an avatar interacts with a new world.
No comments:
Post a Comment