Examine PackedScene nodes without instancing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey
:warning: Old Version Published before Godot 3 was released.

I have a PackedScene with a RigidBody2D as a parent. I want to know the body’s linear velocity and use that information to eventually instance the scene. Is there a way to do this, perhaps through get_state’s SceneState? Seems like a waste to instance and immediately free() it because it’s not ready.

It is not clear to me what you’re trying to do.

There is no linear velocity to access if it isn’t instanced, and when you instance it and put it into action, then it is instanced, so just access the data you need, or set it to some default state as desired.

avencherus | 2017-05-12 17:41

The PackedScene is like the equivalent of a Class, there is no instance data if you don’t instance it, Zylann solution may be the best.

eons | 2017-05-12 17:55

:bust_in_silhouette: Reply From: Zylann

I asked this in my early days: https://forum.godotengine.org/1627/access-nodes-of-a-scene-before-instantiate-it

I still don’t know how to do exactly that, but there is another design that might fit your case, which is instance pooling.
Basically, you still instance your scene, but you don’t add it to the tree. If your logic decides this scene is not needed yet, don’t free it, keep it cached. Then the next time you want to check it, you won’t need to instance it again.

That brings up another question I have. Is pooling helpful in Godot (since it’s not GC’d)? For performance reasons anyways. Seems like it would be but there are some sources that say otherwise.

Honestly, asking this question just feels like I’ve made a poor design decision somewhere and need to refactor, so maybe I’ll do that. Instance pooling sounds like a great solution though, thanks Zylann. Should you set_process and set_fixed_process to false when an instance is pooled (not in scene tree), or is this done automatically?

jarlowrey | 2017-05-12 22:15

It depends on what you want to pool, but if you remove it from the tree I don’t think it matters. If you leave it in (for performance maybe?) then you need to check process/sound/physics on them.

Also this is not a necessarily a bad design to prefetch scene properties, I had this problem initially to load levels, which I was afraid to pool due to their size and number (although I didn’t try yet). If I have this problem again I will probably either look into a solution deeper, follow a system of rules or generate some kind of meta-files.

Zylann | 2017-05-13 00:10

:bust_in_silhouette: Reply From: Guilherme Furst

I’ve asked a similar question recently to the answer above.

But the way I’m thinking about it, you really shouldn’t be able to access a scene attributes before instancing it.
Look at the PackedScene as an resource object, like a script.gd for example, its a static resource until you actually instance() it (or new()), thus creating an actual “object” in memory.