PackedScene

package
v0.0.0-...-e1beaa7 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

A simplified interface to a scene file. Provides access to operations and checks that can be performed on the scene resource itself.

Can be used to save a node to a file. When saving, the node as well as all the nodes it owns get saved (see graphics.gd/classdb/Node.Instance.Owner property).

Note: The node doesn't need to own itself.

Example: Load a saved scene:

package main

import (
	"graphics.gd/classdb/Node"
	"graphics.gd/classdb/PackedScene"
	"graphics.gd/classdb/Resource"
)

func ExamplePackedSceneLoad(parent Node.Instance) {
	var scene = Resource.Load[PackedScene.Instance]("res://scene.tscn").Instantiate()
	parent.AddChild(scene)
}

Example: Save a node with different owners. The following example creates 3 objects: graphics.gd/classdb/Node2D (node), graphics.gd/classdb/RigidBody2D (body) and graphics.gd/classdb/CollisionObject2D (collision). collision is a child of body which is a child of node. Only body is owned by node and Instance.Pack will therefore only save those two nodes, but not collision.

package main

import (
	"graphics.gd/classdb/CollisionShape2D"
	"graphics.gd/classdb/Engine"
	"graphics.gd/classdb/Node2D"
	"graphics.gd/classdb/PackedScene"
	"graphics.gd/classdb/ResourceSaver"
	"graphics.gd/classdb/RigidBody2D"
)

func ExamplePackedSceneSave() {
	var node = Node2D.New()
	var body = RigidBody2D.New()
	var collision = CollisionShape2D.New()
	body.AsNode().AddChild(collision.AsNode())
	node.AsNode().AddChild(body.AsNode())
	body.AsNode().SetOwner(node.AsNode()) // Change owner of `body`, but not of `collision`.
	var scene = PackedScene.New()
	var result = scene.Pack(node.AsNode()) // Only `node` and `body` are now packed.
	if result == nil {
		var err = ResourceSaver.Save(scene.AsResource(), "res://path/name.tscn", 0) // Or "user://..."
		if err != nil {
			Engine.Raise(err)
		}
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Instantiate

func Instantiate[T Node.Any](packed_scene Instance) T

Instantiate an Instance of a PackedScene as an object of type T. panics if the object is not of the expected type.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsPackedScene() Instance
}

type Expanded

type Expanded [1]gdclass.PackedScene

func (Expanded) Instantiate

func (self Expanded) Instantiate(edit_state GenEditState) Node.Instance

Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [Node.NotificationSceneInstantiated] notification on the root node.

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsPackedScene

func (self *Extension[T]) AsPackedScene() Instance

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

func (*Extension[T]) AsResource

func (self *Extension[T]) AsResource() Resource.Instance

type GenEditState

type GenEditState int //gd:PackedScene.GenEditState
const (
	// If passed to [Instance.Instantiate], blocks edits to the scene state.
	GenEditStateDisabled GenEditState = 0
	// If passed to [Instance.Instantiate], provides local scene resources to the local scene.
	//
	// Note: Only available in editor builds.
	GenEditStateInstance GenEditState = 1
	// If passed to [Instance.Instantiate], provides local scene resources to the local scene. Only the main scene should receive the main edit state.
	//
	// Note: Only available in editor builds.
	GenEditStateMain GenEditState = 2
	// It's similar to [GenEditStateMain], but for the case where the scene is being instantiated to be the base of another one.
	//
	// Note: Only available in editor builds.
	GenEditStateMainInherited GenEditState = 3
)

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.PackedScene

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsPackedScene

func (self Instance) AsPackedScene() Instance

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) AsResource

func (self Instance) AsResource() Resource.Instance

func (Instance) CanInstantiate

func (self Instance) CanInstantiate() bool

Returns true if the scene file has nodes.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) Instantiate

func (self Instance) Instantiate() Node.Instance

Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [Node.NotificationSceneInstantiated] notification on the root node.

func (Instance) Pack

func (self Instance) Pack(path Node.Instance) error

Packs the 'path' node, and all owned sub-nodes, into this graphics.gd/classdb/PackedScene. Any existing data will be cleared. See graphics.gd/classdb/Node.Instance.Owner.

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type Is

type Is[T Node.Any] [1]gdclass.PackedScene

Is wraps a PackedScene with a type.

func (Is[T]) AsObject

func (self Is[T]) AsObject() [1]gd.Object

func (Is[T]) AsPackedScene

func (self Is[T]) AsPackedScene() Instance

func (Is[T]) AsRefCounted

func (self Is[T]) AsRefCounted() [1]gd.RefCounted

func (Is[T]) AsResource

func (self Is[T]) AsResource() Resource.Instance

func (Is[T]) CanInstantiate

func (self Is[T]) CanInstantiate() bool

Returns [code]true[/code] if the scene file has nodes.

func (Is[T]) GetState

func (self Is[T]) GetState() [1]gdclass.SceneState

Returns the [SceneState] representing the scene file contents.

func (Is[T]) Instantiate

func (self Is[T]) Instantiate() T

Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [constant Node.NOTIFICATION_SCENE_INSTANTIATED] notification on the root node.

func (Is[T]) Pack

func (self Is[T]) Pack(path T) error

Packs the [param path] node, and all owned sub-nodes, into this [PackedScene]. Any existing data will be cleared. See [member Node.owner].

func (*Is[T]) SetObject

func (self *Is[T]) SetObject(obj [1]gd.Object) bool

func (*Is[T]) UnsafePointer

func (self *Is[T]) UnsafePointer() unsafe.Pointer

func (Is[T]) Virtual

func (self Is[T]) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

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