= Tickable = == Description == Tickable is an interface that adds only one, but very important, function to the inheriting classes: {{{ #!cpp virtual void tick(float dt); }}} == What is a tick? == A "tick" denotes one whole loop of the main-loop, which includes: Retrieving changes over the network, looking for input of the player, asking every object for new actions, drawing everything. When watching TV or a film, every tick is a different image on the screen, creating the illusion of motion. While ticks in TV have constant lenght (approximatively 1/25 or 1/30 seconds), ticks in a game have different lengths because of the different time needed to render the scene with different amount of objects and effects (and the speed of the used hardware which usually alters from system to system). This makes it impossible to work with hardcoded speed-values like "move 3 units every tick" because the game would slow down on slower machines or in hectic parts of the game while it would be unplayable fast on highend systems. That's where the factor ''dt'' comes in. It denotes the ''delta time'', meaning the time passed since the last frame in '''seconds'''. Speed-values now look like "move 3*dt units every tick". This would result in 3 units per second, independend of the amount of frames per second. == How to use tick(dt) == Every class that wants to do something every tick has to inherit from Tickable or from a class that already inherited from Tickable. Note that tick() is a virtual function. This means: If you want to call the tick(dt) function of your parent, you have to do this manually by writing Parent::tick(dt) into the code of your tick(dt) function. This is a bit unhandy, but it allows you to freely decide whether the tick of your class comes before or after (or between) the tick of the parent class. But this might also cause problems when adding tick(dt) to an old class - several childs might already have implemented their own tick(dt). You have to change them all so they call the tick(dt) of your class too. == Example == Definition of some tickable class: {{{ #!cpp class MyClass : public ParentClass // ParentClass is tickable! { public: MyClass() : time_(0) {} virtual void tick(float dt); float getTime() { return this->time_; } private: float time_; }; }}} Implementation of !MyClass: {{{ #!cpp void MyClass::tick(float dt) { this->time_ += dt; // Call tick(dt) of our parent ParentClass::tick(dt); } }}} Sidenote: !ParentClass might look like this: {{{ #!cpp class ParentClass : public Tickable { public: // This function does what parents usually do virtual void tick(float dt) { std::cout << "Be careful!" << std::endl; } }; }}}