From 7ddd278dead574c6e0905f1b009dbbb85dda3ee8 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Fri, 2 Feb 2024 10:07:45 -0600 Subject: [PATCH] Fix _notification with parent and child classes (cherry picked from commit 23c010900c9a09c5c99bfbb0d465cd468aa74b6c) --- include/godot_cpp/classes/wrapped.hpp | 9 +++++++-- test/project/main.gd | 8 ++++++++ test/project/main.tscn | 2 ++ test/src/example.cpp | 18 ++++++++++++++++++ test/src/example.h | 25 +++++++++++++++++++++++++ test/src/register_types.cpp | 2 ++ 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 32caf39b..3772f275 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -218,11 +218,16 @@ public: \ static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) { \ if (p_instance && m_class::_get_notification()) { \ + if (!p_reversed) { \ + m_inherits::notification_bind(p_instance, p_what, p_reversed); \ + } \ if (m_class::_get_notification() != m_inherits::_get_notification()) { \ m_class *cls = reinterpret_cast(p_instance); \ - return cls->_notification(p_what); \ + cls->_notification(p_what); \ + } \ + if (p_reversed) { \ + m_inherits::notification_bind(p_instance, p_what, p_reversed); \ } \ - m_inherits::notification_bind(p_instance, p_what, p_reversed); \ } \ } \ \ diff --git a/test/project/main.gd b/test/project/main.gd index 59cab6dc..a5b420b9 100644 --- a/test/project/main.gd +++ b/test/project/main.gd @@ -241,6 +241,14 @@ func _ready(): assert_equal(new_example_ref.was_post_initialized(), true) assert_equal(example.test_post_initialize(), true) + # Test that notifications happen on both parent and child classes. + var example_child = $ExampleChild + assert_equal(example_child.get_value1(), 11) + assert_equal(example_child.get_value2(), 33) + example_child.notification(NOTIFICATION_ENTER_TREE, true) + assert_equal(example_child.get_value1(), 11) + assert_equal(example_child.get_value2(), 22) + exit_with_status() func _on_Example_custom_signal(signal_name, value): diff --git a/test/project/main.tscn b/test/project/main.tscn index 2b98e0f0..14e0aed5 100644 --- a/test/project/main.tscn +++ b/test/project/main.tscn @@ -22,4 +22,6 @@ offset_right = 79.0 offset_bottom = 29.0 text = "Click me!" +[node name="ExampleChild" type="ExampleChild" parent="."] + [connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"] diff --git a/test/src/example.cpp b/test/src/example.cpp index 5372d70a..60b7217e 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -626,3 +626,21 @@ void Example::_input(const Ref &event) { emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode()); } } + +void ExampleBase::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_value1"), &ExampleBase::get_value1); + ClassDB::bind_method(D_METHOD("get_value2"), &ExampleBase::get_value2); +} + +void ExampleBase::_notification(int p_what) { + if (p_what == NOTIFICATION_ENTER_TREE) { + value1 = 11; + value2 = 22; + } +} + +void ExampleChild::_notification(int p_what) { + if (p_what == NOTIFICATION_ENTER_TREE) { + value2 = 33; + } +} diff --git a/test/src/example.h b/test/src/example.h index c86a51fd..7a75dafd 100644 --- a/test/src/example.h +++ b/test/src/example.h @@ -216,4 +216,29 @@ protected: virtual int test_function() override { return 25; } }; +class ExampleBase : public Node { + GDCLASS(ExampleBase, Node); + +protected: + int value1 = 0; + int value2 = 0; + + static void _bind_methods(); + + void _notification(int p_what); + +public: + int get_value1() { return value1; } + int get_value2() { return value2; } +}; + +class ExampleChild : public ExampleBase { + GDCLASS(ExampleChild, ExampleBase); + +protected: + static void _bind_methods() {} + + void _notification(int p_what); +}; + #endif // EXAMPLE_CLASS_H diff --git a/test/src/register_types.cpp b/test/src/register_types.cpp index 58080d20..a673dee0 100644 --- a/test/src/register_types.cpp +++ b/test/src/register_types.cpp @@ -27,6 +27,8 @@ void initialize_example_module(ModuleInitializationLevel p_level) { ClassDB::register_class(true); ClassDB::register_abstract_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); } void uninitialize_example_module(ModuleInitializationLevel p_level) {