+1 vote

Hello, I want to delete some nodes after they were used. I tried queuefree() and removechild(), but when I change to a new map and come back to the old map, the nodes still there.

in Engine by (171 points)

2 Answers

0 votes
queue_free()

sorry didn't finish reading :) there is also a free without queue

https://godotengine.org/qa/4062/freeing-objects-nodes

by (1,661 points)
0 votes

As far as I understood you want to maintain some level of persistence between your scenes. The thing is, when you load a PackedScene from the file system, e.g. get_tree().change_scene("path/to/scene.tscn") Godot will load the scene as saved in this file, resetting any changes made during the playthrough.

One way you can achieve what you want is to repack the scene and either keep it in memory and switch to it during the play session, but you will lose the changes when you close the game, or you can save it on disk and load it from there. You can save it in the user:// path.

const SAVED_SCENE_PATH = "user://path/to/saved_scene.tscn"
const DEFAULT_SCENE_PATH = "res://path/to/scene.tscn"

func some_func():
    if ResourceLoader.exists(SAVED_SCENE_PATH):
        get_tree.change_scene(SAVED_SCENE_PATH)
    else:
        get_tree.change_scene(DEFAULT_SCENE_PATH)
by (60 points)

But if I fight a monster in the previous map and dont wanna fight it again when I come back to this map, how can I do that?

I just told you, you have to save the current state of the scene and load it again when you come back.

You can do it using PackedScene.pack(node) where node is the root of your scene. Then you can save it on disk with ResourceSaver.save(packed_scene, "path/to/saved_scene.tscn"

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.