Push or pull? Inclusion of keys in speed for slot machines

Do I have to press keys on vehicles when they are pressed, or should vehicles pull pressed keys from the engine?

I have a vehicle object in which there are elements of location, speed and accelleration (among other things) and an update method in which it updates its location based on its speed and its vevlocity based on its accelleration.

I have a game object that contains a game loop that calls the update method on a vehicle.

If the player controls the car using the arrow keys, if pressing the key sets the appeal (pressing), and the release key clears the speed, or if the car requests a game engine if the accellerate (pull) key is pressed? I think pushing would mean that the keyboard control module should know about vehicles, while traction means that the vehicle should know the specific controls on the keyboard.

I think the related question will look something like this: should all objects know about all other objects or there must be strict hierarchies, so can objects set things / pass things to other objects in the tree, but not down (or vice versa)?

+4
source share
5 answers

You should try to execute the Subscribing / Observer pattern. You put all the key capture code in one singleton InputManager, and then each object requiring a reaction to the input registers with the manager.

The manager contains a list of signed objects and sends them events when keys are pressed / pressed. Just remember to unsubscribe when the subject is deleted or “loses focus”.

This avoids the problem of polling. There are very few exceptions when polling permission is desired.

+8
source

IMO, your car should not know anything about keyboards, mice or gamepads. And not one of them should know about your transport code. The input processing code should read the input for each player and translate it into some kind of instruction specific to their context. For example, if a player is driving a car, his instruction may include steering the wheel, acceleration and braking performance. While a player piloting an airplane may need to step, yaw, etc.

By translating the input of the gamepad (or something else) into the appropriate type of instruction, you can separate input mechanisms from the logic of the game. One thing that would be possible with this level of isolation would be to create a "CarInstruction" from the network input.

+2
source

The answer to this question is difficult without a deeper knowledge of how your game engine works. Having said that, I will take a hit on him. The push keyboard presses approach reads to me like an event or feedback strategy. You define a function somewhere that looks like def handle_key_event(name_of_key): which is called whenever a key event occurs. The advantage of this is that in terms of readability, you know exactly where key events are processed. On the other hand, each keystroke should be considered an atomic operation. If you need to store many state variables around the state of other keys to determine what to do with each press, it can get a little confused.

On the other hand, if you press keystrokes, you enter a delay inherent in keystrokes. You will not capture key events faster than your elevations / frame rate. This is great if your game is ticking both beautifully and quickly, but you do not want the user interface to become overloaded / backward when the frame rate slows down.

Just food for thought, I think. First of all, choose a strategy and stick to it. If keyboard events are callbacks, do not use, for example, the pull approach for mouse events. Consistency is more important than IMO correctness here.

+1
source

@Joel: I agree - vehicles should not know about certain hardware controls, and input processing code should not know anything about vehicles. There must be some intermediate class that displays the keys to the vehicles. Thanks for the contribution!

+1
source

you want to poll:

 void UpdateVehicleFromInput() { if (InputSystem()->IsKeyDown(key)) DoSomething(); } 

And that, of course, is "somewhere in your update cycle, where it suits you." If you want to call this private place “part of your input system” or “part of your game logic” or something else, knock yourself.

That way, you know why you are doing something (no reason is given), you can set the conditions trivially, you know that you do something once and exactly once (and you can change it without branching, especially if the transport the remedy does not exist), and you know when you are doing something (before or after you say, react to damage, or position your particle effects, or who knows what else).

An abstract input system can be valid if you are really doing cross-platform development. for random development, this is very unnecessary (but a fun technical distraction when you run out of game design ideas for implementation).

Contrary to the irrational popular belief, there are no barriers to questioning. The processors do> 1B things per second, one IF if the frame does not matter (basically the only relevant cpu operations are N ^ 2, where N> 100, and purge your cache l2 and, of course, busy waiting for access to the disk). The input for polling is O (1).

+1
source

Source: https://habr.com/ru/post/1277273/


All Articles