Scripting Tutorial-08
Appearance
Scripting Lesson #8 - Held September 1st, 2018 at Sandbox Plaza by Fu Barr
<source lang="lsl"> /*
Fu's basic light switch
v0.2 - turn-off-the-lamp timeout - expanded setup
v0.1 - basic on/off toggle
- /
integer DEBUG = TRUE;
integer DIALOG_CHANNEL; integer dialog_listen_id;
float LAMP_TIMEOUT = 5.0; list lamps;
list colours_names = ["red", "blue", "green"]; list colours_rgb = [<1.0, 0, 0>, <0, 0, 1.0>, <0, 1.0, 0>]; float colours_glow = 0.20;
set_lamps(string colour_name) {
vector rgb; float glow;
if(colour_name == "none") {
rgb = <1,1,1>;
glow = 0.0;
} else {
integer id = llListFindList(colours_names, [colour_name]);
if(id > -1) {
rgb = llList2Vector(colours_rgb, id);
glow = colours_glow;
}
}
integer i = 0; integer end = llGetListLength(lamps);
while(i < end) {
llSetLinkPrimitiveParamsFast(llList2Integer(lamps, i),
// the UUID here is the OSgrid specific one for 'Blank Texture'
[ PRIM_FULLBRIGHT, ALL_SIDES, FALSE,
PRIM_TEXTURE, ALL_SIDES, "5748decc-f629-461c-9a36-a35a221fe21f", <1,1,0>, <0,0,0>, 0,
PRIM_COLOR, ALL_SIDES, rgb, 1.0,
PRIM_GLOW, ALL_SIDES, glow ]
);
i++; }
}
default {
state_entry() {
llOwnerSay("running: " + llGetScriptName());
// 0. setup lamps
integer i = 1; // linked prims are numbered from 1
integer end = llGetNumberOfPrims();
while(i <= end) {
if(llGetLinkName(i) == "lamp") {
lamps += i;
}
i++;
}
if(DEBUG){llOwnerSay(llDumpList2String(lamps, ","));}
// 1. generate random DIALOG_CHANNEL
DIALOG_CHANNEL = ((integer) llFrand(10000000)) * -1;
// 2. showtime!
state unlit;
}
}
state unlit {
state_entry() {
set_lamps("none");
}
touch_end(integer num_touches) {
if(llDetectedKey(0) == llGetOwner()) {
llDialog(llGetOwner(), "\npick a colour", colours_names, DIALOG_CHANNEL);
}
dialog_listen_id = llListen(DIALOG_CHANNEL, "", llGetOwner(), "");
}
listen(integer channel, string name, key uuid, string msg) {
set_lamps(msg);
state lit;
}
}
state lit {
state_entry() {
// 1. setup timer event
llSetTimerEvent(LAMP_TIMEOUT);
}
timer() {
set_lamps("none");
state unlit; }
} </source>
[11:34] Fu Barr: jimmy :)
[11:34] Fu Barr: ari :)
[11:34] Arielle Popstar: hey Jimmy, Hai Albertr
[11:34] Fu Barr: albertlr
[11:34] Arielle Popstar: just in time
[11:34] jimmy dayton: hi folks
[11:34] Fu Barr: yeah - was a bit of a rush on my part... sorry.
[11:35] albertlr Landar: Oh, just take your time Fu, we will catch up.
[11:35] Fu Barr: I was too busy stacking prim this morning and so I didn;t prepare... so I have to rush and missed the Q&A to finish :)
[11:35] Fu Barr: but it's all good - I got everything done in time :)
[11:36] Fu Barr: we'll wait another few minutes and then start
[11:36] Fu Barr: jimmy - is this your first session with us?
[11:38] Fu Barr: right - well I don't think anybody else will turn up, so let's get started.
[11:38] Arielle Popstar: albertr, can you announce the tut on the chat channel?
[11:38] Arielle Popstar: it wont initialize for me
[11:39] albertlr Landar: yea
[11:39] Arielle Popstar: is it a long weekend in the USA like here in canada?
[11:39] Mirror Amorasi is Offline
[11:39] Arielle Popstar: lots people off to their cottages
[11:41] Fu Barr: okay let's get going - the rest of the metaverse can catch up from the - wiki or forum :)
[11:41] Arielle Popstar: ok
[11:41] Fu Barr: please - take a copy of the archway, and rez it...
[11:42] Fu Barr: if you click it post-rez, you should get a dialog, which will ask you for a colour... and if you click on one the lights will switch on and in that colour
[11:42] Fu Barr: give it a go.
[11:43] Fu Barr: yay! it works :)
[11:43] albertlr Landar: We had Red White and Blue for a moment
[11:43] Fu Barr: now - this lesson is all about pull the stuff we learned in the previous lesson together
[11:43] Fu Barr: yes
[11:43] Fu Barr: remember the light script we've been working on has a timer!
[11:43] Fu Barr: so it goes off after the timer runs out :)
[11:44] Fu Barr: let's open up the script and see what's making this thing tick
[11:44] Fu Barr: are the perms ok?
[11:44] Fu Barr: ie. can you open the script?
[11:44] Arielle Popstar: working for me
[11:44] Fu Barr: nice.
[11:44] Fu Barr: always a slight moment of panic for me... I feel like the chemistry teacher... hoping his experiment wont 'blow up' :)
[11:45] Arielle Popstar: lol
[11:45] Fu Barr: okay - well - because of the rush to get the script done in time for class, I didn't add the v0.3 changes to the top of the script. bad fu.
[11:45] Fu Barr: you can add them in the comment or not.
[11:45] Arielle Popstar: so it fu barr?
[11:46] Fu Barr: it doesn't matter - the script works fine without :)
[11:46] Fu Barr: hehe - yeah it's a little fubarred :)
[11:46] Fu Barr: okay let's look at lines 20 22 ans 23
[11:46] Fu Barr: you can see we're defining a bunch of lists
[11:47] Fu Barr: the one in 20 has no initial values, the ones in 22 and 23 do.
[11:47] Fu Barr: like we did in v0.2 we're going to walk the entire prim-set to find the prims we need to light up.
[11:47] Fu Barr: but in this object we have more than one 'lamp' prim
[11:48] Fu Barr: so instead of one variable to hold the link id of the 'lamp' we need to be able to store more than one value
[11:48] Fu Barr: the easiest way to do that is to use a list
[11:48] Fu Barr: we saw how those little buggers work last week.
[11:49] Fu Barr: hey kawaii :)
[11:49] Kawaii Unicorn: hey :D
[11:49] Fu Barr: copy the arch and rezzit... give it a click or two and then open the script
[11:49] Fu Barr: we're only just starting to discuss it
[11:50] Fu Barr: so the first list we're using is 'lamps' and we don't really know what the id's of the prims are... until we loop through the link-set.
[11:50] Fu Barr: so we dont give it values.
[11:50] Fu Barr: the other two lists... well they are filled with date we know before hand.
[11:50] Fu Barr: colours
[11:50] Fu Barr: i've used the boringest colours in the universe but they serve the purpose of explaining stuff.
[11:50] Fu Barr: i;ve used 2 lists
[11:51] Fu Barr: one for the names
[11:51] Fu Barr: one for the rgb colour value vectors
[11:52] Fu Barr: the reason I didn;t merge the two lists into one - like we saw you can do in lesson 3... is that I wanted to keep this script really simple, and we'll see in a bit that iit'll be good to have the names for the colours seperate in a single list.
[11:52] Azi Az is Online
[11:52] Fu Barr: oh hey everyone - sorry didn't see you - you were hidden behind my script window :)
[11:53] Fu Barr: please copy the arch, rez it, play click on it to see what it does, and then opens the script - that's if you haven't done all of this already :)
[11:53] Fu Barr: ok - so we know understand the 3 lists in the script.
[11:53] paela argus is Online
[11:53] Fu Barr: and we've sort of recapped a bit about lists from last weeks lesson.
[11:53] Fu Barr: now to this weeks lesson - functions
[11:53] Fu Barr: on line 26
[11:54] Fu Barr: you see there's a block of code starting, that runs to line 64
[11:54] Fu Barr: notice that it's not inside a state
[11:54] Fu Barr: it's not in an event handler either
[11:54] Fu Barr: (obvious as event handler block have to be inside a state)
[11:54] Fu Barr: it's sitting outside the states blocks
[11:55] Fu Barr: after line 64... then the states start
[11:55] Fu Barr: so - this block of code lines 26-64 are labeled as 'set_lamps'
[11:56] Fu Barr: and set_lamps has a few brackets after the name...
[11:56] Fu Barr: (setting colour_name)
[11:56] Fu Barr: this means.... the function set_colours needs some information to work properly.
[11:56] Fu Barr: when you try and use this 'function' - you have to tell it which colour to set...
[11:57] Fu Barr: it's quite obvious really.
[11:57] Fu Barr: here's this stuff in a 'normal' conversation
[11:57] Fu Barr: hey script I want you to set the colours of my lamps
[11:57] Fu Barr: oh, right fu, what colour do you want me to set it to?
[11:57] Fu Barr: well, this time set it to red
[11:58] Fu Barr: but maybe next time set it to blue
[11:58] Fu Barr: fine, don't confuse me fu, what colour do you want me to set it NOW
[11:58] Fu Barr: oh - make it red then
[11:58] Fu Barr: cool. red lamps coming up
[11:58] Fu Barr: that's basically what set_colours(string colour_name) { block of code } does for me
[11:59] Fu Barr: so far so good?
[11:59] albertlr Landar: yep
[11:59] Arielle Popstar: kk
[11:59] Fu Barr: no comments from the dazed students.. good then i'll continue :)
[11:59] paela argus is Online
[12:00] Fu Barr: so now forget the block of code inside the function - lets just treat it like a 'black box' and lets go to the states first
[12:00] Fu Barr: I'm jumping to line 64... the end of the function block
[12:00] Fu Barr: on line 67 we have an old friend - the default state.
[12:00] Fu Barr: again - in this state nothing really happens we're just setting up the stage for the script to work.
[12:01] Fu Barr: but this time we need to set a bunch of stuff we haven't seen before...
[12:01] Arielle Popstar: why is this at the first?
[12:01] Fu Barr: in lines 73 - 85 we loop the link set to get all the id's of the prims names 'lamp'
[12:01] Arielle Popstar: isn't
[12:01] Fu Barr: ari - why isn't what at the first?
[12:02] albertlr Landar: the default state
[12:02] Arielle Popstar: the default state?
[12:02] Fu Barr: oh - it's simply a result of how the script engine works
[12:02] Fu Barr: if you would move the function around - then the script engine will complain when you try and use the function - because it reads the script top to bottom
[12:03] Fu Barr: the function has to be defined BEFORE you use it in code
[12:03] Fu Barr: so it's simple to just add any functions you write before you start setting up your states and event handlers
[12:04] Fu Barr: that way you are guaranteed that the script engine will have read your functions by the time you need to use it in some state deep down in your script
[12:04] Arielle Popstar: ok
[12:04] Fu Barr: does that explain it?
[12:04] albertlr Landar: yes
[12:04] Arielle Popstar: yes
[12:04] Fu Barr: so just to make it 100% vlear...
[12:04] Fu Barr: the order of 'stuff' in your script should be:
[12:05] Fu Barr: 1. some comments at the top to explain what your script does
[12:05] Fu Barr: 2. any variables or lists you need in your states further down
[12:05] Fu Barr: 3. any functions you write yourself that you need in the states further down
[12:06] Fu Barr: 4. the default state - and I would suggest you only use default for setting up your other states and script stuff.
[12:06] Fu Barr: 5. any other states that make the magic happen
[12:06] Fu Barr: that's really what most scripts should look like from a birds eye view
[12:06] Arielle Popstar: ok
[12:06] Arielle Popstar: that helps
[12:06] Fu Barr: comment, vars, lists, functions, default states, states
[12:07] Fu Barr: ok - back to the script :)
[12:07] Fu Barr: so in lines 73-85 we loop through all the prims in the link set looking for the ones called 'lamp'
[12:08] Fu Barr: if we find one, we stick its link number into a list... called lamps
[12:08] Fu Barr: after we run the prims.... we dump the prim id's we've found to the console... in line 87
[12:08] Fu Barr: we don't have to, but it makes more 'educational sense' to do so :)
[12:09] Fu Barr: so now we have the prims we're going to work with.
[12:09] Fu Barr: notice that with this system - as long as you name your prims in the linkset... you can link and relink and add prims etc. and the script wont break
[12:10] Fu Barr: the script doesn't care about the link id's - it just cares about the names you give the prims - if you do that, the script will always figure out which prims to use
[12:10] Fu Barr: so you can remove or add 'amp' prims to your hearts content
[12:10] Fu Barr: ok.
[12:10] Fu Barr: on line 90
[12:10] Fu Barr: we generate a random number
[12:11] Fu Barr: we need this number for our dialog to work - we'll see how/why in a few moents
[12:11] Fu Barr: but for now just see that we generate a LARGE number
[12:11] Fu Barr: and then multiply it with -1
[12:12] Fu Barr: to make the number NEGATIVE
[12:12] Fu Barr: again we'll see in a minute why that is.
[12:12] Fu Barr: and then in line 93 we get the show on the rod.
[12:12] Fu Barr: any questions so far?
[12:12] albertlr Landar: no
[12:12] Fu Barr: nope great
[12:12] Fu Barr: so on to the next state...
[12:12] Fu Barr: lit
[12:13] Fu Barr: on line 102 - the magic happens
[12:13] Fu Barr: this is the core of today's lesson
[12:13] Fu Barr: when I move into the the unlit state...
[12:13] Fu Barr: I simple call the function I defined before and ask the script to set the lamps to 'none'
[12:14] Fu Barr: at this point the script engine jump to line 26
[12:14] Fu Barr: and executes the code block that is written there.
[12:14] Fu Barr: then it comes back to line 103
[12:14] Fu Barr: once the code in lines 26-94 is run
[12:15] Fu Barr: on line 103 nothing much happens - it just closes the block of the state_entry event handler.
[12:15] Fu Barr: so this is the core of todays lesson
[12:15] Fu Barr: functions allow you to repeat code in many places in your script
[12:15] Fu Barr: it allows you to write shorter code.
[12:15] Fu Barr: and shorter code means less mistakes
[12:16] albertlr Landar: basically the functions allow a shorter version of the code, which can be used over and over in different places.
[12:16] Fu Barr: and also if your function is only written once but called from several places in the script... if you change the code in the function - it changes everywhere you 'call' it. so you only have to make changes to your code once and it will work the same everywhere you use it
[12:17] Fu Barr: yes -exactly albertlr
[12:18] Fu Barr: generally if you find that you're writing the same code over and over in your script - it's a sign you should be putting that code into a function
[12:18] Fu Barr: and the clever part of functions is that you can make them do various things because you can send them information
[12:18] albertlr Landar: and those are the purposes of the OSSL functions?
[12:18] Fu Barr: now we're just sending in the colour name
[12:18] Fu Barr: not really- this is just me explaining the general idea of 'functions'
[12:19] Fu Barr: the OSSL functions are things that the opensim peeps felt were missing in the original LSL language...
[12:19] Fu Barr: and some things that only make sense in opensim - like NPC's
[12:20] Fu Barr: but the general idea is - oh look I'm doing this stuff more than once in my code - cool, let's turn this into a function
[12:20] Fu Barr: so far so good? any questions?
[12:20] albertlr Landar: yea
[12:20] albertlr Landar: no questions
[12:20] Fu Barr: just to be 'complete' the info you send into a function are called 'parameters'
[12:21] Fu Barr: the whole line of function Name(parameter1, parameter2, parameter3) is called the function 'signature'
[12:21] Arielle Popstar: what activates the dialog box and sets the names of the buttons?
[12:21] Fu Barr: we're getting to that right now Ari :)
[12:21] Arielle Popstar: kk
[12:22] Fu Barr: now that we've explained all about functions - lets finish up and see how the rest of the script works..
[12:22] Fu Barr: ari - look at line 109
[12:22] Fu Barr: the LSL function we need for the dialog to pop-up is called 'llDialog'
[12:22] Arielle Popstar: ah kk
[12:22] Fu Barr: that function takes 4 parameters
[12:23] Fu Barr: llDialog(uuid_of_avatar, message to show on dialog, a list of buttons, the channel to return the choice of button to script on)
[12:23] Fu Barr: if you hover over the llDialog in the script editor - you'll get a short help note with the relevant parameters.
[12:24] Fu Barr: this works for ANY built-in LSL or OSSL function
[12:24] Fu Barr: obviously the editor know nothing about the functions your write yourself - so it doesn't do the pop-up help thing
[12:25] Fu Barr: now - here's the catch
[12:25] Fu Barr: there are MANY LSL and OSSL functions
[12:25] Fu Barr: and they all need a bunch of parameters to work properly
[12:25] Fu Barr: and that is why when you write scripts... you almost always have the LSL or OSSL wiki pages open in a web browser
[12:26] Fu Barr: you need to look up these parameters all the time
[12:26] Fu Barr: what they do, what you need to provide etc. etc.
[12:26] Fu Barr: at some point you'll remember the more frequently used ones - but you'll still be coding like you're watching a tennis match
[12:27] Fu Barr: look at your code, look at the wiki, look at your code, look at the wiki, look at your code, look at the wiki
[12:27] Fu Barr: till you make a mistake and the 'ball' goes out and you have to debug
[12:27] Fu Barr: round and round this dance goes till you complete the bug-free script
[12:28] Fu Barr: okay - lets wrap up the llDialog stuff and the rest of the script is trivial to read and understand in your own time
[12:28] Fu Barr: llDialog (if you read the wiki page) behaves like this
[12:28] Fu Barr: it shows the dialog, then waits for a button press
[12:28] Fu Barr: if you press any button except 'ignore'
[12:29] Fu Barr: the label of the button gets 'said' on the channel you tell it to chat... in our case we generated a channel number and stuck that number into DIALOG_CHANNEL :)
[12:29] Fu Barr: so when you press 'red'
[12:29] Fu Barr: the dialog says the word 'red' on channel -3235663246
[12:30] Fu Barr: depending on what the random number is that the script generated in the default state
[12:30] Fu Barr: now... look at 115
[12:30] Fu Barr: there we have the listen event handler
[12:30] Fu Barr: it sits and listens
[12:31] Fu Barr: and when something is heard it does the code in the code block... in our case it call the set_lamps function
[12:31] Fu Barr: and if you look carefully on line 117
[12:32] Fu Barr: the parameter it sends into the set_lamps function is.... the 'string' msg which is what was said on the channel... which happens to be the name if the colour...
[12:32] Fu Barr: and so on line 117 - the script hops to line 26
[12:32] Fu Barr: feeds 'red' into the function.. does the code there till line 96 and jumps back to 118
[12:33] Fu Barr: the listen event is setup on line 112 - and that's where you tell the script to listen on DIALOG_CHANNEL :)
[12:33] Fu Barr: anyhow - that's really the whole thing.
[12:33] Fu Barr: a few lists
[12:34] Fu Barr: one function that changes the colours and glow parameters of a few prims in a link-set
[12:34] Fu Barr: and a Dialog with a listen event.
[12:34] Fu Barr: dassit.
[12:34] Fu Barr: the rest is just a bit of boilerplate to grease the wheels.
[12:35] Arielle Popstar: kk cool
[12:35] Fu Barr: I can read more of the script - with you guys, but I figure it's best if you pull it apart yourself and try to modify it
[12:35] Fu Barr: breaking stuff is very good way to learn :)
[12:35] Arielle Popstar: :)
[12:36] albertlr Landar: as long as you keep the working one as back up
[12:36] Fu Barr: oh - there's one more 'trick' I use in the et_lamps function///
[12:36] Fu Barr: maybe I should say a few words..
[12:36] Fu Barr: if you look in lines 31 - 45
[12:36] Arielle Popstar: helps to be able to recognize what different parts of a script are
[12:37] Fu Barr: what I do there is setup the values for the rgb and the glow of the prim...
[12:37] Fu Barr: I collect those values first
[12:37] Fu Barr: then once I have the right values then I loop through the links in the lamps list.
[12:37] Fu Barr: the reason is that it makes the code much cleaner.
[12:38] Fu Barr: it's like cooking with a good menu in place.
[12:38] Fu Barr: anyhow - that's it. end of.
[12:38] albertlr Landar: ok
[12:38] Fu Barr: I hope these 4 sessinos have been useful
[12:38] Arielle Popstar: they have for me
[12:38] Fu Barr: you should be able to start writing and/or modifying scripts now with out much 'fear'
[12:39] albertlr Landar: well I am looking into how to best add line numbers to the scripts already in the wiki, as they are important for people to follow you.
[12:39] Fu Barr: and you'll also start to see that there's a LOT of very bad code out there.
[12:39] Fu Barr: so my last tip is... when you get a new script... look for the stuff that makes it tick... and then throw it away, and write your own version.
[12:39] albertlr Landar: I should have this script and the chat in the wiki by Friday at the latest.
[12:40] Fu Barr: and also - scripting is something that NEEDS lots and lots of practice.
[12:40] Fu Barr: that's it for the basics.
[12:40] Fu Barr: me done.
[12:40] Fu Barr: fin
[12:40] Fu Barr: end
[12:40] albertlr Landar: like writing a novel, if you don't get all the plot twists right it does not work out right.
[12:41] Arielle Popstar: its a wrap
[12:41] Fu Barr: that's all folks!
[12:41] albertlr Landar: Thanks Fu, you are appreciated.
[12:41] Fu Barr: dont forget to clear up your prim when you leav
[12:41] Fu Barr: e
[12:41] Fu Barr: hehe - the hidden avvys' have all left
[12:41] Fu Barr: I must have bored them to tears
[12:42] Fu Barr: well it's all on the wiki and forum now - I would have to teach this ever again.
[12:42] albertlr Landar: not at all they went to contemplate the wisdom you have shared oh sage.
[12:42] Fu Barr: lol - albertlr - i;m not so naive to think that. there's shopping to do and dances to be at with bouncy boobies LOL!
[12:43] albertlr Landar: ok
[12:43] Cataplexia Numbers is Offline
[12:43] Fu Barr: anyhow - I did my bit for King and Country
[12:43] albertlr Landar: well thanks again for all this, it does give us more to sure in the wiki.
[12:43] Arielle Popstar: lots of people as a holiday weekend and last before school starts
[12:44] Fu Barr: maybe next time we'll do prim building or something like that.
[12:44] Arielle Popstar: yes, thank you Fu
[12:44] Arielle Popstar: good idea
[12:44] albertlr Landar: and that would be a big help to lots of people too.
[12:44] Fu Barr: thanks ari for putting this stuff up on the forum - and albertlr - thanks too
[12:44] Arielle Popstar: prims becoming a lost art with all the focus on mesh
[12:45] albertlr Landar: I prefer prims myself.
[12:45] Fu Barr: yeah - I think building is becoming less of the 'thing' in the virtual
[12:45] Fu Barr: its about finding the mesh you like and rezzing it
[12:45] Arielle Popstar: yes
[12:45] albertlr Landar: when it does rez
[12:45] Fu Barr: I be surprisedto find more than a handful of people who actually build stuff anymore
[12:46] Arielle Popstar: and then be frustrated when you cant modify it
[12:46] Cataplexia Numbers is Online
[12:46] Fu Barr: in-world I mean
[12:46] albertlr Landar: Bye all.
[12:46] Arielle Popstar: tc albertr
[12:46] Fu Barr: ciao