spawn

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package spawn provides the polymorphic Add verb — one call to parent any of {Go component pointer, PackedScene.Instance, "res://..."} into a node and optionally position it. Plus AddNew for fresh stock upstream nodes.

spawn.Add(parent, NewEnemy(), pos)              // Go component
spawn.Add(parent, bulletScene, pos)             // PackedScene.Instance
spawn.Add(parent, "res://enemies/slime.tscn", pos)  // string path

t := spawn.AddNew(parent, Timer.New)
t.SetWaitTime(0.5)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(parent Node.Instance, thing any, pos gd.Vec2) Node.Instance

Add parents `thing` under parent, setting its 2D global position to pos if it's a Node2D. Accepts:

  • *T where T is a registered component (e.g. *Enemy): parented directly
  • [PackedScene.Instance]: instantiated, parented
  • string: treated as a res:// path; the PackedScene is loaded (without Go-side caching — Godot's ResourceLoader caches), instantiated, and parented

Returns the spawned Node.Instance — re-fetched after AddChild because the original handle is invalidated by Godot's ownership transfer.

Use Add3 for 3D positioning.

func Add3

func Add3(parent Node.Instance, thing any, pos gd.Vec3) Node.Instance

Add3 is the 3D counterpart to Add. Same accepted forms; pos applies as 3D global position if the spawned node is a Node3D.

func AddChild

func AddChild(parent Node.Instance, thing any) Node.Instance

AddChild parents thing under parent without changing its position. Use when the spawned node positions itself in Ready, or when the position will be set later from authored data.

func AddNew

func AddNew[T interface{ AsNode() Node.Instance }](parent Node.Instance, newFn func() T) T

AddNew constructs a fresh node via the given upstream class constructor and adds it as a child of parent. Returns the original constructed value — note that after AddChild, the local handle to the node is technically invalidated by Godot's ownership transfer (POC #6 trap), so use the return value for *configuration* (which happens before the next frame) and then drop the reference.

t := spawn.AddNew(parent, Timer.New)
t.SetWaitTime(0.5)  // safe — happens this frame
t.OnTimeout(...)    // safe — happens this frame
// Don't stash `t` in a field expecting it to stay valid.

Takes the constructor explicitly because Go's generic constraints can't express "type T whose package has a New() T" without codegen.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL