Sign in to follow this  
NetrickPL

When to check input in fixed step game loop

Recommended Posts

Hey! I have a question about fixes step game loop. Is it better to check input (mouse, keyboard and packets) in fixed step loop or in render loop?

I mean #1:
while (true) //main loop
{
    while(fixed step loop) //ie 50/100 times a second
     {
       check input
       do logic
      }
      render
}

or rather #2:

while (true) //main loop
{
      check input
    while(fixed step loop) //ie 50/100 times a second
     {
       do logic
      }
      render
}

I think the approach #1 is better especially for lower framerates (you check input whenever you need for logic, and in case #2 @5fps you could press a key for a short moment and it could be processed 20 times by logic loop).

Could you advice me what's better? Because I'm not sure. Which one is used in games?

Thanks

Share this post


Link to post
Share on other sites

Yeah, it looks like version 1 is the better approach to get fast responses when pressing a key or something.

But usually, if the game runs with 5 fps, no one will like to play it anyway ^^

Don´t think to much over something that simple. If you run into problems with the time the game needs to react, you are still able to change your code...

Share this post


Link to post
Share on other sites
I think the approach #1 is better especially for lower framerates (you check input whenever you need for logic, and in case #2 @5fps you could press a key for a short moment and it could be processed 20 times by logic loop).

Could you advice me what's better? Because I'm not sure. Which one is used in games?

Thanks

In many cases you will actually be limited by your OS in which advanced key input handling is tied to Window management, and if you go multithreading the OS will tend you to move input handling to the Graphics rendering thread. It's not impossible to overcome, but gives a lot more work.

 

Doing it in logic should be best. But take in mind you have to split your thinking of input into logic and graphics. Depending on your game, there may be some input that must be handled deterministically, affecting logic; while there's some other input that is graphics related and non-deterministic (i.e. a GUI)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this