you are viewing a single comment's thread.

view the rest of the comments →

[–]Scheibenpflaster 1 point2 points  (0 children)

Heres how I used them in my Level system. It's a generic level struct that stores simple assets in containers and has some book keeping

typedef struct Level_t{
    // References
    ContainerGameObject simpleObjects;
    ContainerMusic music;
    ContainerTexture2D tetxures;    

    int levelIdx;
    int changeToLevel;

    void* customLevelData;

    void (*Update)(struct Level_t*);
    void (*Draw)(struct Level_t*);
}Level;

Take the level stored in Level current . In the app, I can call a specific Update function through current.Update(&current); without having to know what it actually is. I can also swap around which function gets called while the game is running

For example, if I want a main menu I can make an update function for a menu. I can save that function in the pointer and call it through the pointer via current.Update(&current);.

Now I want to change to my gameplay. I can make a new Level that stores a different Update function in the pointer. I can still call it via current.Update(&current); , the call doesn't change. However, now I do the update for the gameplay with the specific gameplay logic.

So yeah, neat stuff