# Script events

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

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

```aes run
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))
        }
    }
}
```

## OnAnimEvent

```
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.

## OnCollisionEnter

```
func OnCollisionEnter(other NodeRef, contact aego/physics.Contact)
```

Runs after the physics step in which this body started touching other.

## OnCollisionExit

```
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.

## OnCollisionStay

```
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.

## OnDespawned

```
func OnDespawned()
```

Runs on every script of a pooled copy when it is despawned, before its coroutines and connections end.

## OnLoad

```
func OnLoad()
```

Runs after Ready on every script that has it when a saved game is loaded; read values with save.Get*.

## OnSave

```
func OnSave()
```

Runs on every script that has it while a game is saved; write values with save.Put*.

## OnSettingChanged

```
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.

## OnSpawned

```
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.

## OnTriggerEnter

```
func OnTriggerEnter(other NodeRef)
```

Runs when other's collider entered a trigger of this body, or this body entered other's trigger.

## OnTriggerExit

```
func OnTriggerExit(other NodeRef)
```

Runs when the overlap with a trigger ends.
