Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
シーンをスクリプトから変更する
Sometimes it helps to have more control over how you swap scenes around. A Viewport's child nodes will render to the image it generates. This holds true even for nodes outside of the "current" scene. Autoloads fall into this category, and also scenes which you instantiate and add to the tree at runtime:
var simultaneous_scene = preload("res://levels/level2.tscn").instantiate()
func _add_a_scene_manually():
# This is like autoloading the scene, only
# it happens after already loading the main scene.
get_tree().root.add_child(simultaneous_scene)
public Node simultaneousScene;
public MyClass()
{
simultaneousScene = ResourceLoader.Load<PackedScene>("res://levels/level2.tscn").Instantiate();
}
public void _AddASceneManually()
{
// This is like autoloading the scene, only
// it happens after already loading the main scene.
GetTree().Root.AddChild(simultaneousScene);
}
To complete the cycle and swap out the new scene with the old one, you have a choice to make. Many strategies exist for removing a scene from view of the Viewport. The tradeoffs involve balancing operation speed and memory consumption, as well as balancing data access and integrity.
Delete the existing scene. SceneTree.change_scene_to_file() and SceneTree.change_scene_to_packed() will delete the current scene immediately. You can also delete the main scene. Assuming the root node's name is "Main", you could do
get_node("/root/Main").free()
to delete the whole scene.メモリをアンロードします。
長所: RAMはもはやデッドウェイトを引きずっていません。
短所: 再びメモリにロードし直す必要があるため、そのシーンに戻るのにコストがかかります(時間がかかります)。すぐに戻る必要がない場合は問題ありません。
短所: そのシーンのデータにアクセスできなくなります。そのデータをすぐに使用する必要がない場合は問題ありません。
注: 間もなく削除されるシーンのデータを保存するには、1つまたは複数のノードを別のシーンに再アタッチするか、SceneTree に直接アタッチすると便利です。
処理の停止。
Pro: No nodes means no processing, physics processing, or input handling. The CPU is available to work on the new scene's contents.
短所: これらのノードの処理と入力処理は動作しなくなります。更新されたデータを使用する必要がない場合は問題ありません。
Hide the existing scene. By changing the visibility or collision detection of the nodes, you can hide the entire node sub-tree from the player's perspective.
メモリはまだ存在しています。
Pro: You can still access the data if needed.
長所: データを保存するためにノードを移動する必要はありません。
Con: More data is being kept in memory, which will be become a problem on memory-sensitive platforms like web or mobile.
処理は続行されます。
Pro: Data continues to receive processing updates, so the scene will keep any data within it that relies on delta time or frame data updated.
長所: ノードはまだグループのメンバーです(グループは SceneTree に属しているため)。
Con: The CPU's attention is now divided between both scenes. Too much load could result in low frame rates. You should be sure to test performance as you go to ensure the target platform can support the load from this approach.
Remove the existing scene from the tree. Assign a variable to the existing scene's root node. Then use Node.remove_child(Node) to detach the entire scene from the tree.
Memory still exists (similar pros/cons as hiding it from view).
Processing stops (similar pros/cons as deleting it completely).
Pro: This variation of "hiding" it is much easier to show/hide. Rather than potentially keeping track of multiple changes to the scene, you only need to call the add/remove_child methods. This is similar to disabling game objects in other engines.
短所: 表示のみから非表示にする場合とは異なり、デルタ時間、入力、グループ、または SceneTree アクセスから派生した他のデータに依存している場合、シーン内に含まれるデータは、古いままになってしまいます。
There are also cases where you may wish to have many scenes present at the same time, such as adding your own singleton at runtime, or preserving a scene's data between scene changes (adding the scene to the root node).
get_tree().root.add_child(scene)
GetTree().Root.AddChild(scene);
Another case may be displaying multiple scenes at the same time using SubViewportContainers. This is optimal for rendering different content in different parts of the screen (e.g. minimaps, split-screen multiplayer).
Each option will have cases where it is best appropriate, so you must examine the effects of each approach, and determine what path best fits your unique situation.