Godot Version
4.3
Question
I know how to free nodes manually, but how do you free scripts? using queue_free() on their path name or something?
4.3
As mentioned in the description you’ve pasted from the documentation, a GDScript
is a resource. If you look at the class reference for GDScript
on Godot Docs, you can see which class(es) it inherits from.
As you can see in the picture above, Object
is the base. Therefore, I first looked at Object
’s class reference and found that it has a method to free the object from memory called free()
.
If you wish to learn the difference between free()
and queue_free()
, you may read: Godot Basics | Memory Management & Node | queue_free().
I hope that answers your question.
Huh, thanks
So if I’m not wrong you add a function in the script itself to call free() on itself and call that function in response to a signal to free the script?
@Sweatix You’re almost right, but in case of scripts you don’t need to use Object.free
or Object.queue_free
because their also inherit from RefCounted
which means this is a object that deletes himself when go out of scope (not used anymore):
You can see a pratical example of this here:
Oh no, you see in my screenshot you can see that static variables cause scripts to stay loaded even when out of scope. I was asking how to manually free them as static_unload is bugged
Yeah, in this case if you’re using static variables you need to delete the script manually or simple set the variable for a smaller data for not weight in the memory
That is exactly why I responded the way I did. I took the context into account.
@Sweatix just in case you forgot to reply to my message
If you’re looking to connect free()
to a signal, I think you can just do that.
button.pressed.connect(free)
I’m not a GDScript expert though. I could be wrong. You might have to reference the script with your_script.free
or something else entirely.
If you don’t already know, Godot has documentation that answers most questions about the engine. It’s worth taking a look when you’re in a bind.
aight then, thanks for your help
I did some experiments and it appears that you can’t free scripts manually by either free or queue_free. It gives an error when trying to free it
Hmm… I see. Would you mind disclosing what you’re trying to create? Perhaps there’s an alternative way to approach it.
I’m not “creating” something, I’m just finding out a way to free scripts that have static variables manually since static unload is bugged and it might crop up some time. It’s just an experiment to get data to use later. So you could say I’m creating a sandbox to test dealing with the persistence of scripts with static variables.