Real code
This is a whole script
A coin from the demo platformer. It bobs, it notices the player, it adds to the score in a global and plays a sound. The value field shows up in the inspector. No boilerplate, no build files: save it and press Play.
More examples →scripts/coin.odinOdin
package scripts
import "core:math"
import e "engine:."
import sdl "vendor:sdl3"
// A coin: an Area with a sprite. Touch it to collect it.
Coin :: struct {
using script: e.Script,
// Set per coin in the inspector (or from an LDtk field)
value: int `export:"default=1 min=1"`,
sprite: ^e.Node,
time: f32,
taken: bool,
}
@(init)
register_Coin :: proc "contextless" () {
e.Script_Register(Coin, {
ready = proc(self: ^Coin) {
self.sprite = e.Node_FindChild(self.node, "Sprite")
self.time = self.node.position.x * 0.05
},
update = proc(self: ^Coin, _: []sdl.Event, deltaTime: f64) {
self.time += f32(deltaTime)
if self.sprite != nil do self.sprite.position.y = math.sin(self.time * 4) * 1.5
},
collisionEnter = proc(self: ^Coin, hit: ^e.Collision) {
if self.taken || !e.Node_Is(hit.other, e.CharacterMover) do return
self.taken = true
if state := e.Global(GameState); state != nil do state.coins += self.value
e.Game_PlaySound(self.game, SOUND_COIN)
e.Scene_RemoveNode(self.scene, self.node.id)
},
})
}