Merge pull request #1321 from dsnopek/postinitialize

Send NOTIFICATION_POSTINITIALIZE to extension classes
pull/1323/head
Rémi Verschelde 2023-11-30 09:55:30 +01:00 committed by GitHub
commit 11b2700b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 0 deletions

View File

@ -50,6 +50,12 @@ void Wrapped::_postinitialize() {
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this); godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this);
} }
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks()); godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
if (extension_class) {
Object *obj = dynamic_cast<Object *>(this);
if (obj) {
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
}
}
} }
Wrapped::Wrapped(const StringName p_godot_class) { Wrapped::Wrapped(const StringName p_godot_class) {

View File

@ -236,6 +236,11 @@ func _ready():
get_viewport().push_input(event) get_viewport().push_input(event)
assert_equal(custom_signal_emitted, ["_input: H", 72]) assert_equal(custom_signal_emitted, ["_input: H", 72])
# Check NOTIFICATION_POST_INITIALIZED, both when created from GDScript and godot-cpp.
var new_example_ref = ExampleRef.new()
assert_equal(new_example_ref.was_post_initialized(), true)
assert_equal(example.test_post_initialize(), true)
exit_with_status() exit_with_status()
func _on_Example_custom_signal(signal_name, value): func _on_Example_custom_signal(signal_name, value):

View File

@ -63,10 +63,18 @@ int ExampleRef::get_id() const {
return id; return id;
} }
void ExampleRef::_notification(int p_what) {
if (p_what == NOTIFICATION_POSTINITIALIZE) {
post_initialized = true;
}
}
void ExampleRef::_bind_methods() { void ExampleRef::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id); ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id); ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
ClassDB::bind_method(D_METHOD("was_post_initialized"), &ExampleRef::was_post_initialized);
ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id"); ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
} }
@ -220,6 +228,7 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200)); ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind); ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static); ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2); ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
@ -597,6 +606,12 @@ Vector4 Example::get_v4() const {
return Vector4(1.2, 3.4, 5.6, 7.8); return Vector4(1.2, 3.4, 5.6, 7.8);
} }
bool Example::test_post_initialize() const {
Ref<ExampleRef> new_example_ref;
new_example_ref.instantiate();
return new_example_ref->was_post_initialized();
}
// Virtual function override. // Virtual function override.
bool Example::_has_point(const Vector2 &point) const { bool Example::_has_point(const Vector2 &point) const {
Label *label = get_node<Label>("Label"); Label *label = get_node<Label>("Label");

View File

@ -35,16 +35,21 @@ private:
static int last_id; static int last_id;
int id; int id;
bool post_initialized = false;
protected: protected:
static void _bind_methods(); static void _bind_methods();
void _notification(int p_what);
public: public:
ExampleRef(); ExampleRef();
~ExampleRef(); ~ExampleRef();
void set_id(int p_id); void set_id(int p_id);
int get_id() const; int get_id() const;
bool was_post_initialized() const { return post_initialized; }
}; };
class ExampleMin : public Control { class ExampleMin : public Control {
@ -167,6 +172,8 @@ public:
Vector2 get_custom_position() const; Vector2 get_custom_position() const;
Vector4 get_v4() const; Vector4 get_v4() const;
bool test_post_initialize() const;
// Static method. // Static method.
static int test_static(int p_a, int p_b); static int test_static(int p_a, int p_b);
static void test_static2(); static void test_static2();