Introduction
This week's post will be about using Second Life and the SL Scripting Language. I will talk about how all students in my class met in a virtual world and tried to make a virtual lesson while sitting next to each other in class. I will also talk about some very basic scripting done during the lesson and afterwards.
Moving Around
It was fun meeting in a virtual world going around using avatars and chatting with friends. It is a good experiment that lets you learn the game more quickly as people in the class room share their experience with each other. During this session we started the first lessons about scripting in Second Life. The first thing to do is to go in a place where the avatar is allowed to build and edit objects. The whole class teleported to a sandbox that allows users to create objects called prims.
In Second Life all objects are prims. Prim is short for primitive and refers the 3D shapes that users uses to build objects. There are several default prims available when building an object from scratch. The image below was taken from the Second Life Wikia and shows the basic Prim shapes
Box | Prism | Sphere | Cylinder |
---|---|---|---|
Torus | Tube | Ring | |
Prims can be Linked to each other to form bigger objects and can be stored in the inventory under the objects folder. All students tried to create their own object in second life. My choice was not exactly a good one for a beginner. In fact I immediately wanted to create a car, something which I still haven't managed to do, mostly because of the time it requires. During the lesson I created several objects and tried some basic scripting. Objects can be worn as clothes or gadgets or be fixed in a place, such as chairs.
Scripting in LSL
The first thing to understand in LSL is how it works. LSL is an event driven language. Events are predefined in the game. A list of valid events can be found on http://wiki.secondlife.com/wiki/Category:LSL_Events. Objects also have states. All Objects have a default State, but other states can be created. An object may react differently to the same events, depending on the state the object is in. Objects can go from state to state simply by using the command 'state nameOfState;'. A commonly used event is state_entry. This event is triggered immediately when a state is changed and commands in this event are executed on state entry.
The first steps in trying to make a car was to create the tires. I created four cylindrical prims and gave them a dark black colour. For the body of the car I simply made a cuboid and placed it on the four tyres. I Linked the five objects together and set the object as physical. Like this I obtained a car look - a-like body. Since I did not have a lot of time, I did not try to improve the car looks, because I preferred trying some scripting.
To create a new script I selected the object and clicked on the content tab and then on the new script button. The New script is not blank, but contains a default state with two events and methods. The first event is the state_entry() event which I already mentioned, while the other event is touch_start() which is triggered when an object is touched by an avatar. The events contained the method llSay() which launches a chat message to all users within 20 meters. Other similar functions like llShout() and llWisper() do the same job but have different ranges.
What makes a car special, is the ability to move. I decided to experiment with the method llMoveToTarget() to make the car move. Using the function llGetPos(), I got the current vector position of the object and stored it into a vector called pos.
vector pos = llGetPos();
The function llMoveToTarget() requires the new vector position ad the time to travel to that position. I used this function to make the car move forward.
llMoveToTarget(pos + <10.0,10.0,0.0>,10.0);
Well using this function, the car does not always move to the desired destination. The image below shows the car going in the air in an uncontrolled manner.
In my experiment, the car should have moved forward for ten seconds. That works on solid flat land, but when the land below is not flat, the car's movements become unexpected. While experimenting, I also tried different states. I created the state moving that is triggered when the default state is touched. In this State, if the user touches the car, it moves in reverse mode. The code is similar to moving forward.
llMoveToTarget(pos + <-10.0,-10.0,0.0>,10.0);
The object is then returned to the original state. Currently, the only way to travel using this car is to sit on it and then touch it. Hopefully I will be able to do much better in future.
The Entire Script code for the very primitive car can be found below:
- default
- {
- state_entry()
- {
- llSay(0, "Hi, there!");
- }
- touch_start(integer total_number)
- {
- llSay(0, "You may sit here.");
- vector pos = llGetPos();
- llMoveToTarget(pos + <10.0,10.0,00.0>,10.0);
- state moving;
- }
- }
- state moving
- {
- state_entry()
- {
- llSay(0, "Stop Me Please");
- }
- touch_start(integer total_number)
- {
- vector pos = llGetPos();
- llMoveToTarget(pos + <-10.0,-10.0,0.0>,10.0);
- state default;
- }
- }
Conclusion
I must say that although I was not that interested at first, using Second Life in class was fun. Seeing and chatting with people over second life makes you realise how SL can be a powerful socialising tool. There also seems to be a lot of users who spend their time creating interesting and complicated objects that make the experience even better. Apart from the coding side which is the part that I'm mostly interested in, SL proved to be move interesting than I thought. After a similar experience, one must always take a class photo.
No comments:
Post a Comment