Skip to content

Commit

Permalink
Fix _notification with parent and child classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnopek committed Feb 22, 2024
1 parent f900859 commit 23c0109
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,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<m_class *>(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); \
} \
} \
\
Expand Down
8 changes: 8 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ func _ready():
assert_equal(example.test_virtual_implemented_in_script("Virtual", 939), "Implemented")
assert_equal(custom_signal_emitted, ["Virtual", 939])

# 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):
Expand Down
2 changes: 2 additions & 0 deletions test/project/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,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"]
18 changes: 18 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,24 @@ void Example::_input(const Ref<InputEvent> &event) {
}
}

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;
}
}

String Example::test_virtual_implemented_in_script(const String &p_name, int p_value) {
String ret;
if (GDVIRTUAL_CALL(_do_something_virtual, p_name, p_value, ret)) {
Expand Down
25 changes: 25 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,31 @@ class ExampleConcrete : public ExampleAbstractBase {
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);
};

class ExampleRuntime : public Node {
GDCLASS(ExampleRuntime, Node);

Expand Down
2 changes: 2 additions & 0 deletions test/src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<ExampleVirtual>(true);
ClassDB::register_abstract_class<ExampleAbstractBase>();
ClassDB::register_class<ExampleConcrete>();
ClassDB::register_class<ExampleBase>();
ClassDB::register_class<ExampleChild>();
ClassDB::register_runtime_class<ExampleRuntime>();
}

Expand Down

0 comments on commit 23c0109

Please sign in to comment.