0 votes

Im trying to make a 2D platformer game and have implemented a scene called witchwhich contains all info about the witch. The witch is the player you play as.

I want to keep the info about the witch when changing scene so i made it a global variableA global variable containing the info about the witch

So this works during a the first scen where i add the player to the game_stage scene like the image below showsadding the player to stage1

Things to note is that i have a GUI that updated the health mana etc about the player, which is updated when the scene is created and also when the players stats changes(heals, takes dmg , etc etc)

The thing is when i change scenes (done by a simple area2d and body_entered function), the stage2´s GUI tries initialize it using the global Witch variable which apparently is "previously freed instance".

How would figuring out this ?

Thanks in advance
Links to images incase they dont work

in Engine by (12 points)

1 Answer

0 votes

It doesn't work because the instance of Witch.tscn was part of the scene tree when you changed the level. So it got deleted, and your global variable now references a "deleted instance".

You should consider removing the witch from the scene tree before switching levels, and add it back afterwards.
Another approach is to just not store the entire witch node as a global variable, instead create a script "witch_globals.gd" containing only variables shared across all levels, and store information such as health or player_name in there.

by (27,962 points)
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.