I'm new to Godot so excuse the rather dumb question.

I'm instancing objects in my scene by preloading another scene and then calling instance and adding it to a node in my main scene. Pretty much as described on the scripting page in the docs. I also add it to a group for future use as I want to be able to access all instances at some point in time.

Now this object moves around on the screen for a bit but after a while I no longer need it. Now I'm playing nice, removing the instance from its parent, removing the instance from its group, and then calling free. Seems to work fine and I'm guessing its the right way to do it but I noticed I can just call free and it nicely cleans up my object.

So am I just being too nice or is it safe to just free it and let Godot just do the cleanup?

And a small follow up on this, instead of detecting on position I'm now using a VisibliityNotifier to detect when my object goes off camera. But calling free gives an error.

Instead I now simply remove the object from its parent and it seems to clean up all right. Or is it still floating in memory somewhere?

I usually just queue_free() stuff. (Though to be honest queue_free() doesn't actually seem any better than plain old free().) The main GDScript docs definitely don't say anything about removing things from parents or groups. If some other object is using it you'll get errors though, but I generally have the other objects check, is not object == null: in those cases. Like with your VisibilityNotifiier. When you remove the object from it's parent it gives the VisibilityNotifier a chance to exit_screen(). You should probably still free() it after that. Do you get any "object instances still in use!" errors on exit?

You should use Node.queue_free(), then you don't have to worry about it at all!

Cool, thanks guys, hadn't seen queue_free yet. That sounds like the right thing to do.

Ross, no I'm not getting "object instances still in use" errors but I figure that is because I don't keep references to the objects. The only things that reference the objects are its children which I'm guessing are nicely freed up. Thanks for the tip though because I'm sure I'll run into that scenario:)

6 years later