Last updated
Last updated
In programming, an event is an action that occurs because of the user or another source, such as a click. An event handler is a that deals with the event, allowing you to write code that executes when the event occurs.
Events are actions that occur based on the player’s input and the state of other events in the game.
They make it easy to have things happen at the exact time the player has pressed a button, with the ease of not having to check for a button press, and the conditions associated with it everywhere.
In FP Motion, there are three types of events, and we use each type for different things; What they all have in common is that they react to input and then pass that to other components as requested.
- Action. We use actions for things that have a ‘state’, meaning things that can be on or off. For example, the player can aim, or not, making aiming an action.
- Attempt. Attempts are good for things that don’t have states, a good example would be attempting to open a door, no states, but you can succeed or fail.
- Value. Values are an event that you can use to store values.
We then use these three types of events as variables for the different things that the player can perform, and store them inside of the component, which also holds the logic for when to use them.
By default, destroyed game-objects don’t receive events. The reason behind that is that it would cause a lot of errors with the engine.
Inactive objects don’t receive events either, as usually there is no need for them to do so; However, sometimes they need to receive them, that is what persistent events are for.
Persistent events allow inactive objects to have their methods stay subscribed and to keep reacting to event changes; You can make any event type persistent by using the persistent variation.
Variations are: PersistentAction, PersistentAttempt and PersistentValue.
The best way to use events is to subscribe methods to their changes, be that when they start, stop, or toggle.
If there is a need to know the state of an event while in an Update method, a developer can check for the ‘State’ property; keep in mind that this property is only available for Actions.
The usage for Actions, Attempts and Values is similar but not entirely the same; To use the different types of events: