Aego Русский

Data assets

A data type registered by a native module is available to scripts by its short name.

@export
var Stats AssetRef<EnemyStats>

@export
var Loot TableRef<LootEntry>

Stats.Speed reads a field of the record the reference points to; every read sees the current record, so live tuning in the editor reaches the script on the next read. Fields cannot be assigned through a reference (AES2007). Stats.Copy() returns the record as a struct value of type data.EnemyStats that the script may change freely.

A table reference has Row(id string), Has(id string) bool and Len() int. Row returns a copy of the row.

Reading through a nil reference or asking for a missing row stops the script with AES6004.

Run it: Stats.Copy() returns a struct like EnemyStats below. The sandbox registers no data types, so the program builds the struct itself.

package demo

import "aego/log"

type EnemyStats struct {
    Speed float
    Hp    int
}

script Enemy {
    var stats EnemyStats

    func Ready() {
        stats = EnemyStats{Speed: 3, Hp: 20}
        boss := stats
        boss.Hp *= 5
        log.Info("enemy " + string(stats.Hp) + " hp, boss copy " + string(boss.Hp) + " hp")
    }

    func Update(dt float) {
        node.SetPosition(node.Position() + Vec2{stats.Speed * dt, 0})
        if node.Position().X >= 3 && stats.Speed > 0 {
            stats.Speed = 0
            log.Info("stopped at x " + string(node.Position().X))
        }
    }
}