2
\$\begingroup\$

This may be obvious and trivial for you but as I am a newbie in programming I come with a specific question.

I have three loops in my game engine which are input-loop, update-loop and render-loop. Update-loop is set to 10 ticks per second with a fixed timestep, render-loop is capped at around 60 fps and the input-loop runs as fast as possible. I am using one of the Javascript frameworks which provide such things but it doesn't really matter.

Let's say I am rendering a tile map and the view of which elements are rendered depends on camera-like movement variables which are modified during key pressing. This is only about camera/viewport and rendering, no game physics involved here.

And now, how can I handle input events among these loops to keep consistent engine reaction? Am I supposed to read the current variable modified with input and do some needed calculations in a update-loop and share the result so it could be interpolated in a render-loop? Or read the input effect directly inside the render-loop and put needed calculations inside?

I thought interpreting user input inside an update-loop with a low tick rate would be inaccurate and kind of unresponsive while rendering with interpolation in the final view. How it is done properly in games overall?

\$\endgroup\$
  • 6
    \$\begingroup\$ Why do you have three independent loops? That's usually not a good idea. You don't want to split up processing at that level because you run into exactly these data race conditions. You should have one loop, which gathers input, updates game logic and handles rendering. Parts of those tasks can be done concurrently, but concurrent loops for them seems like unnecessary complexity. \$\endgroup\$ – Josh Jun 11 '14 at 16:16
  • \$\begingroup\$ I said it wrongly. I have one main game loop provided by the framework which handles updating and rendering and second event loop, which captures input, also provided by the framework. So they are not exactly independent. All I want to know is how to maintain input and react to changes in engine. Should I push every input in some kind of queue and then proceed this queue inside update-loop? \$\endgroup\$ – user48023 Jun 11 '14 at 16:54
  • \$\begingroup\$ Contrary to your statement, the JS framework you're using does matter. Generally JS runs in a single thread, so you couldn't have, for example, an input event interrupt a frame being rendered. But if the framework you're using is creating a worker thread for each "game loop" then they are in fact running concurrently. \$\endgroup\$ – bcrist Jun 11 '14 at 16:55
  • \$\begingroup\$ Are you having to poll the input events or just receive the queue input event messages? \$\endgroup\$ – Simeon Pilgrim Jul 12 '14 at 23:47
1
\$\begingroup\$

You should have your Input in your update loop unless input runs at the same interval as your update loop which in the latter case makes no sense.

In your update loop you are working with your input so if it does not run at the same interval you will miss input.

Let's say your input runs 20 times/s and update 10 times/s:

On input 9 you press a key, then it runs input again (10) which does not register the key press anymore and then it runs update 5.

You usually handle input updates on top of the update loop, then it will run all the update code which is perfectly synchronized with the input. Then it will render to screen and repeat itself. Having a separate input loop just complicates things and imho is weird.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.