Instanced nodes are invisible after reloading the scene.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Martik

I’m working on a Tower Defense game. There is a save button that saves the player’s current world and the towers they have built. The information on the towers is recorded as 3D Array. That is 0: Type, 1: Level, 2: Position

And the code of the load button is like this:

	get_tree().change_scene("res://Scenes/" + data["current_world"] + ".tscn") #Changes scene to saved world.
var world = get_node("/root/World") #Gets new scene root.

for i in data["towers"]:
	var t = load("res://Resources/Towers/" + i[0] + ".tscn").instance()
	t.level = i[1]
	t.global_position = i[2]

The scene is reloaded in the last saved world, but the towers are invisible and unresponsive in the scene even though they appear in the SceneTree and give output in their _ready() functions.

Another interesting thing is that if I don’t change the scene, the towers can be reloaded at the saved location with no issues. How can I solve this problem?

Are they really invisible or do they have some ridicilous position away from visible screen ? :wink:

Inces | 2022-09-16 15:26

I have printed their global positions and they have the same global positions before and after load. Please help.

Martik | 2022-09-16 15:51

what about visible property ? Is it really set to false after load ?
If yes, is there any moment in code when You manually set it to true or false ? Like on ready, or when something builds them ?

Inces | 2022-09-16 16:21

Nope, but I noticed that the towers are deleted/removed from the scene right after they are instanced :(. Which is strange because there is nothing to trigger a tower to queue_free() itself on any script. And the towers are right where they are if I don’t change the scene to specified world with get_tree().change_scene("res://Scenes/" + data["current_world"] + ".tscn") That’s really weird.

Martik | 2022-09-16 16:28

Aren’t they just instantiated before scene is replaced ? :wink: And as the scene is replaced with delay, it clears already instantiated data.
replacing scene is generally very shitty way of changing a level, I don’t know why does this built-in function even exists in Godot. It is extremely bug friendly and messy sollution. Good practice is to just have high scope parent node, that keeps persistant data, and changes its children, that contain non-persistant data. This way You will be able to save/load data with parent node, and replace level structure with child node.

Inces | 2022-09-16 20:05

Yep seems quite possible that is due to a delay during screen load. Time for reconstruction. Thanks m8.

Martik | 2022-09-16 23:04