I'm sorry to have to ask so often

I have a level scene with 'node' as root and an instance scene with 'node2d' as root

When I searched for the replacement of scenes on the Internet, I found that I used get.tree (). Scene_change (path)

When I put this content in the instance scene and run it, the level scene changes

I want to replace the instance scene itself with $ "." Or something like $ self.

But again this time I encountered a nonexistent function in base 'node2D' error

Syntax error depending on node is difficult

In this case, how can I replace the instance scene with another scene?

$ self.scene I also tried this But an item called 'scene' does not seem to exist on node2d.

@MungMoong said: I'm sorry to have to ask so often

No worries! There is no limit to how many questions you can ask :smile:

I have a level scene with 'node' as root and an instance scene with 'node2d' as root

When I searched for the replacement of scenes on the Internet, I found that I used get.tree (). Scene_change (path)

If think you are thinking of get_tree().change_scene(path). Here’s the documentation page for SceneTree.

In this case, how can I replace the instanc scene with another scene?

I’m confused, what are you trying to do?

Do you want to reload the scene? You can reload the scene using the reload_current_scene function in SceneTree (documentation).

If you are wanting to load another scene from a file, you’ll either the resource path that points to a .tscn file and use change_scene function in SceneTree, or you’ll need a packed scene and use change_scene_to function in SceneTree.

Here is a simple example that will change the scene after a few seconds using change_scene. Note: I have not tested this code, and you’ll need to setup the scene path in the editor if you use the exported variable..

# This will allow you to set the path to the scene file in the editor!
Export (String, FILE) var path_to_scene
#
# Or you can use a constant string.
# Just replace with the path to your scene in the line below
# var path_to_scene = “res://example_scene.tscn”
#
# variables for the timers
var timer = 0
var wait_time = 4

func _process(delta):
	timer += delta
	if (timer >= wait_time):
		get_tree().change_scene(path_to_scene)

Hopefully this helps :smile:

Like this ↑

I want change just instance scene self

But The level scene above them has changed

I have created TEXT in the script in the level scene, but this part disappears when the scene is replaced

So I noticed that the level scene changed, not the instance.

The script that replaces the scene is contained in the instance

So to force the instance to replace itself I have tried using $ '.' and $ self and get_node ()

However, there is an error in the node that the function does not exist.

Oh! In that case, you don't need to change scenes at all! Now that I have seen the picture, I think I understand what you are looking to do.

What you need to do is instance/create the new scene, position it at the same position as the old scene, do any setup you need to setup for new scene, and then delete the old one.

Or to use the example in the picture, you need to instance/create a new red stick figure scene, then position the red stick figure scene at the same position as the blue stick figure you are wanting to replace, do any setup needed on the newly instanced red stick figure, and then finally delete/free the blue stick figure you are replacing.

change_scene is, more or less, designed for changing everything currently in the currently loaded scene. I primarily use it for changing levels, transitioning from the title screen to the main game, and for things like the game-over and/or win screens.

What you need to down is spawn (or instance as it is called in Godot) a new scene, which will create a copy of whatever scene you want to instance, so long as you are calling the instance function on the PackedScene (which you can get using load and preload). Then all you need to do is destroy (you need to use the free/queue_free functions) the old scene/nodes once you have spawned the new scene.


I have not tested the code, but something like this is what you need to do if you want to replace a blue stick figure from the blue stick figure's script:

extends Node2D
var red_stick_figure_scene = preload("res://path_to_red_stickfigure.tscn")

func _process(delta):
	# Replace the stick figure if the enter key is pressed
	if (Input.is_key_pressed(KEY_ENTER)):
		replace_stick_figure()

func replace_stick_figure():
	# Make a new red stick figure scene and assign it to the new_red_stick_figure  variable
	var new_red_stick_figure = red_stick_figure_scene.instance()
	# Set it's position to the same position as this stick figure
	new_red_stick_figure.global_position = self.global_position
	# Add the new red stick figure as a child of whatever node is the parent of this stick figure
	get_parent().add_child(new_red_stick_figure)
	# Finally, destroy/free this stick figure
	self.queue_free()

You can remove the self parts if you want, as they are not strictly necessary. I just added them so it is hopefully a little easier to see what is going on. The replace_stick_figure function is where the code needed for spawning a new scene and then removing the old one is located.


If you want to replace the blue stick figure from a node that is not the blue stick figure you are wanting to replace, then you'll need something like the following code:

extends Node2D
var red_stick_figure_scene = preload("res://path_to_red_stickfigure.tscn")

func _process(delta):
	# Replace the stick figure if the enter key is pressed
	if (Input.is_key_pressed(KEY_ENTER)):
		replace_stick_figure(get_node("path_to_blue_stick_figure"))

func replace_stick_figure(stick_figure_to_replace):
	# Make a new red stick figure scene and assign it to the new_red_stick_figure  variable
	var new_red_stick_figure = red_stick_figure_scene.instance()
	# Set it's position to the same position as the passed in stick figure
	new_red_stick_figure.global_position = stick_figure_to_replace.global_position
	# Add the new red stick figure as a child of whatever node is the parent of the passed in stick figure
	stick_figure_to_replace.get_parent().add_child(new_red_stick_figure)
	# Finally, destroy/free the passed in stick figure
	stick_figure_to_replace.queue_free()

Then that will remove the stick figure from another node, so long as the NodePath is correct.


For either example, you will need to replace the resource path in red_stick_figure_scene with the resource path to whatever scene you are wanting to use, and you'll probably want to rename the variables to something that makes sense for your project (unless your project is using red stick figures).

In the second example, you'll also need to change the NodePath in replace_stick_figure(get_node("path_to_blue_stick_figure")) with the path to whatever node you are wanting to replace.

Hopefully this helps :smile:

I sincerely appreciate your favor

In fact, this method would be the last one I wanted to see if it was possible in the way I did with the engine I used before. I'm still a beginner, and the code is likely to get messy. So I was hesitating.

I use the method you taught me and it was resolved soon Thank you very much!

@MungMoong said: I sincerely appreciate your favor

In fact, this method would be the last one I wanted to see if it was possible in the way I did with the engine I used before. I'm still a beginner, and the code is likely to get messy. So I was hesitating.

I use the method you taught me and it was resolved soon Thank you very much!

No problem, happy to help! :smile:

4 years later