Godot Version
4
Question
I’m making a turn based battle system where you choose your action, and then on the enemies turn you have to dodge their attack kind of like undertale. I have one scene that has all of the buttons for your turn, and another scene thats where you dodge the enemies attacks. I just useget_tree().change_scene_to_file(" ") to switch between them. But whenever I switch between them, things like the enemies health dont save. How do i fix this?
(As you can see in the video, the enemies health goes back to 20 after their turn ends)
You can use an Autoload, but I’d recommend adding the scene as a child of root and disabling the outer fight scene while it plays.
2 Likes
You can remove it from the scene tree, and keep a variable referencing it so that it isn’t deleted, and then use the variable to add it back when desired. It is effectively paused while not in the tree .
Or you can set its process_mode to disabled and make sure all children are set to inherit process mode (which is the default). Note that depending on the version of the engine you’re using, you might get some engine errors that say p_elem → root when setting the process mode back to inherit, but these don’t seem to cause issues.
2 Likes
What exact code do I need to use to remove one of them from the tree and to add one to the tree?
As an example, your TurnBased node could do this
# Disabling the TurnBased Node
process_mode = Node.PROCESS_MODE_DISABLED
visible = false
# Creating combat
var combat = interactive_battle.instantiate()
get_tree().root.add_child(combat)
# Wait for combat to finish (assuming such a signal exists)
await combat.combat_finished
combat.queue_free()
# Enable the TurnBased Node yet again
visible = true
process_mode = Node.PROCESS_MODE_PAUSABLE
1 Like