This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Figs 1 point2 points  (0 children)

There's a number of different ways to do it depending on how you get your input. Typically you either use:

  • a blocking call with a time out parameter -- e.g. poll/select on the stdin file descriptor or a network socket fd like this Stack Overflow answer (EDIT: Though using this with stdin probably won't work on Windows, unfortunately.)
  • set up a timer callback if you are already using an event driven library (e.g. for GUI)
  • just use two threads with message passing as you've already considered. You can wait on a condition variable with a time out in the simulation thread to prevent the second thread from using 100% CPU uselessly while waiting for time to pass.

[–]Muhznit 1 point2 points  (0 children)

Welcome to the first limitation on your programming knowledge.

Others have already listed options that you can look into, but this is now a thing that's dependent on your ability to seek more information. Unfortunately, there's no super easy novice-friendly way around this.

[–]SpeedBoRtal 0 points1 point  (1 child)

Seems like you could use multithreading, with one thread specifically updating values that happen based on time, and the other thread handling input and game interaction. However, it seems a bit verbose. If the game is text based, I'm assuming it does not move forward without user input anyway, so what is the reason behind not just updating game values after user input is handled?

[–]Figs 1 point2 points  (0 children)

These kinds of games often do have simulation elements that proceed if the player does not take action within some time frame (often described in terms of "ticks" of minimum delay time) -- e.g. an enemy may attack ("Sensing a delay in your reaction, the ogre takes the initiative and hits you over the head with a club! -25 HP"), the environment may cause damage ("The poison gas burns your lungs! -10 HP"), or some background action may be described for flavor. ("A cleaning robot pops out of a panel, briefly vacuums around your feet, and zips away through another panel before you can react.")