This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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.

in Engine by (700 points)

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.

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.

2 Answers

+1 vote
Best answer

I asked this in my early days:

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.

by (29,510 points)
selected by

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?

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.

0 votes

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 for example, its a static resource until you actually instance() it (or new()), thus creating an actual "object" in memory.

by (61 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 Frequently asked questions and 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 [email protected] with your username.