These forums are CLOSED. Please visit the new forums HERE
States vs. user defined functions. |
|
|
Damien1 Thorne
Registered User
Join date: 26 Aug 2007
Posts: 4,877
|
06-08-2009 10:32
I have another basic question trying to get my head around things conceptually. In studying example scripts and the lslwiki, I am not really grasping the use of differents states as opposed to user defined functions. States seem to transfer control of the program from one part of it to another, much the same way a function call transfers control from the main program to the function and then back when function completes. What is the advantage of using one over the other?
|
|
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
|
06-08-2009 10:50
Someone with a more sophisticated understanding will give you a more rigorous answer, but I tend to think of functions as globally defined actions that may be accessed from any state, where states are sets of events that define environmental conditions. I don't know why my mind drifts to thoughts of food (except that it's lunchtime)....... I might conceive of three "states"-- satisfied, hungry, and ravenous --that define different conditions of my body at various times of the day. In each state, I respond to environmental impulses in different ways. In ALL of them, however, I may need a user-defined function called "eating" that I call when I want to do something in response to those impulses. "Eating" is global.
OK... so that's goofy. In my own relaxed way of thinking about scripts, it helps me decide how to compartmentalize the various actions. Now you need the voice of a master scripter to give you a good technical answer. _____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask....
![]() Look for my work in XStreetSL at |
|
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
|
06-08-2009 10:51
States do not replace functions and functions not states. You can not return from a state, you need an active change of state. You cannot pass variables and return variables like you do with functions.
I like to see states as a row of 'main programs' you can switch between ![]() _____________________
From Studio Dora
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
06-08-2009 10:54
States are really a way to separate very distinct behaviors that are used at different points in time. Consider code that looks like this (nevermind that there are more optimizations that could be done and such, just pay attention to the organization of the code):
CODE
And now see what it looks like when done using states: CODE
Now the logic can be a lot more complex than that, with many different event handlers (not just a timer) that all behave differently depending on what logical state the script is in (perhaps touching a sleeping or meditating robot makes it stand up and start walking, but touching a walking or running robot makes it jump). Would you rather repeat the if-else conditionals in each event handler like in the first example, or repeat the event handler declarations like in the second example? There are cases when dividing things up between states can make your code a LOT simpler and better organized. There are other aspects to consider also though. One is the fact that there is some non-trivial behind-the-scenes work the server does when you switch states (removing all listens, probably registering a potentially different set of events the script is interested in, etc.). So it is generally best not to be switching states very rapidly, both for script time (server lag) and the performance of your own application. Another is that sometimes switching states is the ONLY way to accomplish something, such as only accepting money payments when a vendor is properly configured (a resident doesn't get the "pay" option for a prim/object unless one of its script is in a state that has a "money" event handler). |
|
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
|
06-08-2009 11:17
States are best used when you are modeling or interacting with a system where there are well known and distinctive states to the system. For example, you might use a state for reading a notecard because while you're reading that notecard you're not doing anything else, and you don't want to have to handle and ignore events that are not related to that operation.
If the state of the system you're interacting with or modeling is complex, or where you have a lot of behavior in common across multiple states, then you would use state variables instead of states. for example, most AOs don't use different states to represent different animation states because there's so much common event handling between animation states. _____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/
"And now I'm going to show you something really cool." Skyhook Station - http://xrl.us/skyhook23 Coonspiracy Store - http://xrl.us/coonstore |
|
Damien1 Thorne
Registered User
Join date: 26 Aug 2007
Posts: 4,877
|
06-08-2009 11:28
Thanks everyone. Gives me more to think about.
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
06-08-2009 17:02
a simplified version of what argent said:
states control what input you want to allow right now + my addendum: changing states cancels listens, sensor repeats, and a few other things, but not timers, target omega, or constant force physics settings (fun attributes to abuse) _____________________
|
| . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - |
|
Paladin Pinion
The other one of 10
Join date: 3 Aug 2007
Posts: 191
|
06-08-2009 17:42
changing states cancels listens, sensor repeats, and a few other things Anybody got a full list of these, for reference? _____________________
Mote Particle Script Generator - easier and faster than any HUD
Also: Paladin's Sunbeam. Up at dawn, gone by dusk, day and night sounds, fully configurable See more at: www.paladinpinion.com |
|
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
|
06-08-2009 17:59
a simplified version of what argent said: states control what input you want to allow right now You're right, I was less than adequately specific about the effect of states on events. _____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/
"And now I'm going to show you something really cool." Skyhook Station - http://xrl.us/skyhook23 Coonspiracy Store - http://xrl.us/coonstore |
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
06-08-2009 18:40
Anybody got a full list of these, for reference? honestly, no. I think the only ones it cancels are listens, sensor repeats, and possibly the target, look at and and incomplete one time physical functions (move to and rot to, target.)... those last few I've never tested, just an educated guess) state changes will also toss out a new start for repeat style events (like touch and collision start), for things that would still be triggering the simpler (touch and collision) events oh and there was (still is?) a bug when moving from a state without certain event handlers (like touch) where they'd refuse to trigger in the new state under certain circumstances. pretty sure that got fixed though _____________________
|
| . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - |
|
Damien1 Thorne
Registered User
Join date: 26 Aug 2007
Posts: 4,877
|
06-08-2009 18:58
Thanks to everyone for the answers to my rather basic questions that I tend to start threads about. I try to ask them in such a way that I can learn out how to do things rather just letting you guys write quick scripts for specific applications.
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
06-08-2009 20:46
Thanks to everyone for the answers to my rather basic questions that I tend to start threads about. I try to ask them in such a way that I can learn out how to do things rather just letting you guys write quick scripts for specific applications. I think you've captured the essence of what this forum is about then; IMO more than many who come here with requests. Obviously we're happy to help with specific questions as well, but--helping people learn HOW so they can go out there and practice better scripting--THIS is the good stuff! ![]() |
|
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
|
06-08-2009 23:57
oh and there was (still is?) a bug when moving from a state without certain event handlers (like touch) where they'd refuse to trigger in the new state under certain circumstances. pretty sure that got fixed though Do not change states from within the touch_start() event, since this will prevent the touch_end() event being called even if you did not define it and from this preserve a partial solve touch event chain across state transitions. To perform state transitions at a "touch" make use of the touch_end() event instead! I'm not sure it is a bug, rather a 'specification by design' ![]() _____________________
From Studio Dora
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
06-09-2009 17:03
Is it this one you have on your mind? Reference: http://wiki.secondlife.com/wiki/Touch_start I'm not sure it is a bug, rather a 'specification by design' ![]() no, it was actually a jira, where moving from a state w/o the handler caused the next state not to be able to receive those events, but the one after it was fine... I do believe it's been fixed though. very hurtful to scripts that used dead states to handle listen kills. there was some additional thing involved, I think possibly the default state, but it's been awhile. _____________________
|
| . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - |