PackedScene.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="PackedScene" inherits="Resource" version="4.0">
  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 [code]owner[/code] property on [Node]).
  9. [b]Note:[/b] The node doesn't need to own itself.
  10. [b]Example of loading a saved scene:[/b]
  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 of saving a node with different owners:[/b] The following example creates 3 objects: [code]Node2D[/code] ([code]node[/code]), [code]RigidDynamicBody2D[/code] ([code]body[/code]) and [code]CollisionObject2D[/code] ([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 [code]pack[/code] 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 = RigidDynamicBody2D.new()
  31. var collision = CollisionShape2D.new()
  32. # Create the object hierarchy.
  33. body.add_child(collision)
  34. node.add_child(body)
  35. # Change owner of `body`, but not of `collision`.
  36. body.owner = node
  37. var scene = PackedScene.new()
  38. # Only `node` and `body` are now packed.
  39. var result = scene.pack(node)
  40. if result == OK:
  41. var error = ResourceSaver.save("res://path/name.tscn", scene) # Or "user://..."
  42. if error != OK:
  43. push_error("An error occurred while saving the scene to disk.")
  44. [/gdscript]
  45. [csharp]
  46. // Create the objects.
  47. var node = new Node2D();
  48. var body = new RigidDynamicBody2D();
  49. var collision = new CollisionShape2D();
  50. // Create the object hierarchy.
  51. body.AddChild(collision);
  52. node.AddChild(body);
  53. // Change owner of `body`, but not of `collision`.
  54. body.Owner = node;
  55. var scene = new PackedScene();
  56. // Only `node` and `body` are now packed.
  57. Error result = scene.Pack(node);
  58. if (result == Error.Ok)
  59. {
  60. Error error = ResourceSaver.Save("res://path/name.tscn", scene); // Or "user://..."
  61. if (error != Error.Ok)
  62. {
  63. GD.PushError("An error occurred while saving the scene to disk.");
  64. }
  65. }
  66. [/csharp]
  67. [/codeblocks]
  68. </description>
  69. <tutorials>
  70. <link title="2D Role Playing Game Demo">https://godotengine.org/asset-library/asset/520</link>
  71. </tutorials>
  72. <methods>
  73. <method name="can_instantiate" qualifiers="const">
  74. <return type="bool" />
  75. <description>
  76. Returns [code]true[/code] if the scene file has nodes.
  77. </description>
  78. </method>
  79. <method name="get_state">
  80. <return type="SceneState" />
  81. <description>
  82. Returns the [code]SceneState[/code] representing the scene file contents.
  83. </description>
  84. </method>
  85. <method name="instantiate" qualifiers="const">
  86. <return type="Node" />
  87. <argument index="0" name="edit_state" type="int" enum="PackedScene.GenEditState" default="0" />
  88. <description>
  89. Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [constant Node.NOTIFICATION_INSTANCED] notification on the root node.
  90. </description>
  91. </method>
  92. <method name="pack">
  93. <return type="int" enum="Error" />
  94. <argument index="0" name="path" type="Node" />
  95. <description>
  96. Pack will ignore any sub-nodes not owned by given node. See [member Node.owner].
  97. </description>
  98. </method>
  99. </methods>
  100. <members>
  101. <member name="_bundled" type="Dictionary" setter="_set_bundled_scene" getter="_get_bundled_scene" default="{&quot;conn_count&quot;: 0,&quot;conns&quot;: PackedInt32Array(),&quot;editable_instances&quot;: [],&quot;names&quot;: PackedStringArray(),&quot;node_count&quot;: 0,&quot;node_paths&quot;: [],&quot;nodes&quot;: PackedInt32Array(),&quot;variants&quot;: [],&quot;version&quot;: 2}">
  102. A dictionary representation of the scene contents.
  103. Available keys include "rnames" and "variants" for resources, "node_count", "nodes", "node_paths" for nodes, "editable_instances" for base scene children overrides, "conn_count" and "conns" for signal connections, and "version" for the format style of the PackedScene.
  104. </member>
  105. </members>
  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>