1
\$\begingroup\$

I've been wondering this for some time now: Let's say you have a builder game like Anno and let's say you're in the very late game. You have literally thousands of production facilities placed. Each one of these needs base resources and time to produce an output. Also there's modules you can place onto the facility that give some sort of bonus.

Now: How would you make the game check each one of these and act accordingly (look if job is done, when done deliver output, start new job if resources available)?

The simplest I can think of is a loop over each of the facilities. But that just doesn't feel like a good idea due to the amount of facilities and the frequency on which game loops run.

Another thing I can think of is eventing. The facilities could inform the game core about state changes, but I'm having troubles seeing eventing and a game loop together.

As you probably can now tell I have no exp in game development but would like to get some input on that topic.

\$\endgroup\$
5
\$\begingroup\$

I would push all those events onto a priority queue (with the priority based on task completion time) and the event carries all the info it needs to perform its task, even to queue up new tasks if it's a repeating action. Then the main loop only needs to check the queue against the game time to see what needs to pop and process.

The actual work of building your program now becomes simpler, each event can be developed independently of any others and you don't need to modify the main engine at all to add new activities.

As a side note, if you design the game as a data model and views onto that model then "seeing eventing and a game loop together" makes much more sense. A simulation has a much different main loop from an action title.

\$\endgroup\$
4
\$\begingroup\$

Don't overcomplicate. Thousands of facilities might sound hard to calculate for you, but it really isn't. In the worst case, it's just a bunch of equations. Computers are amazing at that.

If you want some sort of optimization, push everything sorted by the time it gets finished to a queue (insertion sort). Then you'll be able to only check the jobs up until you encounter something that hasn't finished yet, since everything after that finishes even later.

\$\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.