I deliberately did not talk too much about states and asked a question about state_entry :)
Today we go with a slightly more interesting example to try.
Create a new object, and put the following script into it.
default
{
state_entry()
{
llOwnerSay("I am serious");
}
on_rez(integer i)
{
llOwnerSay("rezzed serious");
}
touch_start(integer total_number)
{
llOwnerSay("changing state from serious to happy...");
state happy;
}
}
state happy
{
state_entry()
{
llOwnerSay("I am happy!");
}
on_rez(integer i)
{
llOwnerSay("rezzed happy");
}
touch_start(integer total_number)
{
llOwnerSay("changing state from happy to serious...");
state default;
}
}
this script has two states, which represent the moods - one - default - is serious, and the other is "happy". (hmm can I be happy while still being serious ? I guess so, but anyway let's assume it is not the case for training purposes :)
This script switches to another state when the object is being touched.
By playing with it, we can find few interesting things:
- the "state_entry()" gets called every time the object enters the state
- after script editing the first state is always "default"
- taking the running script into the inventory does not change its state
Why would the states be useful ? They could be helpful to alter the behaviour of the script you have - like in this case, the touch of the object causes different reactions.
Also, you can see the logic in the script quite easily. However, changing the state does reset some things, for example, listeners which are installed using llListen, so this is something to keep in mind.
Homerwork:
- modify the script such that it contains the third state, "sad", and the clicks
on the object cause transition serious->sad->happy-serious (etc). - take a look at the state_exit event, and try to put some wording into it - and see when it does get executed.
This should provide enough material to experiment with, till the next post.
2 comments:
For the homework: Should it go to state 'hangover' after state happy and then back to serious or should they be random? =d
oops, indeed I had missed the word,ty - "serious hangover" was the default state :-)
If you know how to do it random - why not :-)
Post a Comment