mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-04 14:11:02 +03:00
Updating change_scene and change_scene_to (#6175)
* Updating change_scene and chnage_scene_to solves #6156
This commit is contained in:
@@ -6,7 +6,7 @@ Autoloads versus regular nodes
|
||||
Godot offers a feature to automatically load nodes at the root of your project,
|
||||
allowing you to access them globally, that can fulfill the role of a Singleton:
|
||||
:ref:`doc_singletons_autoload`. These auto-loaded nodes are not freed when you
|
||||
change the scene from code with :ref:`SceneTree.change_scene <class_SceneTree_method_change_scene>`.
|
||||
change the scene from code with :ref:`SceneTree.change_scene_to_file <class_SceneTree_method_change_scene_to_file>`.
|
||||
|
||||
In this guide, you will learn when to use the Autoload feature, and techniques
|
||||
you can use to avoid it.
|
||||
|
||||
@@ -294,7 +294,7 @@ If one has a system that...
|
||||
|
||||
For smaller games, a simpler alternative with less control would be to have
|
||||
a "Game" singleton that simply calls the
|
||||
:ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>` method
|
||||
:ref:`SceneTree.change_scene_to_file() <class_SceneTree_method_change_scene_to_file>` method
|
||||
to swap out the main scene's content. This structure more or less keeps
|
||||
the "World" as the main game node.
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ balancing operation speed and memory consumption as well as balancing data
|
||||
access and integrity.
|
||||
|
||||
1. **We can delete the existing scene.**
|
||||
:ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>` and
|
||||
:ref:`SceneTree.change_scene_to() <class_SceneTree_method_change_scene_to>`
|
||||
:ref:`SceneTree.change_scene_to_file() <class_SceneTree_method_change_scene_to_file>` and
|
||||
:ref:`SceneTree.change_scene_to_packed() <class_SceneTree_method_change_scene_to_packed>`
|
||||
will delete the current scene immediately. Developers can also delete the
|
||||
main scene though. Assuming the root node's name is "Main", one could do
|
||||
``get_node("/root/Main").free()`` to delete the whole scene.
|
||||
|
||||
@@ -141,14 +141,14 @@ Changing current scene
|
||||
|
||||
After a scene is loaded, you may want to change this scene for
|
||||
another one. One way to do this is to use the
|
||||
:ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>`
|
||||
:ref:`SceneTree.change_scene_to_file() <class_SceneTree_method_change_scene_to_file>`
|
||||
function:
|
||||
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
func _my_level_was_completed():
|
||||
get_tree().change_scene("res://levels/level2.tscn")
|
||||
get_tree().change_scene_to_file("res://levels/level2.tscn")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
@@ -160,7 +160,7 @@ function:
|
||||
Rather than using file paths, one can also use ready-made
|
||||
:ref:`PackedScene <class_PackedScene>` resources using the equivalent
|
||||
function
|
||||
:ref:`SceneTree.change_scene_to(PackedScene scene) <class_SceneTree_method_change_scene_to>`:
|
||||
:ref:`SceneTree.change_scene_to_packed(PackedScene scene) <class_SceneTree_method_change_scene_to_packed>`:
|
||||
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
@@ -168,7 +168,7 @@ function
|
||||
var next_scene = preload("res://levels/level2.tscn")
|
||||
|
||||
func _my_level_was_completed():
|
||||
get_tree().change_scene_to(next_scene)
|
||||
get_tree().change_scene_to_packed(next_scene)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ Custom scene switcher
|
||||
|
||||
This tutorial will demonstrate building a scene switcher using autoloads.
|
||||
For basic scene switching, you can use the
|
||||
:ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>`
|
||||
:ref:`SceneTree.change_scene_to_file() <class_SceneTree_method_change_scene_to_file>`
|
||||
method (see :ref:`doc_scene_tree` for details). However, if you need more
|
||||
complex behavior when changing scenes, this method provides more functionality.
|
||||
|
||||
@@ -208,7 +208,7 @@ current scene and replace it with the requested one.
|
||||
# Add it to the active scene, as child of root.
|
||||
get_tree().root.add_child(current_scene)
|
||||
|
||||
# Optionally, to make it compatible with the SceneTree.change_scene() API.
|
||||
# Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
|
||||
get_tree().current_scene = current_scene
|
||||
|
||||
.. code-tab:: csharp
|
||||
@@ -241,7 +241,7 @@ current scene and replace it with the requested one.
|
||||
// Add it to the active scene, as child of root.
|
||||
GetTree().Root.AddChild(CurrentScene);
|
||||
|
||||
// Optionally, to make it compatible with the SceneTree.change_scene() API.
|
||||
// Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
|
||||
GetTree().CurrentScene = CurrentScene;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user