A
A
Alexander2014-12-08 05:25:04
Game development
Alexander, 2014-12-08 05:25:04

What is the best way to create a game loop?

Hello, dear :)
I'm trying to write a game engine for a strategy like Dune or RedAlert. Just for myself, I conquer, so to speak, the heights of game development))
I do my game cycle in the world class and carry out all operations on model objects in it. In this class, the tick() function is responsible for all this and looks something like this:

void tick() 
{
  foreach ( body1 in mapBodies ) 
  {
    foreach ( body2 in mapBodies )
    {
       /*
         и тут я проверяю всевозможные взаимодействия этих двух
         объектов друг с другом. Если это танк, пушка, солдат и т.п., то 
         они могут кого-то видеть, атаковать или ехать по приказу и т.д.,
         если здание - что-то производить...

         В общем эта функция становится всё "жирнее и жирнее" с 
         добавлением нового функционала...
       */
    }
  }
}

but today in one of the sources I came across this implementation of this function:
void tick()
{
  foreach ( body in mapBodies )
  {
    body->tick();
  }
}

And that's all. In this function, the loop simply loops through all the objects and calls "tick". So my question is: which model of "tika" is simpler/more correct?
And how in the second case the objects should interact with each other? After all, logically, it turns out that tick (), being called in each object, generates a cycle of checking all objects for the possibility of interacting with the current object? And if there are thousands of objects? For one game tick, a thousand cycles should be born and destroyed? Will the computer die? Or am I misunderstanding something? :)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
suslik2015, 2014-12-08
@suslik2015

In your "architecture", the "world class" performs operations on all game objects. There is a violation of the principles of low connectivity and high coupling. Functionally, the game loop should kick the game objects or their managers that it's time to update their state, calculating the delta at the most. He should not know about graphics, sound, physics, etc. The solution is to delegate aspects of behavior to separate objects. You can read here about the component entity system in games.

D
Deerenaros, 2014-12-08
@Deerenaros

You understand everything correctly. Just from the fact that you move the tick into a separate method, nothing really changes in terms of performance. Well, about the number of objects - the more there are, the higher the performance requirements. In the average strategy with AI, I read somewhere that problems start over 1000 objects, but they are mostly solvable, while somewhere around 10,000,000 (ten million) are unsolvable. This means full-fledged objects for which you need to look for a path and calculate statistics.

A
Armenian Radio, 2014-12-08
@gbg

By the way, you can do half as many checks - here A interacts with B, and then B interacts with A. And you only need to check "A and B interacted."
You have not considered the third option - the presence of the "Interaction A and B" class, from which a bunch of classics are inherited with methods that implement the interactions of different pairs of objects. This is the most flexible option, and saves time:
Without this, you will have to write two methods:
in class A - a description of the interaction with class B,
in class B - a description of the interaction with class A.
Going further, we can pre-compile a list of all possible pairs of interactions (with pre-expanded types so that each time A does not find out which class we consider the interaction with now), which will be managed by one cycle.

T
tsarevfs, 2014-12-08
@tsarevfs

Calling tick() /*I'm more used to calling it update()*/ for each object is preferable. Due to polymorphism, for each type of object, its own implementation of this function can be called. Depending on the object, various interaction options are possible. For example, a hive may not do anything on its update. And the soldier must look at all the enemies and choose which one to shoot. This behavior is not obvious how to implement it your way. Well, there is no need to write giant functions.
If there are too many units, various optimizations are possible. For example, counting only units in a certain area, or combining a flock of birds into one object with simplified behavior, instead of calculating each bird. But it's worth thinking about when optimization becomes necessary.
The phrase about cycles being born and destroyed is also suspicious. This is the prerogative of objects. Loops are just a piece of code that runs almost like a set of sequential instructions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question