Ticking

Managing Object Ticking Order

How can we ensure that objects are ticked in a specific order to avoid dependency issues?

Abstract art representing computer programming

Managing the order in which objects are ticked is crucial for avoiding dependency issues and ensuring consistent behavior in your game. Here are several strategies to control the ticking order.

Priority-based Ticking

Assign priorities to different types of objects and tick them in order of priority.

#include <algorithm>
#include <memory>
#include <vector>

class GameObject {
public:
  virtual void Tick() = 0;
  virtual int GetPriority() const = 0;
};

class World {
public:
  void AddObject(
    std::unique_ptr<GameObject> obj) {
    objects.push_back(std::move(obj));
    std::sort(objects.begin(), objects.end(),
              [](const auto& a, const auto& b){
                return a->GetPriority() > b->
                  GetPriority();
              });
  }

  void TickAll() {
    for (const auto& obj : objects) {
      obj->Tick();
    }
  }

private:
  std::vector<std::unique_ptr<GameObject>>
  objects;
};

Multi-phase Ticking

Implement multiple ticking phases for different types of updates.

class GameObject {
public:
  virtual void TickPhysics() {}
  virtual void TickLogic() {}
  virtual void TickRendering() {}
};

class World {
public:
  void TickAll() {
    for (const auto& obj : objects) {
      obj->TickPhysics();
    }
    for (const auto& obj : objects) {
      obj->TickLogic();
    }
    for (const auto& obj : objects) {
      obj->TickRendering();
    }
  }

  // ...
};

Dependency Graph

For complex systems, you can create a dependency graph and perform a topological sort to determine the ticking order.

#include <unordered_set>

class GameObject {
public:
  virtual void Tick() = 0;

  virtual std::vector<GameObject*>
    GetDependencies() const { return {}; }
};

class World {
public:
  void TickAll() {
    std::unordered_set<GameObject*> visited;
    std::vector<GameObject*> sortedObjects;

    for (const auto& obj : objects) {
      DepthFirstSort(obj.get(), visited,
                     sortedObjects);
    }

    for (auto it = sortedObjects.rbegin(); it !=
         sortedObjects.rend(); ++it) {
      (*it)->Tick();
    }
  }

private:
  void DepthFirstSort(
    GameObject* obj,
    std::unordered_set<GameObject*>& visited,
    std::vector<GameObject*>& sorted
  ) {
    if (visited.find(obj) != visited.
      end()) return;
    visited.insert(obj);
    for (auto dep : obj->GetDependencies()) {
      DepthFirstSort(dep, visited, sorted);
    }
    sorted.push_back(obj);
  }

  std::vector<std::unique_ptr<GameObject>> objects;
};

By implementing one or a combination of these strategies, you can ensure that objects are ticked in the correct order, avoiding dependency issues and maintaining consistent game behavior.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 55 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved