I am upgrading some input handling code from Godot 3.5 to 4.0 and noticed that some parts are not working as expected. So was wondering to check if this might be a bug or intended behaviour which I am not aware of.
Here is the C++ code which I am using in 3.5 to separate generic input events into key, mouse button and mouse motion events.
void _input(Variant event) {
Ref<InputEvent> input_event = event;
if (input_event != nullptr) {
Ref<InputEventKey> input_event_key = event;
if (input_event_key != nullptr) {
if (input_event_key->is_pressed()) {
// Handle keyboard key press
} else {
// Handle keyboard key release
}
}
Ref<InputEventMouseButton> input_mouse_button = event;
if (input_mouse_button != nullptr) {
int button_index = input_mouse_button->get_button_index();
if (input_mouse_button->is_pressed()) {
// Handle left and right mouse button press, and mouse wheel scrolls
} else {
// Handle left and right mouse button release
}
}
Ref<InputEventMouseMotion> input_mouse_motion = event;
if (input_mouse_motion != nullptr) {
// Handle mouse motion
}
}
}
This was working well in 3.5. However, when running this in 4.0 all nullptr
checks return false and it never goes into if statements. I also tried casting with these ones but it also does not allow me to separate between key, mouse button and mouse motion events. I.e. using static_cast
I am never getting nullptr
while using dynamic_cast
and cast_to
always comes as nullptr
.
So was wondering if this is intended or it might be a bug? If it's intended then was wondering how could I distinguish between key, mouse button and mouse motion events?