Skip to content

Commit 11b2700

Browse files
authored
Merge pull request godotengine#1321 from dsnopek/postinitialize
Send NOTIFICATION_POSTINITIALIZE to extension classes
2 parents f3143c7 + 20c4e84 commit 11b2700

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/classes/wrapped.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ void Wrapped::_postinitialize() {
5050
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this);
5151
}
5252
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
53+
if (extension_class) {
54+
Object *obj = dynamic_cast<Object *>(this);
55+
if (obj) {
56+
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
57+
}
58+
}
5359
}
5460

5561
Wrapped::Wrapped(const StringName p_godot_class) {

test/project/main.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ func _ready():
236236
get_viewport().push_input(event)
237237
assert_equal(custom_signal_emitted, ["_input: H", 72])
238238

239+
# Check NOTIFICATION_POST_INITIALIZED, both when created from GDScript and godot-cpp.
240+
var new_example_ref = ExampleRef.new()
241+
assert_equal(new_example_ref.was_post_initialized(), true)
242+
assert_equal(example.test_post_initialize(), true)
243+
239244
exit_with_status()
240245

241246
func _on_Example_custom_signal(signal_name, value):

test/src/example.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ int ExampleRef::get_id() const {
6363
return id;
6464
}
6565

66+
void ExampleRef::_notification(int p_what) {
67+
if (p_what == NOTIFICATION_POSTINITIALIZE) {
68+
post_initialized = true;
69+
}
70+
}
71+
6672
void ExampleRef::_bind_methods() {
6773
ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
6874
ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
6975

76+
ClassDB::bind_method(D_METHOD("was_post_initialized"), &ExampleRef::was_post_initialized);
77+
7078
ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
7179
}
7280

@@ -220,6 +228,7 @@ void Example::_bind_methods() {
220228

221229
ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
222230
ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
231+
ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
223232

224233
ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
225234
ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
@@ -597,6 +606,12 @@ Vector4 Example::get_v4() const {
597606
return Vector4(1.2, 3.4, 5.6, 7.8);
598607
}
599608

609+
bool Example::test_post_initialize() const {
610+
Ref<ExampleRef> new_example_ref;
611+
new_example_ref.instantiate();
612+
return new_example_ref->was_post_initialized();
613+
}
614+
600615
// Virtual function override.
601616
bool Example::_has_point(const Vector2 &point) const {
602617
Label *label = get_node<Label>("Label");

test/src/example.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ class ExampleRef : public RefCounted {
3535
static int last_id;
3636

3737
int id;
38+
bool post_initialized = false;
3839

3940
protected:
4041
static void _bind_methods();
4142

43+
void _notification(int p_what);
44+
4245
public:
4346
ExampleRef();
4447
~ExampleRef();
4548

4649
void set_id(int p_id);
4750
int get_id() const;
51+
52+
bool was_post_initialized() const { return post_initialized; }
4853
};
4954

5055
class ExampleMin : public Control {
@@ -167,6 +172,8 @@ class Example : public Control {
167172
Vector2 get_custom_position() const;
168173
Vector4 get_v4() const;
169174

175+
bool test_post_initialize() const;
176+
170177
// Static method.
171178
static int test_static(int p_a, int p_b);
172179
static void test_static2();

0 commit comments

Comments
 (0)