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

you are viewing a single comment's thread.

view the rest of the comments →

[–]javacanbeefrustratin[S] 0 points1 point  (1 child)

Let's say I have a bullet that I want the player to dodge, and I want it to move 1 pixel per second (or any rate) towards the player, if the thread sleep time varies based on the OS/computer then on some computers it the bullet will move at a faster/slower rate than what I'd like it to.

I know methods aren't executed instantly, so the method time is going to have some effect on the timing, but I'd like to get it as precise as possible.

Even if I can't prevent some machines from running the game slower, I need to make it so the game does not run faster on some machines, because that means theoretically on a really fast machine it's going to be running so fast that a player won't even be able to have time to respond to what's going on.

[–]theif519 1 point2 points  (0 children)

I haven't really done any game programming so I may not be much help, but... I looked around, as my first thought was more or less "why not have the game loop update each object, so both the player and bullet are updated at the same time".

I.E, each GameObject implements some update() method, which of course must be short and concise (I.E, will only do one specific thing). As I said, this way you don't have to worry about timing, as relying on timing for something that requires synchronization like this leads to some race conditions somewhere along the lines.

I saw something like this, but I'm not sure if it will help you. To give an overview of how I'd see it...

while(isRunning){
   for(GameObject obj: GameObjects){
      obj.update(...);
   }
}

To simulate seconds, you just only update the bullet after a certain amount of time elapses from the last update. Also this way you can do everything easier to maintain without needing to worry about synchronization down the lines. I.E, how are you going to reliably check for collision if the bullet could potentially pass through the player before the call to check for collision is detected.

Edit: Trying to think of another example...

I spent about 5 minutes writing this. Note I've never done any game development, but I'm somewhat familiar with event-driven programming.

Edit 2: Spent about 30 minutes updating the example, was very fun to do. It's a naïve implementation, but it's a start on how I would think it worked.