| 1 | = Tickable = |
| 2 | |
| 3 | == Description == |
| 4 | |
| 5 | [wiki:Tickable] is an interface that adds only one, but very important, function to the inheriting classes: |
| 6 | {{{ |
| 7 | #!cpp |
| 8 | virtual void tick(float dt); |
| 9 | }}} |
| 10 | |
| 11 | == What is a tick? == |
| 12 | |
| 13 | 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). |
| 14 | |
| 15 | 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. |
| 16 | |
| 17 | == How to use tick(dt) == |
| 18 | |
| 19 | Every class that wants to do something every tick has to inherit from [wiki:Tickable] or from a class that already inherited from [wiki: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. |
| 20 | |
| 21 | == Example == |
| 22 | |
| 23 | Definition of some tickable class: |
| 24 | {{{ |
| 25 | #!cpp |
| 26 | class MyClass : public ParentClass // ParentClass is tickable! |
| 27 | { |
| 28 | public: |
| 29 | MyClass() : time_(0) {} |
| 30 | virtual void tick(float dt); |
| 31 | float getTime() |
| 32 | { return this->time_; } |
| 33 | |
| 34 | private: |
| 35 | float time_; |
| 36 | }; |
| 37 | }}} |
| 38 | |
| 39 | Implementation of !MyClass: |
| 40 | {{{ |
| 41 | #!cpp |
| 42 | void MyClass::tick(float dt) |
| 43 | { |
| 44 | this->time_ += dt; |
| 45 | |
| 46 | // Call tick(dt) of our parent |
| 47 | ParentClass::tick(dt); |
| 48 | } |
| 49 | }}} |
| 50 | |
| 51 | Sidenote: !ParentClass might look like this: |
| 52 | {{{ |
| 53 | #!cpp |
| 54 | class ParentClass : public Tickable |
| 55 | { |
| 56 | public: |
| 57 | // This function does what parents usually do |
| 58 | virtual void tick(float dt) |
| 59 | { std::cout << "Be careful!" << std::entl; } |
| 60 | }; |
| 61 | }}} |