-2

I have three activities:

  1. Attack
  2. Troops Training
  3. Gathering Resources

I want to attack and train troops while I am gathering resources. I want these activities to run in sync.

Here is an example of one of my functions:

for (int x=100; x<=0; x--) 
{ 
   sleep(50); 
   playerFood += 10;
}

How do I make functions run in sync with each other?

scope
  • 69
  • 9

1 Answers1

2

You might not need parallel execution for this at all. Many actions in games that seem to play out simultaneously are in fact being stepped sequentially. It's just that we only see the updated state once they've all been advanced, so it appears they're progressing in parallel.

As long as we arrive at the new state in time to present a new rendered frame to the player 30, 60, or more times each second, then the exact order in which the events were processed doesn't matter (or at least shouldn't matter, if we've planned our dependencies right and avoided creating order-of-update bugs for ourselves)

This is called the Game Loop Pattern - Robert Nystrom's book on such patterns does a great job explaining and motivating this pattern, so I highly recommend giving it a read.

In your case, your logic might look like this:

int Main() {
  while(exit == false) {
     Update_Game_State();
     Render_Frame();
     sleep(50);   
  }
  return 0;
}

Where your update function works through each system that needs to be advanced and ticks it forward one step:

void Update_Game_State() {
   playerFood += 10;

   troopsTrained += 1;

   Process_Attacks();

   // etc.
}

If you want your harvesting/training/attack events to run at different frequencies, then you can maintain a list of ongoing events and the next timestamp when they need to occur. Your update loop can then walk the list, updating the events whose time has come and skipping anything that's still pending for the future.

There are more sophisticated game loops possible too. Glenn Fielder's "Fix Your Timestep" article outlines some popular best practices.

If you prefer to write code in a different style, where each system looks like it's running uninterrupted in parallel, but without the complications of thread synchronization, you can look into Coroutines. These are methods that can suspend their execution state midway through to let other code take a turn, then resume where they left off. I wrote about using threads vs coroutines in the context of Unity here, and similar principles will apply in other environments.

DMGregory
  • 134,153
  • 22
  • 242
  • 357
  • i will not go into Deep , The Game name is Last Empire war z.a server sided Game. So when you gethring the troops go and gather resource while you can play the ther activities like attacks Or upgrades, and the time running on screen. its like they are parralely coded.. Any idea about thats Sir? – The Develop3r Apr 13 '17 at 12:48
  • There's no fundamental difference. The server can update all these events in what's effectively a fast turn-based loop, just like a client can. Or, if your game is asynchronous and players can log out and check back in after some time has passed, the server can defer the work until (if) the player returns, then quickly fast-forward to account for all the progress they made while offline. Parallel execution across threads is a useful tool in game development, but modelling progress over time is not what we typically use it for — we can get the same results much more simply in many cases. – DMGregory Apr 13 '17 at 13:22