# aego/scene

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

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

## Example

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

## ByID

```
func ByID(id string) NodeRef
```

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

## Despawn

```
func Despawn(node NodeRef)
```

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

## Destroy

```
func Destroy(node NodeRef)
```

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

## Find

```
func Find(path string) NodeRef
```

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

## InArea

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

## Instantiate

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

## Pool

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

## Spawn

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

## SpawnAt

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

## WithComponent

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

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

## WithTag

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