CODE
integer switch = 1;
touch_start(integer n)
{
if (switch)
open() ;
else
close();
switch = !switch;
}
These forums are CLOSED. Please visit the new forums HERE
Better to use states or not? |
|
|
Kornscope Komachi
Transitional human
Join date: 30 Aug 2006
Posts: 1,041
|
05-19-2008 19:53
For a moving window is it better to use different states or is the following ok for best practice?
CODE
_____________________
SCOPE Homes, Bangu
----------------------------------------------------------------- |
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
05-19-2008 20:03
For such simple logic I'd say that way is perfect. When you have complex logic, especially where different event handlers are required for different cases, I think states are a good tool. I don't think there's any magic criterion for when and when not to use them.
|
|
Kahiro Watanabe
Registered User
Join date: 28 Sep 2007
Posts: 572
|
05-19-2008 20:42
states are faster and clearer you need less "if" and less variables.
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
05-19-2008 20:58
you need less "if" and less variables. But you have more states and more state changes.. For something like the OP posted, I can't imagine you get any real savings by using states. |
|
Malachi Petunia
Gentle Miscreant
Join date: 21 Sep 2003
Posts: 3,414
|
05-19-2008 21:41
especially where different event handlers are required for different cases, I think states are a good tool. |
|
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
|
05-19-2008 23:44
I agree with Malachi, for simple on/off stuff, like a light switch, it's silly to use states that way. States are for exactly what Malachi said, establishing another set of event handlers. Even the wiki states (no pun intended)
"After the default state definition can follow additional state definitions which the script may use to change how and which events are handled." Note how it mentions how and which events are handled. The underlying implication is that it is there for setting up new event handlers. |
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
05-20-2008 06:24
I agree with Malachi, for simple on/off stuff, like a light switch, it's silly to use states that way. States are for exactly what Malachi said, establishing another set of event handlers. Even the wiki states (no pun intended) "After the default state definition can follow additional state definitions which the script may use to change how and which events are handled." Note how it mentions how and which events are handled. The underlying implication is that it is there for setting up new event handlers. It is and it isn't. In typical SL fashion certain events are persistant across states i.e. timers. _____________________
I'm back......
|
|
Kahiro Watanabe
Registered User
Join date: 28 Sep 2007
Posts: 572
|
05-20-2008 06:39
But you have more states and more state changes.. For something like the OP posted, I can't imagine you get any real savings by using states. "States vs. Global Variables A state and a set of global variables can serve the same purpose, and each can be expressed in terms of the other. In general, using states over global variables allows immediate script state assumption without making comparisons--and the fewer comparisons a script makes, the more regular code statements it can run." http://lslwiki.net/lslwiki/wakka.php?wakka=state Check example 1 and 2. |
|
RobbyRacoon Olmstead
Red warrior is hungry!
Join date: 20 Sep 2006
Posts: 1,821
|
05-20-2008 06:56
Yes, branching is teh evil, especially in interpreted languages. But those wiki examples are clearly there only to demonstrate the point being made, not as examples of real-world best practices. How fast does a touch_start handler need to be, is it realistic to think that one additional compare-and-branch will be noticeable to a user at all?
For extremely simple tasks like that given in the original post, you should simply do whatever you are more comfortable with. Maintenance and readability are far more valuable goals than any theoretical gains in efficiency for any script of that kind of trivial complexity. . _____________________
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
05-20-2008 07:52
Yes, branching is teh evil, especially in interpreted languages. But those wiki examples are clearly there only to demonstrate the point being made, not as examples of real-world best practices. How fast does a touch_start handler need to be, is it realistic to think that one additional compare-and-branch will be noticeable to a user at all? I agree but my response to Kahiro above was more based on size than speed. For example, this... CODE
Produces: "Object: 16119" And this... CODE
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
05-20-2008 11:50
not sure how states are handled under mono, but the behavior of slowed state changes (in mono) suggests that they exclude all but the current state (I think strife suggested possible JIT compiling on states was happening)... the above example probably wouldn't matter much either way, complex logic might work better as states, unless you want to do a lot of rapid state changes. (there's a post around here about jesse testing the speed of various funtions/state changes that deserves credit).
when it comes down to it though you should go with watever gives you the simplest logic, and if you feel like it try the alternate for speed... but the simplest logic usually wins for speed _____________________
|
| . "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... | - |
|
Shadow Subagja
Registered User
Join date: 29 Apr 2007
Posts: 354
|
05-20-2008 12:18
I use states for readability, _if the state behavior gets non trivial_.
If I find myself with awhole buncha global integers like "SLEEPING' 'DRIVING' 'DANCING' 'ANIMATING' or what have you, and a bagillion other variables all of which mean something but only when one of the above is turned on.. I start to look at whether the script could be broken up to either 1. jump from state to state as it runs like a typical state machine might, really only doing one set of things at a time depending on what state its in 2. or to use the states as callbacks to break up related bits of functionality, like menu handlers and short lived user interaction/requests for input etc. I also seem to use them a lot to deal with initialization, jumping from state to state as I parse and deal with individual notecards so each state can be doing very obvious parsing and not trying to handle the world in each event. Honestly, though.. I think a script has to evolve beyond 'door open, door closed' before it really starts to be an obvious win for maintainability. |
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
05-20-2008 14:39
Agree with everything posted above, including that the example script is perfect the way it is and is not a good candidate for state changes. I don't use state changes unless I have to. That being said, I find that I do HAVE to use state changes all of the time. Those last two sentences really aren't at odds with each other as they may seem at first glance.
State changes make the simulator do more work, and as pointed out in Void's post, MONO really doesn't treat them nicely and is actually slower doing a state change then the old LSO. Took liberty of tweaking Sindy's script to show that even if you don't declare a state_entry or state_exit, the simulator still has to process each during a state change: CODE default {The simulator is actually doing all of this: "*** state_entry() GetFreeMemory() *** touch_start(1) state->closed *** state_exit() *** state_entry() *** touch_start(1) state->default *** state_exit() *** state_entry() GetFreeMemory()" But in most cases the end user is never going to notice a difference in speed using either method and as also pointed out, it is sometimes much more readable breaking a script up into different states and can be more efficient. I have also started to go one step further when I am doing state changes now. In a case where in state_entry I have multiple global variables that have to be declared such as setting a random listen channel, building a menu etc & you don't really need to keep defining them then just add one more state: default{ state_entry{//globals are declared state run; } state run{ //blah, blah, blah state next; } state next{ //do other stuff state run; Now the simulator isn't constantly defining a bunch of vairiables and using llFrand etc when it doesn't really need to. _____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum |
|
Ollj Oh
Registered User
Join date: 28 Aug 2007
Posts: 522
|
05-20-2008 18:48
I LOVE how the post above undemines every claim it makes, proving how useless states are in most cases. Only reason left is readability, because its put in nice blocks. but that doesnt make its logic clearer, its actually obfuscating complex state interactions.
Do you have ANY idea how many bytes each state is wasting in compuiled code, compared to some smart if-conditioning? I dont, because even testing it is a waste of time. Id rather use ONE 32bit integer as a bitfield to store 2^32 states. Or 32 states that can run parallel, FUCKING PARALLEL, and 2 small procedures to change states bitwise. Or a mix of both. SOME event handlers may check SOME bits of that integer, FLEXIBLE and small. state changes just mess up mah event queue, but if you want fun timey with events, like someone buying something and waiting to get delivery, being dropped by a state change, have fun with complaints. kthxbye! |
|
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
|
05-20-2008 19:20
There are valid uses for state changes, but I dont use them a whole bunch really. I have a swing, which sits there and waits for someone to get on it, then it switches to the swing state, where the swing is swung, and checks to see if they got off, if they get off, it reverts back to the default state. It made coding it alot easier. As far as efficiency goes, I guess it all depends on the script. If it's overly complex, states may make things easier, but if it's a simple script, states are more a waste of time. Ollj Oh seems to want to cram 32 flags into an integer, which is pretty silly considering it would make readability difficult even for Ollj down the road. Ollj, if you want to get THAT crazy, why not just code in LSL2 or mono bytecode so you can be even more efficient. There's a line between readability, efficiency, and the necessity of each. I imagine it depends on each particular situation to determine the levels of readability and execution efficiency. When it comes to a simple light switch, even though using states is kind of overkill, it's not going to break the server's back to execute a few extra cycles at most, a couple of times a day. The scripts we write are executed on the server, along with all the other scripts, so we do need to be as efficient as possible, but at the same time, take into account how often this script is going to be executed. Any script, no matter how efficient, is still taking up 16k of memory, and if no events are queued, (it's just sitting there waiting), efficiency doesn't really mean much because nothing is being executed.
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
05-20-2008 19:29
Ollj Oh seems to want to cram 32 flags into an integer For people who learned to code using languages like C and C++, this is pretty much second nature and, once you get used to it, not a very hard thing. It's also, for certain kinds of code, a lot more efficient and far less complicated. edit: not talking about lsl 'states' above - just talking about using bit flags.. |
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
05-20-2008 19:32
I LOVE how the post above undemines every claim it makes, proving how useless states are in most cases. Simple challenge. Script an AO or a flight assist script and post it here with no state changes. Yes, state changes suck, I stated they suck and why, but they are still necessary sometimes. _____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
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
|
Theres more than one way to skin a cat
05-21-2008 00:54
It really boils down to what the individual scripter/programmer is happiest with.
Bit packing is second nature (for me at least) and I use it when ever I need too, but as Jesse pointed out states also have their uses. You could go the whole hog and write each state as a seperate (single state) script, switching state by linked Message. It works and its quite useful in certain applications. It keeps the code modularised and is generally more readable than a multistate script, or smart if's, but does require far more careful handling of data. *** I'd respectfully request that people moderate their language, it was uncalled for. *** Newgy has sensitive ears _____________________
I'm back......
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
05-21-2008 05:58
I'm back...... Welcome back!! |
|
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
|
05-21-2008 09:35
I just write everythjing as a TECO macro...
_____________________
So many monkeys, so little Shakespeare.
|
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
05-21-2008 09:44
Wow.. Newgy and Lee posting again. Saw Strife recently, too. And that bozo Jumpman.
You 4 are all alts of each other, aren't you?!?1 _____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224 - If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left |
|
Shadow Subagja
Registered User
Join date: 29 Apr 2007
Posts: 354
|
05-21-2008 10:59
I LOVE how the post above undemines every claim it makes, proving how useless states are in most cases. Only reason left is readability, because its put in nice blocks. but that doesnt make its logic clearer, its actually obfuscating complex state interactions. Do you have ANY idea how many bytes each state is wasting in compuiled code, compared to some smart if-conditioning? I dont, because even testing it is a waste of time. Id rather use ONE 32bit integer as a bitfield to store 2^32 states. Or 32 states that can run parallel, FUCKING PARALLEL, and 2 small procedures to change states bitwise. Or a mix of both. SOME event handlers may check SOME bits of that integer, FLEXIBLE and small. state changes just mess up mah event queue, but if you want fun timey with events, like someone buying something and waiting to get delivery, being dropped by a state change, have fun with complaints. kthxbye! I think the example you appear to have been bittin by (changing states while actively dealing with critical requests, and losing those requests) is an example of _bad coding_, not an example of the uselessness of state machines as a code-organization technique. I think you have to wrap your head around the fact that states (in LSL) are designed to not take events with them. From what I can grok, this is so that you don't have to always be running all of your possible events and switching behavior depending what bits are set before many calls and have your handlers open despite their purpose having been fulfilled. At the end of the day, this serves to improve system load at the cost of memory (although by how much with all the state testing code in a 1-state approach, I can't say), and also to reduce the in-code switching you have to do while handing all of your events. In LSL you can do almost anything you can do with states in some other way, its a cosmetic choice really. Lets face it, some people like ugly just fine. My experience is that the code bloat caused by not using states and instead manually starting and stopping listeners, testing integers around big chunks of handling inside listen and link message, data server, changed, and permissions events, and trying to clearly depict what the code is doing at the same time leads me naturally to take the 'like chunks' surrounded by the same bit tests and group them all together in a state, wherever possible. Also Do you mean 1 2^32 integer to store 32 states in one small int like this (if so your code must be... interesting): integer MYINT; if(MYINT ^ 1) else if(MYINT ^2) . . x32 or do you mean 33 2^32 integers to store 32 states in one small int like this: integer MYINT; integer STATE1 = 1; integer STATE2 = 2; integer STATE3 = 4; . . x32 if(MYINT ^ STATE1) |
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
05-21-2008 11:23
or do you mean 33 2^32 integers to store 32 states in one small int like this: integer MYINT; integer STATE1 = 1; integer STATE2 = 2; integer STATE3 = 4; . . x32 if(MYINT ^ STATE1) Er.. I think he meant keeping 32 values in an int.. integer NEWBIE = 0x00000000; integer FOUND_MAGIC_LAMP = 0x00000001; integer KILLED_TROLL = 0x00000002; integer HELPED_OLD_LADY_CROSS_STREET = 0x00000004; integer TOOK_MEDS = 0x00000008; integer gStuffDone = NEWBIE; . . . if (gStuffDone & FOUND_MAGIC_LAMP) { llSetGlow (llGetGlow() * 100000); } if (!(gStuffDone & TOOK_MEDS)) { llSetHealth (PARANOID); } . . _____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224 - If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left |
|
Shadow Subagja
Registered User
Join date: 29 Apr 2007
Posts: 354
|
05-21-2008 11:41
I know, my point is its a lot more memory than 1 int to manage that.
|
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
05-21-2008 11:51
...but possibly less than using 32 separate ints.
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224 - If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left |