Say hello
You'll learn: how a game tells your code that something happened, and how
your code can answer.
Time: 10 minutes.
Try itOpen this lesson in the Bitflinger Games app to try it.
Play it first
When the game starts, "Hello!" appears at the top of the screen. Here's the code that does it:
func _ready() -> void:
Variations.event.connect(_on_event)
func _on_event(event_name: String, payload: Dictionary) -> void:
if event_name == "world.ready":
Variations.emit_event("hud.message", {"text": "Hello!"})
Events
While a game runs, things happen: the world finishes building, the player jumps, a coin is collected. Each time, the game sends an event, a message with a name that every layer can hear.
This code does two things:
- In
_ready,Variations.event.connect(_on_event)means "whenever there's an event, run_on_event". _on_eventis a function of your own: a named piece of code that runs when something calls it. It's given the event's name and its details. When the name is"world.ready", the world has just been built, so it sends an event of its own,"hud.message", with the text to show.
Starter World listens for hud.message and shows the text. Any layer can send
it.
Change it
Change "Hello!" to your own message, such as "Find all ten coins". Keep the
quotes.
Try this
Starter World sends player.jumped every time you jump. Add a second check to
_on_event, lined up with the first if:
if event_name == "player.jumped":
Variations.emit_event("hud.message", {"text": "Boing"})
Play, and jump.
Events in Starter World
| Event | When it's sent | Details |
|---|---|---|
world.ready | The world has been built | world, player |
player.jumped | The player jumps | player, count (1, or 2 for a double jump) |
player.fell | The player fell off the world | player |
collected | A coin was picked up | item |
hud.message | Send it to show text | text |
What you learned
- A game sends events when things happen, and your layer can listen.
- A function is a named piece of code.
_on_eventruns for every event. - Your layer can send events too, and other layers can listen for them.
That's the end of Stage 1. Stage 2, building a place of your own, is coming next. Until then, the Guide shows everything a layer can do.