Godot Version
4.2.1
Question
I’m testing ClassDB binding features and found a weird situation.
I attempted to bind two properties from my GDExtension module and the code is:
void Camera3DStateMachine::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_camera"), &Camera3DStateMachine::camera);
ClassDB::bind_method(D_METHOD("set_camera", "camera"), &Camera3DStateMachine::setCamera);
ClassDB::bind_method(D_METHOD("get_controller"), &Camera3DStateMachine::controller);
ClassDB::bind_method(D_METHOD("set_controller", "controller"), &Camera3DStateMachine::setController);
ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_NODE_TYPE, "Camera3D"), "set_camera", "get_camera");
ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "controller", PROPERTY_HINT_RESOURCE_TYPE, "Camera3DController"), "set_controller", "get_controller");
}
It leads to crash when I launch the editor. But when I exchange two ‘add_property’ 's order and it suddenly becomes normal. The modified code is:
void Camera3DStateMachine::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_camera"), &Camera3DStateMachine::camera);
ClassDB::bind_method(D_METHOD("set_camera", "camera"), &Camera3DStateMachine::setCamera);
ClassDB::bind_method(D_METHOD("get_controller"), &Camera3DStateMachine::controller);
ClassDB::bind_method(D_METHOD("set_controller", "controller"), &Camera3DStateMachine::setController);
ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "controller", PROPERTY_HINT_RESOURCE_TYPE, "Camera3DController"), "set_controller", "get_controller");
ClassDB::add_property(get_class_static(), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_NODE_TYPE, "Camera3D"), "set_camera", "get_camera");
}
I tried to debug the editor and it breaks at VariantTypeConstructor::variant_from_type method, looks like it attempted to access a wild pointer ‘p_value’. The stack is in the below image.
I mean does the ‘add_property’ order matter? Actually I wrote a binding script to generate these code for me, but if the order is relevant, how?
[Solved] I found my bug, I forgot to initialize a pointer which leads to the crash.