Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input - prevent action missed input on Android #83438

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
action.pressed++;
}
if (action.pressed == 1 && (is_joypad_axis_valid_zone_enter || !is_joypad_axis)) {
action.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
action.pressed_process_frame = Engine::get_singleton()->get_process_frames();
action.pressed_physics_frame = input_curr_tick;
action.pressed_process_frame = input_curr_process_frame;
}
is_pressed = true;
} else {
Expand All @@ -728,8 +728,8 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em

if (is_released) {
if (action.pressed == 1) {
action.released_physics_frame = Engine::get_singleton()->get_physics_frames();
action.released_process_frame = Engine::get_singleton()->get_process_frames();
action.released_physics_frame = input_curr_tick;
action.released_process_frame = input_curr_process_frame;
}
action.pressed = MAX(action.pressed - 1, 0);
}
Expand Down Expand Up @@ -860,8 +860,16 @@ void Input::action_press(const StringName &p_action, float p_strength) {

action.pressed++;
if (action.pressed == 1) {
action.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
action.pressed_process_frame = Engine::get_singleton()->get_process_frames();
// Dev check we are correctly updating the input curr tick and frame.
// This should currently be done in the MainLoop.
// Things will go pretty wrong input wise, if this isn't done,
// so it makes sense to assert and catch this early.
// Probably most relevant to users overriding the MainLoop.
DEV_ASSERT((input_curr_tick - Engine::get_singleton()->get_physics_frames()) <= 1);
DEV_ASSERT((input_curr_process_frame - Engine::get_singleton()->get_process_frames()) <= 1);

action.pressed_physics_frame = input_curr_tick;
action.pressed_process_frame = input_curr_process_frame;
}
action.strength = p_strength;
action.raw_strength = p_strength;
Expand All @@ -874,8 +882,8 @@ void Input::action_release(const StringName &p_action) {

action.pressed--;
if (action.pressed == 0) {
action.released_physics_frame = Engine::get_singleton()->get_physics_frames();
action.released_process_frame = Engine::get_singleton()->get_process_frames();
action.released_physics_frame = input_curr_tick;
action.released_process_frame = input_curr_process_frame;
}
action.strength = 0.0f;
action.raw_strength = 0.0f;
Expand Down
10 changes: 10 additions & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class Input : public Object {
int64_t mouse_window = 0;
bool legacy_just_pressed_behavior = false;

// Input can come in AFTER the user has had the opportunity to poll input for the
// frame or tick, and thus the input "clocks" are kept one ahead of the regular
// engine clocks, and syncing immediately prior to process() and physics_process()
// in order to minimize latency.
uint64_t input_curr_tick = 0;
uint64_t input_curr_process_frame = 0;
Comment on lines +105 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using naming similar to Engine and Action: input_physics_frame and input_process_frame?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terminology of tick and physics_frame has been discussed before, I won't rehash here.

Upshot was to move over to the tick terminology wherever possible. See for example the renaming to project setting physics/common/physics_ticks_per_second.

This is used internally in timing code in a number of places, but it may take some time to move existing terminology, and authors often copy terminology from existing code, and there is backward compatibility to think about, so there may be some inconsistencies.


struct Action {
uint64_t pressed_physics_frame = UINT64_MAX;
uint64_t pressed_process_frame = UINT64_MAX;
Expand Down Expand Up @@ -347,6 +354,9 @@ class Input : public Object {

void set_event_dispatch_function(EventDispatchFunc p_function);

void set_curr_tick(uint64_t p_tick) { input_curr_tick = p_tick; }
void set_curr_process_frame(uint64_t p_frame) { input_curr_process_frame = p_frame; }

Input();
~Input();
};
Expand Down
4 changes: 4 additions & 0 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ bool SceneTree::physics_process(double p_time) {

flush_transform_notifications();

Input::get_singleton()->set_curr_tick(Engine::get_singleton()->get_physics_frames() + 1);

if (MainLoop::physics_process(p_time)) {
_quit = true;
}
Expand Down Expand Up @@ -486,6 +488,8 @@ bool SceneTree::physics_process(double p_time) {
bool SceneTree::process(double p_time) {
root_lock++;

Input::get_singleton()->set_curr_process_frame(Engine::get_singleton()->get_process_frames() + 1);

if (MainLoop::process(p_time)) {
_quit = true;
}
Expand Down