0 votes

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?

Godot version v3.2.2 stable
in Engine by (35 points)
edited by

Are they really invisible or do they have some ridicilous position away from visible screen ? ;)

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

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 ?

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.

Aren't they just instantiated before scene is replaced ? ;) 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.

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

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.