This week we’re going back to our Animation system but not to fix bugs, instead, we’ll be extending it to add frame actions. A frame action is a function that is called during a specific frame in an animation. For example, as part of a death animation, we may want a function to be called on the last frame that removes the dead character and performs any necessary cleanup. If you’ve used the Unity game engine before then you may have created Animation Events, which provide a similar feature to the frame actions that we’ll be writing today.
I’ve created an alias for an std::function. If you haven’t used them before an std::function is a “general purpose polymorphic function wrapper”. It is a class that can wrap a callable element (in our case a function). We’ll use it to store functions for an animations frame to be invoked during the correct frame. How we invoke these functions will be shown shortly, it is very similar to calling a typical function. How to create and bind the functions is a topic for another week, when we create our first frame action, but it is not difficult once you understand the syntax.
Before deciding on how to store our frame actions I looked into the efficiency of std::functions as I assumed there would be an overhead in calling them, which is the case. However, the overhead is much smaller than I first assumed. In fact, it is the same as a virtual function call. This means something very interesting for how our code can be structured. We could remove the hierarchy prevalent in our Object and Component code and move to something more akin to what is shown here. This is definitely something I will look into for a future tutorial but for now let’s get back to implementing action frames.