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

InputSystem doesn't use input in correct order #87

Open
denniskaselow opened this issue Aug 24, 2020 · 8 comments · May be fixed by #91
Open

InputSystem doesn't use input in correct order #87

denniskaselow opened this issue Aug 24, 2020 · 8 comments · May be fixed by #91

Comments

@denniskaselow
Copy link

InputSystem uses input_queue.keys_pressed.pop() and is commented as // Get the first key pressed but pop() removes the last item.

If you input keys fast enough (very easy when not using release mode and batch rendering) this can be seen because of erratic movement of the player character.

@iolivia
Copy link
Owner

iolivia commented Aug 24, 2020

good catch, we probably want a queue not a stack!

@denniskaselow
Copy link
Author

Something simple as this could also be sufficient:

impl InputQueue {
    pub fn get_first(&mut self) -> Option<KeyCode> {
        if self.keys_pressed.is_empty() {
            None
        } else {
            Some(self.keys_pressed.remove(0))
        }
    }
}

But I'm new to Rust, so I have no idea if this is the Rust way to do things, but it's what I'd do, considering the key_pressed Vec will never have more than a few keys even in worst case (ignoring automated input).

@fineconstant
Copy link

I used VecDaque in my implementation:

#[derive(Default)]
pub struct KeyDownQueue {
    pub keys_pressed: VecDeque<KeyCode>,
}

@iolivia
Copy link
Owner

iolivia commented Dec 31, 2020

that looks great @fineconstant! feel free to submit a PR, will be very helpful for others using the repo 😄

@fineconstant
Copy link

fineconstant commented Jan 1, 2021

I looked into it and I think that Vec<KeyCode> is sufficient, so there is no need to make any changes.

With Vec<KeyCode> calling input_queue.keys_pressed.pop() function removes the last element indeed, but input_queue.keys_pressed.push(keycode); appends an element at the back of the collection - it should not cause any problems.

std::vec::Vec - Rust #method.push
std::vec::Vec - Rust #method.pop

I've just recently started learning Rust so I may be wrong, but if you agree then I think we can close this issue 😄

@denniskaselow
Copy link
Author

Like I said in the initial comment:

If you input keys fast enough (very easy when not using release mode and batch rendering) this can be seen because of erratic movement of the player character.

A push can happen several times in the time it takes one pop to move the character. Thus you can input up, left, down and it'll move the character down, left, up. So your VecDeque would be much better.

@fineconstant
Copy link

fineconstant commented Jan 1, 2021

Oh I see now, you are right. I will prepare a PR with changes 😃

@fineconstant
Copy link

@iolivia , @denniskaselow
Take a look at #91 - what do you think? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants