Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

State Examples

Hank Ramos
Lifetime Scripter
Join date: 15 Nov 2003
Posts: 2,328
07-29-2004 06:29
There is an example "Sim Stats Display" that I use in a Scripting Class covering States that I teach. It shows a simple use of three states...

DEFAULT: for initialization, the first state that is run when script is reset.
IDLE: a state for when I want my script to sit there and do nothing, waiting for user input.
RUNNING: a state for when I want my script to be constantly displaying statistics for the sim

Simple Example

CODE

default
{
state_entry()
{
state running;
}
}

state running
{
touch_start(integer total_number)
{
state idle;
}
}

state idle
{
touch_start(integer total_number)
{
state running;
}
}


Each state is like a "mini-script" within your script. Each state can have it's own event handlers, like a unique timer event handler in two states, and no timer event in another state. They all share, however, any global variables you might use and any custom functions you declare.

States are used for organization, so you can break your script up into logical parts. You name these states or parts as you see fit, whatever makes sense to you. Remember there must always be a default state, so the script has something to run when it is reset.

Another example would be a gambling machine with the following states: default, waiting, playing, winner, loser.

DEFAULT: the first state that is run, for inititalization
WAITING: state that sits there and has a money event, waiting for somebody to pay your gambling machine.
PLAYING: state that is actively waiting to do some gambling stuff, like showing cards or rolling dice. There is no money event here, because we've already been paid and the machine is now "running". We don't want to accidentally be paid twice, so a separate state makes this easy. (i.e. since there is no money event in this state and the other non-waiting states, it's impossible to pay the machine until we get back to the waiting state).
WINNER: this state will flash some lights in its state_entry event, and pay out some money.
LOSER: this state will play some bad sounds in it's state_entry event, and NOT pay out money. Having it separate from the payout in the winner state, makes it easy to make sure you don't lose money!

So you sit there in the waiting state for somebody to pay you L$. Once you are paid, you switch to the playing state by calling "state playing". Once the gambling part is done, you decide whether or not they are a winner or a loser. Then switch to the appropriate state. Once the winner or loser state are done with declaring them a winner (by paying/flashing lights) or a loser (by playing a bad sound/no money), those two state would switch back to the "waiting" state for more money.

Another Example, Long
CODE

//Gambling Machine
//State Example
//by Hank Ramos
key playerID;
integer amountPaid;
integer winningAmount;

default
{
state_entry()
{
//Do some initialization here
llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
}
run_time_permissions(integer permissions)
{
//Only wait for payment if the owner agreed to pay out money
if (permissions)
{
llSay(0, "Initialized Successfully...");
state waiting;
}
}
}

state waiting
{
state_entry()
{
llSay(0, "Idle...");
}
money(key id, integer amount)
{
playerID = id;
amountPaid = amount;
state playing;
}
}

state playing
{
state_entry()
{
//Do the gambling bit in this state

//Determine if they are a winner or a loser
//Half the time pay twice the bet
//The other half, pay nothing.
if (llFrand(1) >= 0.5)
{
winningAmount = amountPaid * 2;
state winner;
}
else
{
state loser;
}
}
}

state winner
{
state_entry()
{

llSay(0, "You won L$" + (string)winningAmount + "!");
llGiveMoney(playerID, winningAmount);
state waiting;
}
}

state loser
{
state_entry()
{
llSay(0, "Sorry, you lose.");
state waiting;
}
}



Goto the University of SL library, and get a copy of the objects and scripts mentioned here so you can see the scripts and how they work. That might hopefully help
_____________________
Chance Small
Linden PITA
Join date: 30 Jul 2003
Posts: 170
07-31-2004 23:18
Nice examples