How does change_scene_to_file() method exactly work?

Question

How does the method change_scene_to_file exactly work? Which current scene does it exactly replaces with the given file?

If this is what my remote tree looks like, I’ve always found it changing the Game node with the given file even when the children scenes call the method.
image

And how do I change a specific node with another scene? e.g. in the following image, if I want to change the GameTimer scene with something else, how do I do so?
image

I’ll appreciate any help.

Use owner to get the root of this scene (this scene here stands for the instantiated scene file), and you can only have one scene (this scene here stands for the current game scene).

1 Like

It would change your Game node since that is the active/running scene.

The root node is, well, the root and GameMusic and GameEvents are autoloads. They are managed by godot.

If you want to change a node other than your running scene you can free it and add a new child to its parent. E.g.

func replace():
    $UI/GameTimer.queue_free()
    var node := Sprite2D.new()
    $UI.add_child(node)
1 Like

For instance, I have a player scene, in which there is a timer node. If I write @onready var player = owner in the timer’s script, the player property will be the root node of the player scene.

On one hand, games have a “scene” that contains all current things, this is what the change_scene_to_file() stands for. On the other hand, a scene is a node tree, stored as a file in Godot and can be instantiated to be used.

1 Like

Thank you @coderjo and @peltodev :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.