States
|
|
DevilPliers VanDornan
Registered User
Join date: 12 Oct 2006
Posts: 20
|
10-13-2006 13:52
Ok.. I've been reading a bunch about scripting lately.. and there's one thing I just don't understand - states. I've tried reading the wiki, but that didn't really answer my questions. First of all.. is there a list of them? In the wiki crash course they go through this example script where they "script" a baby.. they use the state awake, but then say it's not a valid LSL state. So.. is there a list of them? And what are they used for? Seems like you could just stay in the default state. I guess it kind of makes sence for things like driving, but I still don't understand what it changes. Could someone possibly explain this for me? Thanks,
Jes
|
|
Don Misfit
Registered User
Join date: 1 Jun 2006
Posts: 60
|
10-13-2006 14:28
Briefly... use states to coordinate what code you want active at different times.
A simple example might be a door to a house --- when you are home, you want someone to be able to touch the door and have the door-bell ring. When you are not home, you don't want the bell to ring, but maybe you don't even want the door to be touch-enabled. If your script has a touch_start() handler in its current state, the mouse cursor will change when it's moved over the door - even if you set a variable to ignore any touches. To avoid this, you script in "home" and "away" states, and only the home state has the touch_start() event handler. You said you looked at the wiki crash course... also take a look at the wiki Examples and Script Library pages for sample code - might help.
|
|
Jopsy Pendragon
Perpetual Outsider
Join date: 15 Jan 2004
Posts: 1,906
|
10-13-2006 15:24
If it helps, you can almost think of states as different programs, you can specify which program is active. The first and only required 'state' is the "default" state. The rest you make up for yourself. For example: The big benefit to using states is that you can have some events active or inactive, depending on what 'mode' your object is currently in... without having to have big nasty "if" statements to determine if it should respond to a touch, spoken or some other kind of event. Here's an example. (the variable x isn't used for anything here, it's just an example of how you'd pass data from one state to another) integer x; // this is a global variable, usable by both states below.
default { state_entry() { // this code always gets run whenever this script is compiled, // or reset, or when another state issues a "state default;" // forcing this state to become active again. llSetText("default state active \n (not listening) \n touch me!", <1,1,1>, 1.0 ); }
touch_start( integer n ) { llOwnerSay( "switching to listening state" ); state touched; }
on_rez( integer n ) { llResetScript(); } }
state listening { state_entry() { llListen( 0, "", "", "" ); llSetText("listening state active \n (not touchable) \n speak to me!", <1,1,1>, 1.0 ); x=2; }
listen(integer chan, string name, string msg, key id ) { llOwnerSay("switching to default state"); state default; }
on_rez(integer n) { llResetScript(); }
state_exit() { // You could use llListenRemove() here.. but it's not necessary, // the listener started when this state became active automatically // stops when this state is left. } }
Anyway, I hope that helps clarify how states can be helpful! =)
_____________________
* The Particle Laboratory * - One of SecondLife's Oldest Learning Resources. Free particle, control and targetting scripts. Numerous in-depth visual demonstrations, and multiple sandbox areas. - Stop by and try out Jopsy's new "Porgan 1800" an advanced steampunk styled 'particle organ' and the new particle texture store!
|
|
DevilPliers VanDornan
Registered User
Join date: 12 Oct 2006
Posts: 20
|
10-13-2006 16:46
Hm.. so is there a list of states somewhere? Or can I use whatever word I want to define a state? Thanks though, that answered a lot of the questions I had.. makes a lot more sense now.
Jes
|
|
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
|
10-13-2006 17:31
From: DevilPliers VanDornan Hm.. so is there a list of states somewhere? Or can I use whatever word I want to define a state? Thanks though, that answered a lot of the questions I had.. makes a lot more sense now. Jes You make your own states. The only one LSL defines is default. Once you get the hang of states, they are ever so useful.
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
10-13-2006 17:33
You can use whatever word you want, more or less. You can't call a state 'default', because there's already a default state. I don't think you can call a state "if" or "for" or any other keyword. I don't know if you can call a state llResetScript or any other function name. But those are 'freak cases', you wouldn't want to do that. In general, you can pick any word that's not part of the LSL 'vocabulary' and use that as a state name. Basically, the same way you name variables.
|
|
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
|
10-13-2006 18:05
yea you can use any word you like after the default state (all lsl scripts need a default state) ... i would try to name them along the lines of their functions (such as the above example state listening)
|
|
Vares Solvang
It's all Relative
Join date: 26 Jan 2005
Posts: 2,235
|
10-13-2006 18:23
This script uses states in a way that is very easy to follow, check it out. It helped me to understand them.
|
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
10-13-2006 18:35
Just wanted to give you a warning DevilPliers. We come to SL to escape from RL and have freinds and fun. BUT........ once you get the scripting bug it is all downhill. Next thing you know you will create an alt where you can escape RL AND SL. So for your sanity it is probably better if you run screaming from this forum and go engage in some nice healthy pixel slapping or some other FUN activity. Life before SL: Watch TV till 11 PM. Life after SL: Chat and shop til 11 PM. Life after scripting: 3 AM, still here FOR JUST 5 more minutes  .
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime. From: someone I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
10-14-2006 10:28
ROFL, welcome to my world Jesse......
|
|
Vares Solvang
It's all Relative
Join date: 26 Jan 2005
Posts: 2,235
|
10-14-2006 12:05
I got so bad for a while my partner started to make jokes about it in front of other people. Now I try to restrict my scripting to when she's offline.  (Which really isn't too hard since we live on opposite sides of the world.)
|