Scripting Tutorial-06

From Osgrid Wiki
Jump to: navigation, search

Scripting Lesson #6 - Held August 19th, 2018 at Sandbox Plaza by Fu Barr

    Fu's basic light switch
    - turn-off-the-lamp timeout
    - expanded setup
    - basic on/off toggle
integer DEBUG = TRUE;
integer LAMP_LINK_ID;
float   LAMP_TIMEOUT = 5.0;
default {
    state_entry() {
        llOwnerSay("running: " + llGetScriptName());
        if(DEBUG){llOwnerSay("state: default");}
        // 0. setup lamp
        integer i   = 1;    // linked prims are numbered from 1
        integer end = llGetNumberOfPrims();
        if(DEBUG){llOwnerSay("Number of prims: " + (string) end);}
        while(i <= end) {
            if(DEBUG){llOwnerSay("Link ID: " + (string) i + ", prim name: " + llGetLinkName(i));}
            if(llGetLinkName(i) == "bulb") {
                LAMP_LINK_ID = i;
        // 1. showtime!
        state unlit;
state unlit {
    state_entry() {
        if(DEBUG) {
            llOwnerSay("state: unlit");
            llOwnerSay("LAMP_LINK_ID: " + (string) LAMP_LINK_ID);
            // the UUID here is the OSgrid specific one for 'Blank Texture'
              PRIM_TEXTURE,     ALL_SIDES, "5748decc-f629-461c-9a36-a35a221fe21f", <1,1,0>, <0,0,0>, 0 ]
    touch_end(integer num_touches) {
        if(llDetectedLinkNumber(0) == LINK_ROOT) {
            state lit;
state lit {
    state_entry() {
        if(DEBUG) {
            llOwnerSay("state: lit");
            llOwnerSay("LAMP_TIMEOUT: " + (string) LAMP_TIMEOUT);
        // 0. switch on lamp
            // like in the 'unlit' state, the UUID here is the OSgrid specific one for 'Blank Texture'
              PRIM_TEXTURE,     ALL_SIDES, "5748decc-f629-461c-9a36-a35a221fe21f", <1,1,0>, <0,0,0>, 0 ]
        // 1. setup timer event
    timer() {
        state unlit;
[11:32]  Arielle Popstar: wouldnt  have  thought    it  could  be  made  to  look that  good  in  prim
[11:32]  Fu Barr: hey albertlr
[11:32]  Arielle Popstar: Hi  Albertr
[11:32]  albertlr Landar: Hum. Audrey 2?
[11:32]  Fu Barr: nope Alien Plant :)
[11:33]  Caro Fayray is Offline
[11:33]  albertlr Landar: Little Shop of Horrors.
[11:33]  Fu Barr: hehe - never saw that film
[11:33]  albertlr Landar: I did that show several times at my Theatre
[11:33]  albertlr Landar: No the musical.
[11:33]  Arielle Popstar: Hi  cilla
[11:33]  cilla teebrook: howdy
[11:33]  Fu Barr: hi cilla - welcome :)
[11:34]  Arielle Popstar: grabbing  coffee,  brb
[11:34]  Fu Barr: well i'm going to wait a few more minutes and then i'll start. today will (hopefully) be a shorter lesson than last wekk.
[11:35]  Fu Barr: hey wiz - welcome :)
[11:35]  Wizard Atazoth: Greetings,
[11:35]  Fu Barr: pull up some square cm's of sand :)
[11:35]  Fu Barr: we'll wait for ari to return with coffee and start
[11:35]  albertlr Landar: and donuts?
[11:36]  Fu Barr: who knows - ive got tea and madeleines
[11:36]  Fu Barr: albertlr - will you be recording the log?
[11:36]  albertlr Landar: yes
[11:37]  paela argus: hi
[11:37]  Fu Barr: cool - thnx
[11:37]  paela argus: sorry i just wake up
[11:37]  Fu Barr: hey paela :)
[11:37]  albertlr Landar: Hi paela
[11:37]  Fu Barr: arielle - you back yet?
[11:37]  Arielle Popstar: bk
[11:38]  Fu Barr: okay let's start )
[11:38]  Fu Barr: so first a short recap of last week and then we'll segway into this week's subject: Events
[11:38]  Fu Barr: last week we spoke about a few things the most important were:
[11:38]  Fu Barr: 1. every object/link set has a life cycle
[11:39]  Fu Barr: rez, exist, de-rez
[11:39]  Fu Barr: during this life-cycle a script that in placed inside an object/link set can react to the world around it. things that happen in the world during the life cycles of the object/linkset are called events
[11:40]  Fu Barr: 3. inside of an object/link set this life-cycle can be extended by stepping through 'states'
[11:41]  Fu Barr: 4. there's always one default state... called 'default'
[11:41]  Fu Barr: 5. a scripter can decide to use more than one state to structure his/her code.
[11:41]  Fu Barr: 6. brackets {} indicate blocks of code.
[11:41]  Fu Barr: 7. blocks of code always have open and close brackets - they ALWAYS need to be balanced.
[11:42]  Fu Barr: i think that's just about the core of what we spoke about last week.
[11:42]  Fu Barr: the chat log is on the wiki and the forum if you want to relive my typonese :)
[11:42]  Arielle Popstar: i  corrected  that :)
[11:42]  Fu Barr: so - today we're going to spend a little more time with the notion of events
[11:42]  Arielle Popstar: it  was an event
[11:43]  Fu Barr: I've tried to keep it short.
[11:43]  Fu Barr: lol
[11:43]  Fu Barr: so without further ado - please try and 'take copy' this Alien Plant
[11:43]  Fu Barr: then rez it from inv.
[11:43]  paela argus: i love this plant
[11:43]  Fu Barr: welcome koni
[11:44]  Arielle Popstar: yes  its  really  nice
[11:44]  Fu Barr: please take a copy this plant and rez it
[11:44]  Koni Lanzius: ty sweetie
[11:44]  Arielle Popstar: Hi  Koni
[11:44]  Fu Barr: now once you've rezzed it...
[11:44]  Koni Lanzius: hi there :)
[11:44]  Fu Barr: click on the sphere base once. and wait
[11:45]  Fu Barr: you'll see the 'light bulb' in the flower go on and then off
[11:45]  Fu Barr: try it again if you missed it
[11:45]  Fu Barr: click once and then wait
[11:45]  Fu Barr: wait
[11:45]  Fu Barr: the lamp will go off on its own
[11:45]  Alien plant: state: unlit
[11:45]  Alien plant: LAMP_LINK_ID: 13
[11:45]  Alien plant: state: lit
[11:45]  Alien plant: LAMP_TIMEOUT: 5.000000
[11:45]  Alien plant: state: unlit
[11:45]  Fu Barr: and in the meantime there's a bunch of debug data being show on your screen.
[11:46]  Fu Barr: so let's open the script and see what's going on
[11:46]  Fu Barr: everybody got the script open?
[11:46]  Fu Barr: I'll assume you have :)
[11:46]  Fu Barr: okay - let's go
[11:47]  Wizard Atazoth: Greetings, Jeff
[11:47]  Jeff Hall: hey Wiz and everyone
[11:47]  Fu Barr: so first of all, at the top of the script I've added a version note. so i know what is different in this script from the previous one
[11:47]  Fu Barr: hey jeff :)
[11:47]  Wizard Atazoth: Alien plants are multiplying here ^_^
[11:47]  Fu Barr: this is not something you have to do. It's something that I do for myself so in 3 weeks time i know what this thing does
[11:48]  Fu Barr: I do recommend you do something similar with your own scripts
[11:48]  Fu Barr: your free to decide what/how etc.
[11:48]  Fu Barr: now, just like in the previous script there are a few CONSTANT variables at the top
[11:49]  Fu Barr: we said Constants were variables that are set once and don't change. and we use CAPITALS to hint at that.
[11:49]  Fu Barr: we see that there's an extra variable in this version of the script - s= which is the length of time to wait before the lamp goes out automatically
[11:49]  Fu Barr: LAMP_TIMEPOUT
[11:49]  Fu Barr: it's made up by me.
[11:50]  Fu Barr: not something that is already defined in LSL
[11:50]  Fu Barr: then, if you look we have the same 3 states as in the previous version default, unlit and lit
[11:50]  Fu Barr: but, in the default one there's some significant changes
[11:51]  Fu Barr: remember how in the first version of this script we set the LINK_LAMP variable to 3
[11:51]  Fu Barr: because that was the 3rd and final prim in the link set...
[11:51]  Fu Barr: and we needed to know which prim to change so it would 'light up'
[11:51]  Fu Barr: well. in this version we're going it in a MUCH smarter way
[11:51]  Fu Barr: let's read the code.
[11:52]  Fu Barr: find //0. setup lamp
[11:52]  Fu Barr: we'll start reading from there
[11:52]  Fu Barr: I define 2 new variables
[11:52]  Arielle Popstar: 27
[11:52]  Fu Barr: "i" and "end"
[11:52]  Fu Barr: "i" is short for increment
[11:52]  Fu Barr: and "end" is short for.... end
[11:52]  Fu Barr: :)
[11:53]  Fu Barr: then there's one of the if DEBUG things, ignore that and move to the line: while(i <= end)
[11:53]  Fu Barr: what I'm asking the script to do here is do something while "i" is smaller or equal to "end"....
[11:53]  Fu Barr: and who can tell me what "end" is?
[11:53]  Fu Barr: or rather what value is held in "end"
[11:54]  Fu Barr: well the line where we define "end" says:
[11:54]  Fu Barr: integer end = llGetNumberOfPrims();
[11:54]  Fu Barr: so it stands to reason that llGetNumberOfPrims will return a value that's the number of prims in the link set
[11:55]  Fu Barr: and why do we need that number?
[11:55]  Fu Barr: well because the way that I'm doing things now is...
[11:55]  Fu Barr: I'm looping through all the prims in the link set...
[11:55]  Fu Barr: and when I find the prim that I've called 'bulb'
[11:55]  Fu Barr: then I stick that link id into LAMP_LINK_ID
[11:56]  Fu Barr: so that's my 'algorithm' for finding the right prim to 'light up'
[11:56]  Fu Barr: loop through all the prims
[11:56]  Fu Barr: and when you find the one called 'bulb' save it's number
[11:56]  Fu Barr: lets see the code...
[11:56]  Fu Barr: line 33
[11:57]  Fu Barr: while "i" is less than the last link id,
[11:57]  Fu Barr: line 37
[11:57]  Fu Barr: if the link name of the link at link id number "i" is equal to 'bulb'
[11:58]  Fu Barr: then set LAMP_LINK_ID to whatever value "i" is
[11:58]  Fu Barr: then continue the loop.
[11:58]  Fu Barr: by increasing "i" to the next value
[11:58]  Fu Barr: we have the script march past every link in the link set and check the name of each link prim
[11:59]  Fu Barr: once we find the right link, we store the id.
[11:59]  Fu Barr: i hope this is clear to all
[11:59]  Fu Barr: questions?
[11:59]  Fu Barr: cool.
[11:59]  Fu Barr: it doesn't take a lot of imagination that a loop like this can be used to initialize all sorts of things.
[12:00]  Fu Barr: but for our simple plant thing - this is all we need
[12:00]  Fu Barr: and then, we leave the default state because we force the script to move to the 'unlit' state
[12:00]  Fu Barr: thats in line 46
[12:00]  Fu Barr: and we never go back into the default state after that
[12:01]  Fu Barr: so, when we get into the unlit state...
[12:01]  Fu Barr: we have an event.
[12:01]  Fu Barr: the 'state_entry' event
[12:01]  Fu Barr: and there we do what we did last week - we switch off the bulb.
[12:02]  Fu Barr: I also removed the switching off of the root prim (if you compare the two scripts) because we don't have the green cylinder button in this plant.
[12:02]  Fu Barr: then the script just waits.
[12:02]  Fu Barr: it sits and does nothing
[12:02]  Fu Barr: just hanging about in the unlit state
[12:03]  Fu Barr: until.... a new event happens
[12:03]  albertlr Landar: unless you click it again.
[12:03]  Fu Barr: you touch the plant - exactly
[12:03]  Fu Barr: and like jeff said last week, we 'handle' this event in the event handler 'touch_end'
[12:03]  Fu Barr: liine 70
[12:04]  Fu Barr: in this block, if check...
[12:04]  Fu Barr: if the touch happened to be on the root prim, then...
[12:04]  Fu Barr: go to the state 'lit'
[12:04]  Fu Barr: that's all the event handler does. And all it has to do.
[12:04]  Fu Barr: It's the switch part.
[12:05]  Fu Barr: wait till there's a click event, then do something
[12:05]  Fu Barr: all good - welcome - take a copy of the plant! :)
[12:05]  Wizard Atazoth: Greetings, inanna.
[12:05]  Fu Barr: open it up and dive into the script with us :)
[12:05]  Jeff Hall: hey ina
[12:05]  inanna beaumont: hi all :-)
[12:05]  Fu Barr: so now that we have line 74 sending us to 'lit' lets see what's in the lit state...
[12:06]  Alien plant: state: lit
[12:06]  Alien plant: LAMP_TIMEOUT: 5.000000
[12:06]  Alien plant: state: unlit
[12:06]  Alien plant: LAMP_LINK_ID: 13
[12:06]  Fu Barr: well again - there's always the 'state_entry' event handler. you always have one of those.
[12:06]  Fu Barr: and the first bits are all the DEBUG printing information stuff. that now old news for us.
[12:06]  Fu Barr: but look at line 90
[12:07]  Fu Barr: I commented there: // 0. switch on lamp
[12:07]  Fu Barr: and in line 99: // 1. setup timer event
[12:07]  Fu Barr: this is how I build my 'steps'
[12:07]  Fu Barr: i write in human language in a comment what the script needs to do... and then i write the code that does it.
[12:08]  Fu Barr: that way i don't go nuts trying to figure out what to do.
[12:08]  Fu Barr: in this case...
[12:08]  Fu Barr: the script needs to turn on the bulb and then set the timer in motion
[12:08]  Fu Barr: so lets read in the code...
[12:08]  Fu Barr: the switching on is exactly the same as last week
[12:08]  Fu Barr: we use llSetLinkPrimitiveParamsFast
[12:09]  Fu Barr: function (more about those in two weeks)
[12:09]  Fu Barr: and we feed it the LAMP_LINK_ID so it knows which of the prims in the link set to modify and we give it the modification parameters
[12:09]  Fu Barr: this is the same as we've been doing all along
[12:09]  Fu Barr: but in line 100 we do something new
[12:09]  Fu Barr: in line 100 we actually FORCE AN EVENT TO HAPPEN
[12:10]  Fu Barr: usually an event happens because something happens in the world
[12:10]  Fu Barr: you touch a prim
[12:10]  Fu Barr: you collide with it you pay it money etc.
[12:10]  Fu Barr: but a timer is something we have to set in motion ourselves
[12:10]  Fu Barr: it's like pressing the button on a countdown timer
[12:11]  Fu Barr: and how long does it wait before it sets off the timer event?
[12:11]  Fu Barr: well...
[12:11]  Fu Barr: it waits LAMP_TIMEOUT seconds
[12:11]  Fu Barr: which we've defined all the way at the top of our script
[12:11]  Fu Barr: in our case 5.0 seconds
[12:12]  Fu Barr: if you want to have it wait longer/shorter - you just change the value
[12:12]  Fu Barr: so what happens after LAMP_TIMEOUT seconds passes?
[12:12]  Fu Barr: the script engine will fire the timer event...
[12:12]  Fu Barr: and in line 103...
[12:12]  Fu Barr: we see the timer event handler
[12:13]  Fu Barr: all it does... is send the script back into its 'unlit' state.
[12:13]  Fu Barr: and we know that the state_entry event handler of the 'unlit' state... that turns off the bulb
[12:13]  Fu Barr: and that's all there is to it.
[12:13]  Fu Barr: 3 states and a couple of event handlers
[12:13]  Fu Barr: default for setting up the correct ID of the bulb
[12:14]  Fu Barr: and lit/unlit states to light and turn off the lamp
[12:14]  Fu Barr: and a timer event and event handler to turn off the lamp at the right time.
[12:14]  Fu Barr: dassit.
[12:14]  Fu Barr: end of.
[12:15]  Arielle Popstar: why  does  the  script  identify  the  bulb  as:
[12:15]  Arielle Popstar: Alien plant: Link ID: 13, prim name: bulb
[12:15]  Fu Barr: now of course there's a bunch more event handlers etc. and we'll see more of them in the next two sessions.
[12:15]  Arielle Popstar: while  in edit  mode  it  shows  as  10
[12:15]  Fu Barr: becuae that's how it's been linked
[12:16]  Fu Barr: i have no idea - lol
[12:16]  Fu Barr: it does work though
[12:16]  Arielle Popstar: its  a miracle
[12:16]  Fu Barr: i'll have to dig into that.
[12:16]  Fu Barr: yes indeed!
[12:17]  cilla teebrook: sorry RL needs me
[12:17]  Fu Barr: it might be a script reset thing
[12:17]  cilla teebrook: ty fu
[12:17]  Fu Barr: i dunno - will try and figure it out for next week.
[12:17]  Fu Barr: yvw - thanks for joining!
[12:17]  Arielle Popstar: tc  Cilla
[12:17]  Fu Barr: any questionis abbout this session?
[12:17]  Fu Barr: I though i'd try and keep it short this time.
[12:18]  Arielle Popstar: does  everyone  see  my  debug  messages?
[12:18]  Fu Barr: hopefully repeating some of the stuff from last week and a bit more time on events reading the code made it all a bit clearer so we can do some more complex stuff nexxt week.
[12:18]  albertlr Landar: no
[12:18]  albertlr Landar: only your debug message will show in your chat.
[12:18]  Fu Barr: no - i'm using llOwnerSay() which keeps all the messages private
[12:18]  Arielle Popstar: yes  it  did  help  to go back over that  Fu
[12:19]  Fu Barr: please run with the script - relink the plant and see if/how it should still work
[12:19]  Fu Barr: because the script should still pick up the next link id every time you relink it
[12:19]  Fu Barr: try and break the script and learn from the broken pieces :)
[12:20]  Fu Barr: next week we're going to move on to something entirely more powerful
[12:20]  Arielle Popstar: cool
[12:20]  Fu Barr: we're going to add scripting to this:
[12:20]  albertlr Landar: and everyone take their plants with them please.
[12:20]  Arielle Popstar: thx  for  the  lesson Fu, it  was  helpfull
[12:21]  albertlr Landar: hum looks like a gate to somewhere?
[12:21]  Fu Barr: we're going to script this 'portal'
[12:21]  Fu Barr: using the light stuff and a bunch of new stuff
[12:21]  albertlr Landar: on a hypergrid gate?
[12:21]  Fu Barr: you can do that too - yes :)
[12:21]  albertlr Landar: or teleport
[12:21]  Arielle Popstar: can  one  be  put  for  people  to  copy  and  look through the  script   if  they  come  later?
[12:22]  Fu Barr: like albertlr said - don't forget to delete your Alien Plant :)
[12:22]  albertlr Landar: This script and the lessons will be put in the wiki.
[12:22]  albertlr Landar: just have to edit and correct it first.
[12:22]  Fu Barr: not sure this is the best place to put out the objects...
[12:22]  Fu Barr: lets discuss.
[12:22]  Jeff Hall: ty Fu and have a good time everyone:)
[12:22]  Fu Barr: any questions?
[12:23]  Fu Barr: np jeff - thanks for joining us!
[12:23]  inanna beaumont: you too Jeff
[12:23]  albertlr Landar: I was thinking about a way to maybe put up an iar of the object in the wiki.
[12:23]  Arielle Popstar: or maybe  on  wright  plaza/
[12:23]  Fu Barr: yeah - maybe
[12:23]  albertlr Landar: yea, that could be done also.
[12:24]  Fu Barr: maybe we could box it all up into something at the end and we can keep it as a give away on lbsa?
[12:24]  albertlr Landar: yea, good idea also.
[12:24]  albertlr Landar: we could include the script and lessons and object.
[12:24]  Fu Barr: yep
[12:24]  Fu Barr: lol ianna didn;t derez her plant
[12:24]  albertlr Landar: And number them as they are in the wiki.
[12:24]  Fu Barr shakes his head
[12:25]  Fu Barr: wiz - what are your thoughts?
[12:25]  Fu Barr: hehe he's off to the loo :)
[12:25]  Fu Barr: okay well that's all from me.
[12:25]  Fu Barr: thanks for coming
[12:26]  albertlr Landar: well thanks again Fu, enjoyed your presentation.
[12:26]  Arielle Popstar: i  will  post the  lesson  on the  forum  after  i clean  up  the  log  a  bit
[12:26]  Fu Barr: like i said - i decided to keep it simple this week. next week we're going to do much more
[12:26]  albertlr Landar: well this will be in the wiki Arielle too.
[12:27]  albertlr Landar: Thanks Fu
[12:27]  Fu Barr: np :)
[12:27]  Arielle Popstar: ok,  but  that  might  be  a  bit  before  it is  up  on  it?
[12:27]  Fu Barr: right - me go do something else for abit
[12:27]  albertlr Landar: no problem.
[12:27]  Fu Barr: been at the machine for 700 hours - or so it feels
[12:27]  Arielle Popstar: i  just  doing  this  way  so  people  who  come  next  week  have  had  a  chance  to catch  up
[12:28]  albertlr Landar:
[12:28]  albertlr Landar: Here is last weeks lesson
[12:28]  Arielle Popstar: ahh  ok\
[12:28]  albertlr Landar:
[12:28]  Fu Barr: i dont see much harm in having both up
[12:29]  albertlr Landar: And here are the ones from last year too.
[12:29]  Fu Barr: some people will gravitate to the wiki, other to the forum
[12:29]  albertlr Landar: Under Scripting class logs
[12:29]  albertlr Landar: Oh its fine to put it in the forums too.
[12:29]  albertlr Landar: Its just they tend to eventually get lost in the forums.
[12:29]  Fu Barr: in the end in a perfect world i'd make a bunch of video's and stick them on youtube
[12:29]  Arielle Popstar: well  no  sense  in  both of  us  editing  the  log  so  i  can  wait
[12:29]  Fu Barr: but the world ain't perfect - lol
[12:30]  albertlr Landar: Ok well bye all,
[12:30]  Fu Barr: okay my job is done here - thanks again and tc all!
[12:30]  albertlr Landar: take care.
[12:30]  Fu Barr is Offline
[12:30]  Arielle Popstar: tc  Fu