Events

Don’t Starve uses a special system to allow entities to communicate with each other flexibly, usually referred to as event listeners.

In its most basic definition; an event is pushed onto an entity’s event listener (using PushEvent). If any functions were previously associated with that event (using ListenForEvent), they will be executed¬†accordingly.

PushEvent

This function is used to push an event. Optionally, a table with arbitrary information can be added.

 ListenForEvent

This function is used to listen for a set event and fire a function when it’s pushed. Optionally, a different entity can be set as a third parameter. Then that entity has to push the event, rather than the entity used to call ListenForEvent in the first place (notice that you could add the listener from the source entity straight away, but then removing the first entity won’t remove the event listener).

RemoveEventCallback

This function removes an event listener. It requires both the event name and the function, and can also accept a different entity as the third parameter, if used in the original ListenForEvent.

ListenForEventOnce

This function isn’t in the game code (simplex invented it), but a creative combination of ListenForEvent and RemoveEventCallback that can be useful in many cases.

Create the function like so:

Then you can use the function ListenForEventOnce as much as you want to, the same way ListenForEvent is used.