Difference between revisions of "A Basic LSL Tutorial"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with 'Basic Tutorial on LSL. By James Benedek --------------------------------------------------------------------------------------------------- ----------------------------------...')
 
Line 5: Line 5:


When you create a new script, it will always start out like this:
When you create a new script, it will always start out like this:
 
<lsl>
default
default
{
{
Line 18: Line 18:
     }
     }
}
}
</lsl>


This script will basically say "Hello, Avatar!" on the public channel which is 0,  
This script will basically say "Hello, Avatar!" on the public channel which is 0,  
and will then say "Touched." on the public channel when a user touches or clicks it.
and will then say "Touched." on the public channel when a user touches or clicks it.


Errors and how to fix them?
'''Errors and how to fix them?'''
---------------------------
---------------------------
If you get an error while scripting at anytime, it will ususually be that you have missed
If you get an error while scripting at anytime, it will ususually be that you have missed
Line 28: Line 29:
common error (a synthax error). Sometimes this may not be the case though.
common error (a synthax error). Sometimes this may not be the case though.


Basic understanding of terms:
'''Basic understanding of terms''':
-----------------------------
-----------------------------
A function shown in red and does  
A function shown in red and does  
Line 49: Line 50:




TASK 1:
'''TASK 1''':
-------
-------
Lets begin by doing a easy task first off, lets make the box say something else
Lets begin by doing a easy task first off, lets make the box say something else
Line 63: Line 64:




TASK 2:
'''TASK 2''':
-------
-------
Lets just add some information after this explaining what it each thing does,  
Lets just add some information after this explaining what it each thing does,  
Line 69: Line 70:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 78: Line 80:
     touch_start(integer total_number)
     touch_start(integer total_number)
     {
     {
         llSay(0, "Touched."); // says touched when somone touches it
         llSay(0, "Touched."); // says touched when someone touches it
     }
     }
}
}
</lsl>


TASK 3:
'''TASK 3''':
-------
-------
Lets now try make it change its color when we click it, the function we will need is
Lets now try make it change its color when we click it, the function we will need is
Line 95: Line 98:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 107: Line 111:
     }
     }
}
}
</lsl>




 
'''TASK 4''':
TASK 4:
-------
-------
Lets now make it have text appear over it, you can do this by using the function
Lets now make it have text appear over it, you can do this by using the function
Line 120: Line 124:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 132: Line 137:
     }
     }
}
}
</lsl>


 
'''TASK 5''':
TASK 5:
-------
-------
Now that you understand alpha we shall make our object dissapear!
Now that you understand alpha we shall make our object dissapear!
Line 143: Line 148:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 155: Line 161:
     }
     }
}
}
</lsl>




Task 6:
'''Task 6''':
-------
-------
Lets try make our object texture itself, to do this we will use the function llSetTexture, its
Lets try make our object texture itself, to do this we will use the function llSetTexture, its
Line 170: Line 177:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 182: Line 190:
     }
     }
}
}
</lsl>


Task 7:
'''Task 7''':
-------
-------
Now lets move onto something more tricky, as you understand a bit about keys we will make out object
Now lets move onto something more tricky, as you understand a bit about keys we will make out object
Line 191: Line 200:
clicking/touching your object. So firstly lay it out your script like this:
clicking/touching your object. So firstly lay it out your script like this:


 
<lsl>
default
default
{
{
Line 208: Line 217:
     }
     }
}
}
</lsl>


If you want it to do something when someone else other than the owner touches it, use a else statement.
If you want it to do something when someone else other than the owner touches it, use a else statement.
You can do this by, laying out your script like this:
You can do this by, laying out your script like this:


<lsl>
default
default
{
{
Line 231: Line 242:
     }
     }
}
}
</lsl>


 
'''Task 8''':
Task 8:
-------
-------
Now lets try make a object do something when you say something, to do this
Now lets try make a object do something when you say something, to do this
Line 247: Line 258:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 260: Line 272:
     }
     }
}
}
</lsl>


To make it only listen for commands not on the public channel, which will be more secret and  
To make it only listen for commands not on the public channel, which will be more secret and  
Line 266: Line 279:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 279: Line 293:
     }
     }
}
}
</lsl>


To chat in another channel, just type in the chat bar /channel before you type something,
To chat in another channel, just type in the chat bar /channel before you type something,
Line 286: Line 301:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 305: Line 321:
     }
     }
}
}
</lsl>


To learn more about llListen take a look at:
To learn more about llListen take a look at:
Line 310: Line 327:
http://www.lslwiki.net/lslwiki/wakka.php?wakka=llListen
http://www.lslwiki.net/lslwiki/wakka.php?wakka=llListen


TASK 9:
'''TASK 9''':
-------
-------
Now lets experiment with vectors, and make your object move non physically,
Now lets experiment with vectors, and make your object move non physically,
Line 318: Line 335:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 330: Line 348:
     }
     }
}
}
</lsl>


A position vector layout is in the format x,y,z, so <0,0,1> will make it go up 1m.
A position vector layout is in the format x,y,z, so <0,0,1> will make it go up 1m.
Line 336: Line 355:
http://www.lslwiki.net/lslwiki/wakka.php?wakka=llSetPos
http://www.lslwiki.net/lslwiki/wakka.php?wakka=llSetPos


TASK 10:
'''TASK 10''':
--------
--------
Like when editing a object you can make it Phantom, Physical...
Like when editing a object you can make it Phantom, Physical...
Line 345: Line 364:
Your script should look something like this:
Your script should look something like this:


<lsl>
default
default
{
{
Line 352: Line 372:
     }
     }
}
}
</lsl>


---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Line 359: Line 380:
To know more functions and what they do, take a look at these links below:
To know more functions and what they do, take a look at these links below:


http://wiki.secondlife.com/wiki/LSL_Portal
[http://wiki.secondlife.com/wiki/LSL_Portal]


http://www.lslwiki.net/lslwiki/wakka.php?wakka=HomePage
[http://www.lslwiki.net/lslwiki/wakka.php?wakka=HomePage]


http://rpgstats.com/wiki/index.php?title=Main_Page
[http://rpgstats.com/wiki/index.php?title=Main_Page]


---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------

Revision as of 14:02, 6 July 2009

Basic Tutorial on LSL. By James Benedek



When you create a new script, it will always start out like this: <lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
       llSay(0, "Touched.");
   }

} </lsl>

This script will basically say "Hello, Avatar!" on the public channel which is 0, and will then say "Touched." on the public channel when a user touches or clicks it.

Errors and how to fix them?


If you get an error while scripting at anytime, it will ususually be that you have missed out a ; at the end of a function or a { or a } after or before an event. This is the most common error (a synthax error). Sometimes this may not be the case though.

Basic understanding of terms:


A function shown in red and does

A event is shown in light blue

A string is charachters and should be in "", like "hello bill!".

A integer is a whole number like 1

A vector is numbers placed out like so <0,0,0>, it can represent colour, position.

A float is a decimal number like 1.0, usually used in alpha and calculation.

A key is a random generated UUID on the linden labs database mine is d77442ea-5acc-4ed3-bbb4-1f2bf2c31bb6.

TRUE and FALSE, FALSE is equal to the integer 0, and TRUE is equal to the integer 1.


TASK 1:


Lets begin by doing a easy task first off, lets make the box say something else when you touch/click it, you can do this by editing the "llSay(0, "Touched.");". Just edit inbetween the "". llSay's layout is like this llSay(Channel, "SAY STUFF");

llSay is not the only function where the object can communicate you can also try out:

llShout(Channel, "SHOUT STUFF"); llWhisper(Channel, "WHISPER STUFF"); llOwnerSay("SAY STUFF TO YOU ONLY"); llRegionSay(Channel, "REGION SAY STUFF");


TASK 2:


Lets just add some information after this explaining what it each thing does, you can do this by adding // and anything after it will appear orange, this is a comment. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!"); // says hello at start
   }
   touch_start(integer total_number)
   {
       llSay(0, "Touched."); // says touched when someone touches it
   }

} </lsl>

TASK 3:


Lets now try make it change its color when we click it, the function we will need is llSetColor, its layout is like this llSetColor(vector color, integer face); this basically means if i wanted the cube to be red i would use the vector <1,0,0>, green <0,1,0>, blue <0,0,1>. For a full color chart see the link below:

http://www.lslwiki.net/lslwiki/wakka.php?wakka=color

If it was a linked object you could use llSetLinkColor, which its layout is llSetLinkColor(integer linknumber, vector color, integer face). Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   llSetColor(<1,0,0>, ALL_SIDES);
   }

} </lsl>


TASK 4:


Lets now make it have text appear over it, you can do this by using the function llSetText, its layout is llSetText(string text, vector color, float alpha). Basically if you do this llSetText("HELLO CAN YOU READ THIS",<1,0,0>,1); will appear as HELLO CAN YOU READ THIS in the colour red. Alpha is the transparency, if you did llSetText("HELLO CAN YOU READ THIS",<1,0,0>,0); you wouldnt be able to see it. 0 = high transparency, 0.5 = in the middle, 1 = no transparency. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   llSetText("HELLO CAN YOU READ THIS",<1,0,0>,1);    
   }

} </lsl>

TASK 5:


Now that you understand alpha we shall make our object dissapear! To do this we will use the function llSetAlpha, its layout is llSetAlpha(float alpha, integer face). llSetAlpha(0, ALL_SIDES); would make all the faces of the object transparent. If it was a linked object you could use llSetLinkAlpha, which its layout is llSetLinkAlpha(integer linknumber, float alpha, integer face). Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   llSetAlpha(0, ALL_SIDES);     
   }

} </lsl>


Task 6:


Lets try make our object texture itself, to do this we will use the function llSetTexture, its layout is llSetTexture(string texture, integer face).You can do this two ways, you can place a texture inside the object or use a UUID. To make it change to a texture you have placed inside the object, do it like this llSetTexture("NAME OF TEXTURE", ALL_SIDES); If you want to use a UUID, find a texture in your inventory, right click it > copy asset UUID, then paste the UUID in the "" so it looks like this llSetTexture("j4m3s000-0000-0000-0000-b3n3d3k00000", ALL_SIDES); If it was a linked object you could use llSetLinkTexture, which its layout is llSetLinkTexture(integer linknumber, string texture, integer face); Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   llSetTexture("NAME OF TEXTURE", ALL_SIDES);    
   }

} </lsl>

Task 7:


Now lets move onto something more tricky, as you understand a bit about keys we will make out object only respond when the owner clicks the object, this is called a if statement. To do this we will need to use the functions llGetOwner() and llDetectedKey(0);, llGetOwner will return your key UUID and llDetectedKey(0) when put under the touch event will return the key of who is clicking/touching your object. So firstly lay it out your script like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   if (llDetectedKey(0) == llGetOwner())
   {
       llSay(0, "Owner Touched.");
   }
   
   }

} </lsl>

If you want it to do something when someone else other than the owner touches it, use a else statement. You can do this by, laying out your script like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   if (llDetectedKey(0) == llGetOwner())
   {
       llSay(0, "Owner Touched.");
   }
   else
   {
   llSay(0, "Someone Else Touched.");
   }
   }

} </lsl>

Task 8:


Now lets try make a object do something when you say something, to do this we will have to use a llListen function, its layout is this llListen(integer channel, string name, key id, string msg); We will fistly place this under the state_entry() event. In this task we will just make it use the public channel, which is 0, and we will make it listen to only you, which requires the llGetOwner() function we used previously. We will then need to use a listen event, its layout is like this listen(integer channel, string name, key id, string message) {, replace the touch event with this. we will then make the object annoy you by it saying "Really?" whenever you type something XD This will involve using a llSay like we used in task 1. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llListen(0,"",llGetOwner(),"");
   }
   
   listen(integer channel, string name, key id, string message)
   {
       llSay(0,"Really?");
   }

} </lsl>

To make it only listen for commands not on the public channel, which will be more secret and which is less laggier in complex scripts, you can do this by changing the integer 0 to something like 99, this time use a llOwnerSay instead of a llSay, which should make it secret. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llListen(99,"",llGetOwner(),"");
   }
   
   listen(integer channel, string name, key id, string message)
   {
       llOwnerSay("Really?");
   }

} </lsl>

To chat in another channel, just type in the chat bar /channel before you type something, in this case it will be "/99 hello" for example.To make it do only certain stuff when you say certain commands we will have to use a if statement, like before but this time we will have to check if the message equals the command, if (message == "Your Command Here"). Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llListen(99,"",llGetOwner(),"");
   }
   
   listen(integer channel, string name, key id, string message)
   {
   if (message == "hello")
   {
       llOwnerSay("Hello to you too!");
   }
   else
   {
       llOwnerSay("INVALID COMMAND");
   }
   }

} </lsl>

To learn more about llListen take a look at:

http://www.lslwiki.net/lslwiki/wakka.php?wakka=llListen

TASK 9:


Now lets experiment with vectors, and make your object move non physically, to do this we will need to use the function llSetPos, its layout is llSetPos(vector pos); We will also use llGetPos() so we can make it move +1 of its current position. Place the llSetPos function under a touch event with a vector <0,0,1> + llGetPos();. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
   touch_start(integer total_number)
   {
   llSetPos(llGetPos() + <0,0,1>);
   }

} </lsl>

A position vector layout is in the format x,y,z, so <0,0,1> will make it go up 1m. to learn more take a look at:

http://www.lslwiki.net/lslwiki/wakka.php?wakka=llSetPos

TASK 10:


Like when editing a object you can make it Phantom, Physical... You can also do this with scripting, the function you have to use to do this is llSetStatus, its layout is llSetStatus(integer status, integer value);, so if i wanted to make my object go physical the integer status would be STATUS_PHYSICS or 1, if i wanted to make my object go phantom i would use STATUS_PHANTOM or 16. Your script should look something like this:

<lsl> default {

   state_entry()
   {
       llSetStatus(STATUS_PHYSICS, TRUE);
   }

} </lsl>


You now know pretty much the basics of scripting, but there are still many more functions to use and experiment with yet!!!!!! To know more functions and what they do, take a look at these links below:

[1]

[2]

[3]