0 votes

Hi,

I'm adding a method to Godot source code and I still didn't figure how to obtain the owner of a given RID and then how to convert it to PhysicsBody2D. The Godot engine development documentation doesn't cover much about those conversions.

Thanks in advance!

asked Oct 25, 2018 in Engine by gcardozo (22 points)

It doesn't look like you can get the direct Resource for a given RID. From the RID documentation: "They are opaque, so they do not grant access to the associated resource by themselves." They're usually used by low-level servers. It may be possible by accessing the PhysicsServer directly. But that's a whole other can of worms.

Yes, I don't know how to use the RID to obtain a PhysicsBody2D. I have a List<RID> so I suppose it should be something like:

for (List<RID>::Element *E = rid_list.front(); E; E = E->next()) {
    RID body = E->get();
    //somehow obtain the body owner here:
    NoIdeaOfType body_owner = SomehowObtainOwner(body);
    PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(body_owner);
    //do my stuff with physics_body...
}

I haven't tried this but maybe Physics2DServer::body_get_object_instance_id(RID) and ObjectDB::get_instance(ObjectID) could help?

Looks like your suggestion is part of what I needed, thank you so much!
Here's what I did:

for (List<RID>::Element *E = rid_list.front(); E; E = E->next()) {
    RID body = E->get();
    ObjectID instance_id = Physics2DServer::get_singleton()->body_get_object_instance_id(body);
    Object *obj = ObjectDB::get_instance(instance_id);
    PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
    //doing stuff with the physics_body...
}

Awesome, glad I could help. Should I add it as an answer?

I've answered it and referenced your suggestion, thx again! :D

1 Answer

+1 vote

@IndicaInkWell suggestion was part of the solution, here's how I did it:

for (List<RID>::Element *E = rid_list.front(); E; E = E->next()) {
    RID body = E->get();
    ObjectID instance_id = Physics2DServer::get_singleton()->body_get_object_instance_id(body);
    Object *obj = ObjectDB::get_instance(instance_id);
    PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
    //doing stuff with the physics_body...
}
answered Oct 26, 2018 by gcardozo (22 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 How to use this Q&A? before posting your first questions.