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: