X Tutup
Skip to content

Add change_scene_to_node()#85762

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:wait_it's_all_nodes︖
Oct 22, 2025

Hidden character warning

The head ref may contain hidden characters: "wait_it's_all_nodes\ufe16"
Merged

Add change_scene_to_node()#85762
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:wait_it's_all_nodes︖

Conversation

@KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Dec 4, 2023

This PR adds a new method: change_scene_to_node(), which changes the scene to node. The code already existed, this change just extracts part of it into a new method. Thus calling change_scene_to_file() will call change_scene_to_packed(), which now calls change_scene_to_node().
The new method is useful when you want to do some setup to the new scene before changing.
Also corrected the docs, which claimed that the new scene is instantiated after the old one is removed, which wasn't true.

Closes godotengine/godot-proposals#921
Closes godotengine/godot-proposals#9243

This does not implement the part where the old scene can be left intact, because it's not very useful. My current approach to the problem mentioned in the proposal is adding the menu scene on top of game and pausing the game; changing scenes temporarily is not a good practice.

@radiantgurl
Copy link
Contributor

Any activity?

Copy link
Contributor

@radiantgurl radiantgurl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@Mahi
Copy link

Mahi commented Feb 4, 2025

Any news on this? Seems like a very trivial yet useful feature that just hasn't been merged yet?

Meanwhile for anyone else with this issue, use this workaround:

var tree = get_tree()
tree.current_scene.queue_free()
tree.root.add_child(my_scene)
tree.current_scene = my_scene

But it's 1. cumbersome to use in multiple places, 2. very beginner unfriendly, and 3. inconsistent with other change_scene_to_* methods.

@KoBeWi KoBeWi modified the milestones: 4.x, 4.5 Feb 4, 2025
@KoBeWi KoBeWi force-pushed the wait_it's_all_nodes︖ branch 2 times, most recently from 3993b49 to b441162 Compare June 11, 2025 18:31
@Repiteo Repiteo modified the milestones: 4.5, 4.x Sep 18, 2025
@KoBeWi KoBeWi force-pushed the wait_it's_all_nodes︖ branch 2 times, most recently from 1fa088b to 37f5e1a Compare September 23, 2025 19:14
@kleonc
Copy link
Member

kleonc commented Sep 23, 2025

Consider the user doing something like (simplified):

var node_a := Node.new()
var node_b := Node.new()

get_tree().change_scene_to_node(node_a)
get_tree().change_scene_to_node(node_b)

It potentially might be surprising for the user that node_a was freed as a result. The docs should probably explicitly note/warn that by passing a node to change_scene_to_node you kinda give up the ownership and it will be auto freed even in a scenario like above (node_a never became the current scene, it was a pending new scene only temporarily).

For comparison change_scene_to_packed is fine without such note/warn as it's not the user who instantiates the scene.

Besides that code LGTM, the addition is trivial.

@KoBeWi
Copy link
Member Author

KoBeWi commented Sep 23, 2025

Something like this?
[b]Warning:[/b] After using this method, the [SceneTree] will take ownership of the node and will free it automatically when changing scene again. Any references you had to that node may become invalid.

@kleonc
Copy link
Member

kleonc commented Sep 23, 2025

Something like this? [b]Warning:[/b] After using this method, the [SceneTree] will take ownership of the node and will free it automatically when changing scene again. Any references you had to that node may become invalid.

Yeah, LGTM, sends the message. 👍

@KoBeWi KoBeWi force-pushed the wait_it's_all_nodes︖ branch from 37f5e1a to 268e4b5 Compare September 23, 2025 21:22
Copy link
Member

@kleonc kleonc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the addition makes sense, and looking at the reactions there's a demand for it.

@KoBeWi KoBeWi force-pushed the wait_it's_all_nodes︖ branch from 268e4b5 to a34f7bf Compare September 25, 2025 09:50
@Repiteo Repiteo requested a review from Ivorforce October 21, 2025 19:20
Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the principle of least astonishment, I would have expected the interface to be designed in this way anyway — and there seems to interest in it too.

Code looks good to me.

@Ivorforce Ivorforce modified the milestones: 4.x, 4.6 Oct 22, 2025
@KoBeWi KoBeWi force-pushed the wait_it's_all_nodes︖ branch from a34f7bf to 88a5cd2 Compare October 22, 2025 10:21
@Repiteo Repiteo merged commit c53a181 into godotengine:master Oct 22, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 22, 2025

Thanks!

@KoBeWi KoBeWi deleted the wait_it's_all_nodes︖ branch October 22, 2025 19:14
@os-architect
Copy link

Wow. I just literally ran into a situation where I want something like this. Glad to know it's been merged. Talk about timing 😅

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add change_scene_to_node() method Rework the way of changing current scene

9 participants

X Tutup