Skip to content
0.2.0-dev Download ZIP
123 lines ·5.1 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="PackedScene" inherits="Resource" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 An abstraction of a serialized scene.
5 </brief_description>
6 <description>
7 A simplified interface to a scene file. Provides access to operations and checks that can be performed on the scene resource itself.
8 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 [member Node.owner] property).
9 [b]Note:[/b] The node doesn't need to own itself.
10 [b]Example:[/b] Load a saved scene:
11 [codeblocks]
12 [gdscript]
13 # Use load() instead of preload() if the path isn't known at compile-time.
14 var scene = preload("res://scene.tscn").instantiate()
15 # Add the node as a child of the node the script is attached to.
16 add_child(scene)
17 [/gdscript]
18 [csharp]
19 // C# has no preload, so you have to always use ResourceLoader.Load&lt;PackedScene&gt;().
20 var scene = ResourceLoader.Load&lt;PackedScene&gt;("res://scene.tscn").Instantiate();
21 // Add the node as a child of the node the script is attached to.
22 AddChild(scene);
23 [/csharp]
24 [/codeblocks]
25 [b]Example:[/b] Save a node with different owners. The following example creates 3 objects: [Node2D] ([code]node[/code]), [RigidBody2D] ([code]body[/code]) and [CollisionObject2D] ([code]collision[/code]). [code]collision[/code] is a child of [code]body[/code] which is a child of [code]node[/code]. Only [code]body[/code] is owned by [code]node[/code] and [method pack] will therefore only save those two nodes, but not [code]collision[/code].
26 [codeblocks]
27 [gdscript]
28 # Create the objects.
29 var node = Node2D.new()
30 var body = RigidBody2D.new()
31 var collision = CollisionShape2D.new()
32
33 # Create the object hierarchy.
34 body.add_child(collision)
35 node.add_child(body)
36
37 # Change owner of `body`, but not of `collision`.
38 body.owner = node
39 var scene = PackedScene.new()
40
41 # Only `node` and `body` are now packed.
42 var result = scene.pack(node)
43 if result == OK:
44 var error = ResourceSaver.save(scene, "res://path/name.tscn") # Or "user://..."
45 if error != OK:
46 push_error("An error occurred while saving the scene to disk.")
47 [/gdscript]
48 [csharp]
49 // Create the objects.
50 var node = new Node2D();
51 var body = new RigidBody2D();
52 var collision = new CollisionShape2D();
53
54 // Create the object hierarchy.
55 body.AddChild(collision);
56 node.AddChild(body);
57
58 // Change owner of `body`, but not of `collision`.
59 body.Owner = node;
60 var scene = new PackedScene();
61
62 // Only `node` and `body` are now packed.
63 Error result = scene.Pack(node);
64 if (result == Error.Ok)
65 {
66 Error error = ResourceSaver.Save(scene, "res://path/name.tscn"); // Or "user://..."
67 if (error != Error.Ok)
68 {
69 GD.PushError("An error occurred while saving the scene to disk.");
70 }
71 }
72 [/csharp]
73 [/codeblocks]
74 </description>
75 <tutorials>
76 <link title="2D Role Playing Game (RPG) Demo">https://godotengine.org/asset-library/asset/2729</link>
77 </tutorials>
78 <methods>
79 <method name="can_instantiate" qualifiers="const">
80 <return type="bool" />
81 <description>
82 Returns [code]true[/code] if the scene file has nodes.
83 </description>
84 </method>
85 <method name="get_state" qualifiers="const">
86 <return type="SceneState" />
87 <description>
88 Returns the [SceneState] representing the scene file contents.
89 </description>
90 </method>
91 <method name="instantiate" qualifiers="const" keywords="create, make, spawn, new">
92 <return type="Node" />
93 <param index="0" name="edit_state" type="int" enum="PackedScene.GenEditState" default="0" />
94 <description>
95 Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [constant Node.NOTIFICATION_SCENE_INSTANTIATED] notification on the root node.
96 </description>
97 </method>
98 <method name="pack">
99 <return type="int" enum="Error" />
100 <param index="0" name="path" type="Node" />
101 <description>
102 Packs the [param path] node, and all owned sub-nodes, into this [PackedScene]. Any existing data will be cleared. See [member Node.owner].
103 </description>
104 </method>
105 </methods>
106 <constants>
107 <constant name="GEN_EDIT_STATE_DISABLED" value="0" enum="GenEditState">
108 If passed to [method instantiate], blocks edits to the scene state.
109 </constant>
110 <constant name="GEN_EDIT_STATE_INSTANCE" value="1" enum="GenEditState">
111 If passed to [method instantiate], provides local scene resources to the local scene.
112 [b]Note:[/b] Only available in editor builds.
113 </constant>
114 <constant name="GEN_EDIT_STATE_MAIN" value="2" enum="GenEditState">
115 If passed to [method instantiate], provides local scene resources to the local scene. Only the main scene should receive the main edit state.
116 [b]Note:[/b] Only available in editor builds.
117 </constant>
118 <constant name="GEN_EDIT_STATE_MAIN_INHERITED" value="3" enum="GenEditState">
119 It's similar to [constant GEN_EDIT_STATE_MAIN], but for the case where the scene is being instantiated to be the base of another one.
120 [b]Note:[/b] Only available in editor builds.
121 </constant>
122 </constants>
123</class>