Aego Русский

aego/scene

Find takes a path from the scene root. The With* and InArea functions fill a slice you pass, never more than its length, and return how many nodes they wrote. New nodes and destroyed nodes take effect at the end of the phase. Spawn, SpawnAt, Pool and Despawn come from the Spawning module, which the documentation sandbox does not load; Instantiate needs a prefab assigned in the Inspector, so the example skips it when the field is empty.

On this page

Example

package demo

import (
    "aego/log"
    "aego/scene"
)

script Crate {
    func Ready() {
        node.SetPosition(Vec2{1, 0})
        node.AddTag("Breakable")
    }
}

script Level {
    @export var Coin AssetRef<Prefab>

    var found []NodeRef
    var frames int

    func Ready() {
        for i := 0; i < 8; i++ {
            found = append(found, nil)
        }
        if Coin != nil {
            coin := scene.Instantiate(Coin, node)
            coin.SetPosition(Vec2{0, -2})
        } else {
            log.Info("Coin prefab is not set, nothing to spawn")
        }
    }

    func Update(dt float) {
        frames++
        if frames == 1 {
            n := scene.WithComponent("SpriteRenderer", found)
            log.Info(string(n) + " nodes with a SpriteRenderer")
            n = scene.WithTag("Breakable", found)
            log.Info(string(n) + " breakable, first is " + found[0].Name())
            n = scene.InArea(Rect{0.5, -0.5, 1, 1}, -1, found)
            log.Info(string(n) + " node in the area right of the origin")
        }
        if frames == 60 {
            crate := scene.Find("Crate")
            if crate != nil {
                log.Info("destroying " + crate.Name())
                scene.Destroy(crate)
            }
        }
        if frames == 61 {
            log.Info("Crate still found: " + string(scene.Find("Crate") != nil))
        }
    }
}

Functions

ByID(id string) NodeRefReturns the node with this id, as written in the scene file, or nil.
Despawn(node NodeRef)Returns a spawned copy to its pool; it destroys a node that did not come from a pool.
Destroy(node NodeRef)Removes a node and its children at the end of the phase.
Find(path string) NodeRefReturns the node at a path from the scene root, or nil.
InArea(rect Rect, mask int, out []NodeRef) intWrites the bodies touching the rectangle on the layers in mask into out. Without physics it writes every enabled 2D node whose position lies in the rectangle, ignoring mask.
Instantiate(prefab AssetRef<prefab>, parent NodeRef) NodeRefCreates a copy of a prefab under parent, or under the root when parent is nil.
Pool(prefab AssetRef<prefab>, count int)Makes n copies of a prefab ahead of time; their Ready runs now and Spawn takes them later.
Spawn(prefab AssetRef<prefab>, position Vec2) NodeRefPlaces a copy of a prefab at a position under the scene root, taken from its pool when one is free.
SpawnAt(prefab AssetRef<prefab>, parent NodeRef, position Vec2, rotation float) NodeRefPlaces a copy of a prefab under parent with a local position and rotation.
WithComponent(component string, out []NodeRef) intWrites enabled nodes that have the component into out and returns the count.
WithTag(tag string, out []NodeRef) intWrites enabled nodes with the tag into out and returns the count; Enemy also finds Enemy.Flying. Nodes come in the order they got the tag.

ByID function

func ByID(id string) NodeRef

Returns the node with this id, as written in the scene file, or nil.

See the module example for this function in use.

Despawn function

func Despawn(node NodeRef)

Returns a spawned copy to its pool; it destroys a node that did not come from a pool.

Example

scene.Despawn(node)

Destroy function

func Destroy(node NodeRef)

Removes a node and its children at the end of the phase.

Example

scene.Destroy(Target)

Find function

func Find(path string) NodeRef

Returns the node at a path from the scene root, or nil.

Example

spawn := scene.Find("Level/Spawn")

InArea function

func InArea(rect Rect, mask int, out []NodeRef) int

Writes the bodies touching the rectangle on the layers in mask into out. Without physics it writes every enabled 2D node whose position lies in the rectangle, ignoring mask.

Example

n := scene.InArea(Rect{x - 2, y - 2, 4, 4}, -1, near)

Instantiate function

func Instantiate(prefab AssetRef<prefab>, parent NodeRef) NodeRef

Creates a copy of a prefab under parent, or under the root when parent is nil. The new nodes appear at the end of the phase; their scripts get Ready before the next Update.

Example

b := scene.Instantiate(Bullet, nil)
b.SetPosition(node.WorldPosition())

Pool function

func Pool(prefab AssetRef<prefab>, count int)

Makes n copies of a prefab ahead of time; their Ready runs now and Spawn takes them later.

Example

scene.Pool(Bullet, 32)

Spawn function

func Spawn(prefab AssetRef<prefab>, position Vec2) NodeRef

Places a copy of a prefab at a position under the scene root, taken from its pool when one is free.

Example

b := scene.Spawn(Bullet, node.WorldPosition())

SpawnAt function

func SpawnAt(prefab AssetRef<prefab>, parent NodeRef, position Vec2, rotation float) NodeRef

Places a copy of a prefab under parent with a local position and rotation.

Example

fx := scene.SpawnAt(Spark, node, Vec2{}, 0)

WithComponent function

func WithComponent(component string, out []NodeRef) int

Writes enabled nodes that have the component into out and returns the count.

Example

n := scene.WithComponent("Body2D", bodies)

WithTag function

func WithTag(tag string, out []NodeRef) int

Writes enabled nodes with the tag into out and returns the count; Enemy also finds Enemy.Flying. Nodes come in the order they got the tag.

Example

n := scene.WithTag("Enemy", enemies)