Default

From InWorldz Wiki
Jump to: navigation, search

The default state is the only required code in a script. When a script runs, it can exist in one or more states, or contexts. While most scripts only exist in the default state, it is possible to define a collection of different states, each with their own event responders within that state.

When a script is compiled, reset or loaded, this is the state it enters by default. A script will not automatically re-enter the default state's state_entry event when the task is rezzed or attached (even by a new owner), nor if the task is moved to another region, nor on region restart.

It most often follows global variable and functions declarations. In the example below, it follows the global declaration for the string named "currentState".

The following example says a chat message using llSay every time it enters a new state, including the default state, and also when a user clicks on the object containing this script. It also performs a state change on each click, toggling between the default state and a user-defined "clicked" state.

string currentState;

default
{
    state_entry()
    {
        // some code that runs when the script enters the "default" state
        currentState = "default";
        llSay(0, "Now in the " + currentState + " state.");
    }
 
    touch_start(integer num)
    {
        // some code that runs when the user clicks on the object
        llSay(0, "Hello from the " + currentState + " state.");

        // now that the user has clicked, go to the clicked state
        state clicked;
    }
}  // end of default state

state clicked
{
    state_entry()
    {
        // some code that runs when the script enters this "clicked" state
        currentState = "clicked";
        llSay(0, "Now in the " + currentState + " state.");
    }

    touch_start(integer num)
    {
        // some code that runs when the user clicks on the object
        llSay(0, "Hello from the " + currentState + " state.");

        // now that the user has clicked again, return to the default state
        state default;
    }
}  // end of clicked state