The tutorial assumes that you are somewhat familiar with the State Machine, so you might want to have a look at the State Machine tutorial first.
In this tutorial we will make a Box entity rotate when we click a Sphere.
If you’d like to follow the video tutorial, click play below:
Before we start: What is an event?
A basic event consists of a name. This name is used to indicate that something happened. For example, if you make a button entity, you can make it emit a
"buttonClicked" event when it’s clicked. Other entities can listen to this event and react to it as they want.
An event can also carry more information about the event, such as which or how button was clicked. But for most applications, the event name is good enough.
In our tutorial we will use a
"sphereClicked" event, to indicate when the sphere is clicked.
Set up the scene
- Go to goocreate.com and log in to Goo Create.
- From the Dashboard, create a new Empty scene.
- Open the Create dialog by clicking Create at the top.
- Choose “Sphere”.
- Create a Box by repeating steps 3 and 4, but instead choosing “Box”.
- Position the box and sphere so they don’t overlap. Use either the Transform panel or mouse drag the gizmos in the viewport.
When you’re done, your scene will look something like this:
Emitting an event from the Sphere
You can use the State machine to emit an event. Add the “Emit Message” action to a state and it will emit the message when the State is entered.
To emit an event in a Script, you would use goo.SystemBus.emit. This is not needed in our tutorial, but it’s good to know.
We want the Sphere to emit a
"sphereClicked" event when it is clicked, so we do the following:
- Add a State Machine Component to the sphere.
- Add a Behavior to the State Machine. Name it “Emit on click”.
- Open the Behavior, and rename the initial state to “Listen for pick”.
- Add a Pick action to the initial state.
- Create a second state, name it “Emit Event”.
- Add an Emit action to the second state, and set the channel name to “sphereClicked”.
- Connect the “to” transition of the Pick action to the second state.
The result should look like this:
Listening for an event in the Box
Any script or state in the State Machine can listen to events, and do whatever they want to react to it.
To listen to an event using the State Machine, simply use the Listen action. Make sure to specify the event name correctly:
The Listen action has a “to” transition, which you can use to transition when the event happens. We will create a new State, and connect it here. In the new State, we want the box to rotate, so we add a Rotate action.
To listen to events in Scripts, you would use the goo.SystemBus.addListener method. We won’t do this in this tutorial, but now you know.
You can now press Play and try to click the sphere. When clicked, it will emit the
"sphereClicked" event. The Box will react to the event, and start rotating.
Next step is to do more complex event systems. Or emit events from the Timeline and/or Scripts. Good luck!