Attempt to call function 'instantiate' in base 'previously freed' on a null instance

Godot Version

Godot 4.4.1

Question

Hello

i get the error “Attempt to call function ‘instantiate’ in base ‘previously freed’ on a null instance” in a scipt where i intend to create and delete scenes for navigating menus.

i use a function in a permanent scene to “transit” between scenes that are children of the permanent scene. I’m trying to implement return buttons in every scene for smoother navigation but that’s where i get the error.

here is all relevant code to this error:

the Startup scene and script are the permanent scene.

Here are the 2 scenes scripts which are the ones i’m trying to navigate to and from.

This is a very early prototype and i’m probably trying to this in a way that’s so very not optimal so if you have any suggestion on how to do menu navigation easier i’m all for it, please tell me :wink:

Thanks and have a nice day/evening

Whenever you set sceneExited (or any other node) to another node, use Node.duplicate. Otherwise, you will just create a reference to the node and when it is freed, that reference becomes invalid.

One thing I would like to add is that your code is generally more readable if you use static types because it makes it immediately visible which variable has which members and what its function is. I believe this is just preference though and there are people that don’t use it.

1 Like

youre passing a null instance on parent.sceneExited since the variable wasnt declared in that scope put that variable in your control node that these extend from

1 Like

Thanks for you advice but i’m not sure i understand how that would solve my issue here.

Can you be more specific please?

thank you

The error message means that you are trying to call a method on a node that does not exist anymore. This is probably because, in transitScene, you are setting sceneExited directly to sceneOut and then calling queue_free on sceneOut. So now both sceneExited and sceneOut are ‘previously freed’ and cannot be used anymore. But sceneExited is used in _on_return_pressed

1 Like

Okay i think i get it: you use the node duplicate to store it’s value into a var because if you don’t and you delete the node right after you assigned a var to it’s value, the var becomes null.

Thanks for the explanation it is much clearer :slight_smile:

you could also use remove_child() as it doesnt delete the data from the node you can read more about it here

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.