Script events
A script method with one of these names is an event handler: the engine calls it when the event happens, it is never called directly, and it cannot be a coroutine.
Declare the method in a script; the module that owns the event calls it when the event happens. The trigger events below come from Physics 2D and need a body with a trigger collider, which the documentation sandbox does not have, so the example calls the same Collect method that OnTriggerEnter calls.
Example
package demo
import "aego/log"
script Coin {
@export var Value int = 10
var taken bool
func Ready() {
log.Info("coin ready")
}
func OnTriggerEnter(other NodeRef) {
if other.HasTag("Player") {
Collect(other)
}
}
func OnTriggerExit(other NodeRef) {
log.Info(other.Name() + " left the coin")
}
func Collect(by NodeRef) {
if taken {
return
}
taken = true
log.Info(by.Name() + " picked up " + string(Value) + " coins")
node.Destroy()
}
}
script Player {
var frames int
func Ready() {
node.AddTag("Player")
}
func Update(dt float) {
frames++
if frames == 30 {
coin := node.Find("/Coin")
if coin != nil {
coin.GetScript<Coin>().Collect(node)
}
}
if frames == 31 {
log.Info("coin left in the scene: " + string(node.Find("/Coin") != nil))
}
}
}
Functions
| OnAnimEvent | (id int, payload float) | Runs on the scripts of a node whose Animation Player reaches an event of its clip; anim.EventName(id) gives the name. |
| OnCollisionEnter | (other NodeRef, contact aego/physics.Contact) | Runs after the physics step in which this body started touching other. |
| OnCollisionExit | (other NodeRef, contact aego/physics.Contact) | Runs when the bodies stop touching, including when one of them is destroyed or disabled. Contact.Point is the last known point. |
| OnCollisionStay | (other NodeRef, contact aego/physics.Contact) | Runs after every later physics step while the bodies keep touching. It costs nothing when no script declares it. |
| OnDespawned | () | Runs on every script of a pooled copy when it is despawned, before its coroutines and connections end. |
| OnLoad | () | Runs after Ready on every script that has it when a saved game is loaded; read values with save.Get*. |
| OnSave | () | Runs on every script that has it while a game is saved; write values with save.Put*. |
| OnSettingChanged | (id string) | Runs on every script with this method after a setting's effective value changed: Apply, Revert, an override or a confirmation timeout. |
| OnSpawned | () | Runs on every script of a spawned prefab copy, after Ready for a new copy and instead of it for a copy taken from a pool. |
| OnTriggerEnter | (other NodeRef) | Runs when other's collider entered a trigger of this body, or this body entered other's trigger. |
| OnTriggerExit | (other NodeRef) | Runs when the overlap with a trigger ends. |
OnAnimEvent function
func OnAnimEvent(id int, payload float)
Runs on the scripts of a node whose Animation Player reaches an event of its clip; anim.EventName(id) gives the name.
See the module example for this function in use.
OnCollisionEnter function
func OnCollisionEnter(other NodeRef, contact aego/physics.Contact)
Runs after the physics step in which this body started touching other.
See the module example for this function in use.
OnCollisionExit function
func OnCollisionExit(other NodeRef, contact aego/physics.Contact)
Runs when the bodies stop touching, including when one of them is destroyed or disabled. Contact.Point is the last known point.
See the module example for this function in use.
OnCollisionStay function
func OnCollisionStay(other NodeRef, contact aego/physics.Contact)
Runs after every later physics step while the bodies keep touching. It costs nothing when no script declares it.
See the module example for this function in use.
OnDespawned function
func OnDespawned()
Runs on every script of a pooled copy when it is despawned, before its coroutines and connections end.
See the module example for this function in use.
OnLoad function
func OnLoad()
Runs after Ready on every script that has it when a saved game is loaded; read values with save.Get*.
See the module example for this function in use.
OnSave function
func OnSave()
Runs on every script that has it while a game is saved; write values with save.Put*.
See the module example for this function in use.
OnSettingChanged function
func OnSettingChanged(id string)
Runs on every script with this method after a setting's effective value changed: Apply, Revert, an override or a confirmation timeout.
See the module example for this function in use.
OnSpawned function
func OnSpawned()
Runs on every script of a spawned prefab copy, after Ready for a new copy and instead of it for a copy taken from a pool.
See the module example for this function in use.
OnTriggerEnter function
func OnTriggerEnter(other NodeRef)
Runs when other's collider entered a trigger of this body, or this body entered other's trigger.
See the module example for this function in use.
OnTriggerExit function
func OnTriggerExit(other NodeRef)
Runs when the overlap with a trigger ends.
See the module example for this function in use.