# States and events

> The default state, state changes and the event queue, quoted from the LSL definitions and the wiki.

:::caution[Stub]
This documentation page is a stub and is currently under development.
:::

## States

From the [LSL definitions](/controls/default/): "All scripts must have a default state, which is the first state entered when the script starts. If another state is defined before the default state, the compiler will report a syntax error."

On a [state change](/controls/state/), from the wiki's [`state_entry`](/events/state_entry/) page: "A state_exit is triggered in the old state and the state_entry is triggered in the new state. All other events in the queue are lost as a result of the state change."

- [`state_exit`](/events/state_exit/): "Events queued during state_exit, are dumped before the next state is entered." and "While the default state_entry is triggered on script reset, state_exit is not triggered prior to the reset."
- The wiki's examples warn: "NEVER do a state change from within a touch_start event - that can lead to the next touch_start on return to this state to be missed."

## The event queue

From the wiki's events overview:

> Events do not interrupt each other, but instead are queued FIFO, though the state_entry event can jump the queue. If more that 64 events are waiting, new events are discarded until free slots become available. If the script is paused, such as when an object is taken to inventory, pending events are preserved and handled the next time the object is rezzed.
> On state change the event queue is cleared and any open listens are removed automatically. Reoccurring sensor events (sensor) are set not to reoccur in the new state and must be manually re-enabled (via llSensor or llSensorRepeat), but timer events (from llSetTimerEvent) do reoccur.

## Reference

- [`default`](/controls/default/), [`state`](/controls/state/)
- [`state_entry`](/events/state_entry/), [`state_exit`](/events/state_exit/), [`on_rez`](/events/on_rez/)
- [All events](/events/)
- [`llResetScript`](/functions/llResetScript/), [`llListenRemove`](/functions/llListenRemove/), [`llSensorRepeat`](/functions/llSensorRepeat/), [`llSetTimerEvent`](/functions/llSetTimerEvent/)

*Quotations are from the Second Life Wiki pages [Category:LSL Events](https://wiki.secondlife.com/wiki/Category:LSL_Events), [State entry](https://wiki.secondlife.com/wiki/State_entry), [State exit](https://wiki.secondlife.com/wiki/State_exit), by Second Life Wiki contributors, under [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/).*

---

From lsl.dev: https://lsl.dev/fundamentals/states-and-events/
