more commands, parameters, and tests
We looked at the script you get when you click New Script... in an object. It was this one:
default { state_entry() { llSay(0, "Hello, Avatar!"); } touch_start(integer total_number) { llSay(0, "Touched."); } } You could also use the commands llOwnerSay or llWhisper instead of llSay. So it is no surprise that you could put in new lines like this.
default { state_entry() { llSay(0, "Hello, Avatar!"); llOwnerSay("I do exactly what my script says to do"); } touch_start(integer total_number) { llWhisper(0, "Touched."); } } Try this out to see what happens.
llOwnerSay is like llSay except only the owner of the object running the script gets the message. And llWhisper is like llSay except that the message is whispered. Another similar command is llInstantMessage. But llSay will work for everything we will be doing here.You can look up the basic commands in the reference manual here or here. (The commands get updated periodically, so for some commands you may need to search other Second Life discussions.) We will gradually get to know some of the most useful commands.
We have been indenting our lines are in a certain way that you will have noticed, and we have been writing just one { or } per line. This is to make the code easier for us humans to read. The computer does not need the indenting.
It would be more friendly to say hello to whoever touches you, which you can do like this:
default { state_entry() { llSay(0, "I exist!"); } touch_start(integer detected) { llSay(0,"Hello "+llDetectedName(0)); } } Instead of putting two numbers together, the + here puts two strings together, the string "Hello " and the string which is the name of the avatar that caused the touching event, using the new command llDetectedName. Usually only one object will be touching you, and that object will be number 0. If more objects are touching you (so that detected>1), then you might want to see all their names -- something we will learn how to do soon.
Sometimes you want to do one thing or another depending on the situation. LSL provides a simple way to check on something, with a if(test){do this}else{do that} statement.
default { state_entry() { llSay(0, "Does 1+2==3?"); if (1+2==3) { llSay(0, "yes"); } else { llSay(0, "no"); } } } Notice that the test 1+2==3 is what determines what this script will say. Try changing the 3 to a 4 and see what happens.
Notice that "equals" is written with two symbols! If you just write one equal sign, it means "make equal to". That's something different, which we will see later!
Programs with if-then-else statements can get complicated, so it is sometimes useful to use a flowchart to get a picture of them.
Getting scolded by parental units can sometimes be embarassing but in SL we can listen to them on a different channel. How cool! The public channel is channel 0, but there are many other channels. So let's put a mother cube on channel 1. To say "yes" to your mother on channel 1 you type a return to open your IM window (unless it's already open) and then type:
/1 yes
To say "no" to your mom on channel 1 you type:
/1 no
Create a new cube and call it "mother". Then open the Contents and click on "New script". Then replace the "Hello Avatar" script with this one, using copy-and-paste:
default { state_entry() { llOwnerSay("Your mother is here! Are you paying attention?"); llListen(1,"","",""); } listen(integer channel, string name, key id, string message) { if(message == "yes") { llOwnerSay("You are so nice."); llOwnerSay("You can have a cookie."); } else { llOwnerSay("You're not nice!"); llOwnerSay("Go to your room!"); } } } I think you can understand this one! The only new command is llListen. The llListen command is a little bit tricky, but here we use it in the simplest way: the first parameter says to listen on channel 1, and for all the other 3 parameters we just put in the empty string "". (We will use those other 3 parameters later.)
Besides the new command though, we see that objects notice when things are said. When something is said near your object, it triggers a listen event, so that the mother cube can check the message. That listen event on line 9 comes with 4 parameters too -- and we might talk about them later -- but for now the script only pays attention to the last parameter, the message.
What happens if instead of saying "yes" or "no" you say something rude like
/1 fiddlesticks!
What happens if you type
/1 yes
more than once?What does the mother cube do if you have a friend there who asks you something while she is waiting for an answer from you?
Some people are never embarassed to talk to their mother unit anywhere. See if you can change the mother script so that she talks on the public channel, using llSay(0,"...message...") instead of llOwnerSay, and using llListen(0,"","","") instead of llListen(1,"","",""). Then when you're typing you don't need to put the slash and the channel. You can just type
yes
or (if you're rude!) you can say
no
See if you can make this work!
If you could do that last one, see if you can do this! Create a cube and call it "joker". Then put a script in it to tell a joke, like this:
your joker cube types: Knock knock!
then you type: who's there?
your joker cube types: Tank!
then you type: Tank who?
your joker cube types: Tank you very much!
If you can make this work, tell Tiplife you deserve a prize!(And then maybe you can find a better joke to put in there!)
Notice how fussy these programs are about what you type: every detail matters!