Sunday, 5 June 2011

Week 15 - Linden Scripting Language 2

This week, more Linden Scripting Language have been done during the lecture, where the different types of variables have been explained and more examples on what scripting can be done in second life was given.

Variables

Linden Scripting language is a strongly typed language; variables must be declared by type. The following are the types if variables used in LSL:
  • Integer - is a whole number
  • Float - is a decimal number
  • String - is a sequence of characters
  • Key - is used to identifysomething in second life
  • Vector - are three floats that are in the form <x, y, z>
  • Rotation - is made up of four floats <x, y, z, s>
  • List - a list of other data types that are heterogeneous

Counter

A simple task I did to experiment with variables is creating a counter, where variable cnt is being used:
1:  integer cnt;  
2:  default  
3:  {  
4:    state_entry()  
5:    {  
6:      cnt = 0;  
7:    }  
8:    touch_start(integer total_number)  
9:    {  
10:      cnt ++;  
11:      llSay(0, "Count: " + (string)cnt);      
12:    }  
13:  }  
In the above code, the initial value of the counter is set to zero as shown in line 6. In the state_entry() function, the variable cnt is set to zero and each time the object will be touched, the touch_start() function will be called. The variable will then be inecremented and displayed. In order to display the variable, type casting is used to convert to string. Below shows the result when the object will be touched.

Sitting

I also built a static object which is a chair that allow the avatar to sit properly. That is, instead of 'right click' and and select the 'Sit Here' command, a script have been created. Three variables of a float type as shown in lines 1-3 have been declared, where together they make up a vector. The llSitTarget() function is used to make the avatar sit down, where it consists of a vector offset and rotation (line 8). The llSetSitText() function is used to display an alternative text insted of the default 'Sit Here', when you right click. At first the avatar was not sitting properly one the chair, but after a while playing around, I managed to make the avatar sit properly. One thing I had to change is the values of the x, y and z. When the value of x is increased or decreased, the avatar sits on the object forward or backwards. When the value of y is increased or decreased, the further left or right the avatar sits on the object. When the value of z is increased or decreased, the avatar sits higher or lower on the object. Another thing I had to do was to rotate the avatar to 90 °, where I had to use the llEuler2Rot() function as shown in line 8.
1:  float x = 0;                 
2:  float y = 0.3;   
3:  float z = 0.3;  
4:  default  
5:  {  
6:    state_entry()  
7:    {  
8:      llSitTarget(<x,y,z>,llEuler2Rot(<0,0,90> * DEG_TO_RAD));  
9:      llSetSitText("Sit Av");  
10:    }  
11:  }  
Avatar Sitting

No comments:

Post a Comment