I’m using RenderingServer and I’m having trouble setting a parent instance for an instance to inherit its transform.
In canvas_item related methods, RenderingServer provides canvas_item_set_parent method to set canvas_item’s parent canvas_item, but I didn’t find similar method in instance related methods.
My question is, how to set parent instance for instance to inherit transform from parent instance?
I looked at the source code of the Godot engine and found that in the 4.2 version of the source code the engine directly uses global_transform.
This means that the RenderingServer may not provide a set_parent class method for the instance or I can’t use it directly in gdscript.
Therefore, the workaround for this problem is to use global_transform directly in the code and notify the child node when the transform is updated.
void VisualInstance3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_WORLD: {
ERR_FAIL_COND(get_world_3d().is_null());
RenderingServer::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
_update_visibility();
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
// (edit) Here is it.
Transform3D gt = get_global_transform();
RenderingServer::get_singleton()->instance_set_transform(instance, gt);
} break;
case NOTIFICATION_EXIT_WORLD: {
RenderingServer::get_singleton()->instance_set_scenario(instance, RID());
RenderingServer::get_singleton()->instance_attach_skeleton(instance, RID());
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_visibility();
} break;
}
}