PackedScene.xml 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="PackedScene" inherits="Resource" 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 [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. [codeblock]
  12. # Use `load()` instead of `preload()` if the path isn't known at compile-time.
  13. var scene = preload("res://scene.tscn").instance()
  14. # Add the node as a child of the node the script is attached to.
  15. add_child(scene)
  16. [/codeblock]
  17. [b]Example of saving a node with different owners:[/b] The following example creates 3 objects: [code]Node2D[/code] ([code]node[/code]), [code]RigidBody2D[/code] ([code]rigid[/code]) and [code]CollisionObject2D[/code] ([code]collision[/code]). [code]collision[/code] is a child of [code]rigid[/code] which is a child of [code]node[/code]. Only [code]rigid[/code] is owned by [code]node[/code] and [code]pack[/code] will therefore only save those two nodes, but not [code]collision[/code].
  18. [codeblock]
  19. # Create the objects.
  20. var node = Node2D.new()
  21. var rigid = RigidBody2D.new()
  22. var collision = CollisionShape2D.new()
  23. # Create the object hierarchy.
  24. rigid.add_child(collision)
  25. node.add_child(rigid)
  26. # Change owner of `rigid`, but not of `collision`.
  27. rigid.owner = node
  28. var scene = PackedScene.new()
  29. # Only `node` and `rigid` are now packed.
  30. var result = scene.pack(node)
  31. if result == OK:
  32. var error = ResourceSaver.save("res://path/name.scn", scene) # Or "user://..."
  33. if error != OK:
  34. push_error("An error occurred while saving the scene to disk.")
  35. [/codeblock]
  36. </description>
  37. <tutorials>
  38. <link title="2D Role Playing Game Demo">https://godotengine.org/asset-library/asset/520</link>
  39. </tutorials>
  40. <methods>
  41. <method name="can_instance" qualifiers="const">
  42. <return type="bool" />
  43. <description>
  44. Returns [code]true[/code] if the scene file has nodes.
  45. </description>
  46. </method>
  47. <method name="get_state">
  48. <return type="SceneState" />
  49. <description>
  50. Returns the [code]SceneState[/code] representing the scene file contents.
  51. </description>
  52. </method>
  53. <method name="instance" qualifiers="const">
  54. <return type="Node" />
  55. <argument index="0" name="edit_state" type="int" enum="PackedScene.GenEditState" default="0" />
  56. <description>
  57. Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a [constant Node.NOTIFICATION_INSTANCED] notification on the root node.
  58. </description>
  59. </method>
  60. <method name="pack">
  61. <return type="int" enum="Error" />
  62. <argument index="0" name="path" type="Node" />
  63. <description>
  64. Pack will ignore any sub-nodes not owned by given node. See [member Node.owner].
  65. </description>
  66. </method>
  67. </methods>
  68. <members>
  69. <member name="_bundled" type="Dictionary" setter="_set_bundled_scene" getter="_get_bundled_scene" default="{&quot;conn_count&quot;: 0,&quot;conns&quot;: PoolIntArray( ),&quot;editable_instances&quot;: [ ],&quot;names&quot;: PoolStringArray( ),&quot;node_count&quot;: 0,&quot;node_paths&quot;: [ ],&quot;nodes&quot;: PoolIntArray( ),&quot;variants&quot;: [ ],&quot;version&quot;: 2}">
  70. A dictionary representation of the scene contents.
  71. 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.
  72. </member>
  73. </members>
  74. <constants>
  75. <constant name="GEN_EDIT_STATE_DISABLED" value="0" enum="GenEditState">
  76. If passed to [method instance], blocks edits to the scene state.
  77. </constant>
  78. <constant name="GEN_EDIT_STATE_INSTANCE" value="1" enum="GenEditState">
  79. If passed to [method instance], provides local scene resources to the local scene.
  80. [b]Note:[/b] Only available in editor builds.
  81. </constant>
  82. <constant name="GEN_EDIT_STATE_MAIN" value="2" enum="GenEditState">
  83. If passed to [method instance], provides local scene resources to the local scene. Only the main scene should receive the main edit state.
  84. [b]Note:[/b] Only available in editor builds.
  85. </constant>
  86. <constant name="GEN_EDIT_STATE_MAIN_INHERITED" value="3" enum="GenEditState">
  87. 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.
  88. [b]Note:[/b] Only available in editor builds.
  89. </constant>
  90. </constants>
  91. </class>