Hi, since I introduced diagonal movement to my game, I encountered problems while detecting. Input handling works currently this way: The mainloop forwards the sf::Event (if type is KeyPressed or KeyReleased) to the GameObject's input component. At this component, I store this press/release in a (as map<Key, State>). On each cycle, my component is updated to move all previously pressed keys to currently hold - and previously released keys to currently "free" keys.
This system works great, unless one fact: When querying my input component, all predefined keys (the bindings for left,right,up,down and some additional ones - doesn't matter here) are checked. Key detection works this way:
if (keys.at(binding.up) == PRESS || keys.at(binding.up) == HOLD) {
move.y = -1;
}
Left, down and right are handled in the same way. So the move var (btw sf::Vector2i) finally has values such es (0,1), (-1,1) etc. This works well, IF the keys are pressed EXACTLY at the same time. But often pressing A and W (as left and up) doesn't happen at the same time, because the program is even faster than the user.
Well, so I started delaying each of those queries to the input state for some milliseconds (so they aren't queried on each frame if a user is allowed to). But the effect isn't that great: Sometimes the movement looks delayed to the player (because of the delay ;D ) - and sometimes the previous effect (first walk left, than diagonally left-up) occures (because the up-key is pressed slitely after the first query).
What am I doing wrong referring to my input design? :-\
BTW: The first "left-only" movement is completly executed, because my systems avoids query input while an action (walking to the left tile) isn't completed. This system achieves some kind of discrete movement and simplifies collision detection etc.
It's quite hard to understand how your system works in full details, but what if you just check for the hold status?
if (keys.at(binding.up) == HOLD && keys.at(binding.down) == HOLD) {
move.y = 0;
}
else if (keys.at(binding.up) == HOLD) {
move.y = -1;
}
else if (keys.at(binding.down) == HOLD) {
move.y = 1;
}
if (keys.at(binding.right) == HOLD && keys.at(binding.left) == HOLD) {
move.x = 0;
}
else if (keys.at(binding.left) == HOLD) {
move.x = -1;
}
else if (keys.at(binding.right) == HOLD) {
move.x = 1;
}