Class PackedScene
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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 Owner property).
Note: The node doesn't need to own itself.
Example of loading a saved scene:
// C# has no preload, so you have to always use ResourceLoader.Load<PackedScene>().
var scene = ResourceLoader.Load<PackedScene>("res://scene.tscn").Instantiate();
// Add the node as a child of the node the script is attached to.
AddChild(scene);
Example of saving a node with different owners: The following example creates 3 objects: Node2D (node
), RigidBody2D (body
) and CollisionObject2D (collision
). collision
is a child of body
which is a child of node
. Only body
is owned by node
and Pack(Node) will therefore only save those two nodes, but not collision
.
// Create the objects.
var node = new Node2D();
var body = new RigidBody2D();
var collision = new CollisionShape2D();
// Create the object hierarchy.
body.AddChild(collision);
node.AddChild(body);
// Change owner of body
, but not of collision
.
body.Owner = node;
var scene = new PackedScene();
// Only node
and body
are now packed.
Error result = scene.Pack(node);
if (result == Error.Ok)
{
Error error = ResourceSaver.Save(scene, "res://path/name.tscn"); // Or "user://..."
if (error != Error.Ok)
{
GD.PushError("An error occurred while saving the scene to disk.");
}
}
public class PackedScene : Resource, IDisposable
- Inheritance
-
PackedScene
- Implements
- Inherited Members
Constructors
PackedScene()
public PackedScene()
Properties
_Bundled
A dictionary representation of the scene contents.
Available keys include "rnames" and "variants" for resources, "node_count", "nodes", "node_paths" for nodes, "editable_instances" for paths to overridden nodes, "conn_count" and "conns" for signal connections, and "version" for the format style of the PackedScene.
public Dictionary _Bundled { get; set; }
Property Value
Methods
CanInstantiate()
Returns true
if the scene file has nodes.
public bool CanInstantiate()
Returns
GetState()
Returns the SceneState representing the scene file contents.
public SceneState GetState()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
Instantiate(GenEditState)
Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a NotificationSceneInstantiated notification on the root node.
public Node Instantiate(PackedScene.GenEditState editState = GenEditState.Disabled)
Parameters
editState
PackedScene.GenEditState
Returns
InstantiateOrNull<T>(GenEditState)
Instantiates the scene's node hierarchy, returning null on failure. Triggers child scene instantiation(s). Triggers a NotificationSceneInstantiated notification on the root node.
public T InstantiateOrNull<T>(PackedScene.GenEditState editState = GenEditState.Disabled) where T : class
Parameters
editState
PackedScene.GenEditState
Returns
- T
The instantiated scene.
Type Parameters
T
The type to cast to. Should be a descendant of Node.
- See Also
Instantiate<T>(GenEditState)
Instantiates the scene's node hierarchy, erroring on failure. Triggers child scene instantiation(s). Triggers a NotificationSceneInstantiated notification on the root node.
public T Instantiate<T>(PackedScene.GenEditState editState = GenEditState.Disabled) where T : class
Parameters
editState
PackedScene.GenEditState
Returns
- T
The instantiated scene.
Type Parameters
T
The type to cast to. Should be a descendant of Node.
Exceptions
- InvalidCastException
The instantiated node can't be casted to the given type
T
.
- See Also
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
Pack(Node)
Pack will ignore any sub-nodes not owned by given node. See Owner.
public Error Pack(Node path)
Parameters
path
Node