Skip to content

Commit 23c0109

Browse files
committed
Fix _notification with parent and child classes
1 parent f900859 commit 23c0109

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

include/godot_cpp/classes/wrapped.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,16 @@ public:
239239
\
240240
static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) { \
241241
if (p_instance && m_class::_get_notification()) { \
242+
if (!p_reversed) { \
243+
m_inherits::notification_bind(p_instance, p_what, p_reversed); \
244+
} \
242245
if (m_class::_get_notification() != m_inherits::_get_notification()) { \
243246
m_class *cls = reinterpret_cast<m_class *>(p_instance); \
244-
return cls->_notification(p_what); \
247+
cls->_notification(p_what); \
248+
} \
249+
if (p_reversed) { \
250+
m_inherits::notification_bind(p_instance, p_what, p_reversed); \
245251
} \
246-
m_inherits::notification_bind(p_instance, p_what, p_reversed); \
247252
} \
248253
} \
249254
\

test/project/main.gd

+8
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ func _ready():
245245
assert_equal(example.test_virtual_implemented_in_script("Virtual", 939), "Implemented")
246246
assert_equal(custom_signal_emitted, ["Virtual", 939])
247247

248+
# Test that notifications happen on both parent and child classes.
249+
var example_child = $ExampleChild
250+
assert_equal(example_child.get_value1(), 11)
251+
assert_equal(example_child.get_value2(), 33)
252+
example_child.notification(NOTIFICATION_ENTER_TREE, true)
253+
assert_equal(example_child.get_value1(), 11)
254+
assert_equal(example_child.get_value2(), 22)
255+
248256
exit_with_status()
249257

250258
func _on_Example_custom_signal(signal_name, value):

test/project/main.tscn

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ offset_right = 79.0
2424
offset_bottom = 29.0
2525
text = "Click me!"
2626

27+
[node name="ExampleChild" type="ExampleChild" parent="."]
28+
2729
[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"]

test/src/example.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,24 @@ void Example::_input(const Ref<InputEvent> &event) {
630630
}
631631
}
632632

633+
void ExampleBase::_bind_methods() {
634+
ClassDB::bind_method(D_METHOD("get_value1"), &ExampleBase::get_value1);
635+
ClassDB::bind_method(D_METHOD("get_value2"), &ExampleBase::get_value2);
636+
}
637+
638+
void ExampleBase::_notification(int p_what) {
639+
if (p_what == NOTIFICATION_ENTER_TREE) {
640+
value1 = 11;
641+
value2 = 22;
642+
}
643+
}
644+
645+
void ExampleChild::_notification(int p_what) {
646+
if (p_what == NOTIFICATION_ENTER_TREE) {
647+
value2 = 33;
648+
}
649+
}
650+
633651
String Example::test_virtual_implemented_in_script(const String &p_name, int p_value) {
634652
String ret;
635653
if (GDVIRTUAL_CALL(_do_something_virtual, p_name, p_value, ret)) {

test/src/example.h

+25
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,31 @@ class ExampleConcrete : public ExampleAbstractBase {
220220
virtual int test_function() override { return 25; }
221221
};
222222

223+
class ExampleBase : public Node {
224+
GDCLASS(ExampleBase, Node);
225+
226+
protected:
227+
int value1 = 0;
228+
int value2 = 0;
229+
230+
static void _bind_methods();
231+
232+
void _notification(int p_what);
233+
234+
public:
235+
int get_value1() { return value1; }
236+
int get_value2() { return value2; }
237+
};
238+
239+
class ExampleChild : public ExampleBase {
240+
GDCLASS(ExampleChild, ExampleBase);
241+
242+
protected:
243+
static void _bind_methods() {}
244+
245+
void _notification(int p_what);
246+
};
247+
223248
class ExampleRuntime : public Node {
224249
GDCLASS(ExampleRuntime, Node);
225250

test/src/register_types.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
2727
ClassDB::register_class<ExampleVirtual>(true);
2828
ClassDB::register_abstract_class<ExampleAbstractBase>();
2929
ClassDB::register_class<ExampleConcrete>();
30+
ClassDB::register_class<ExampleBase>();
31+
ClassDB::register_class<ExampleChild>();
3032
ClassDB::register_runtime_class<ExampleRuntime>();
3133
}
3234

0 commit comments

Comments
 (0)