Search code examples
c++objecttypesgame-maker

GameMaker-like functionality in C++


When I was younger, I used a tool called Game Maker a lot. I started to learn to program from it. I'm far beyond that now, but looking back on it, some of it's features and designs are quite interesting. I'm wondering- How would I implement functionality similar to this tool using C++? I'm wondering about:

  • Objects/classes

Game Maker had a list of 'Objects' that you would create which were essentially just different classes all derived from the same base class (I'll call it GameObject for now) amd a system function called 'instance_create' that would take an object type as a paramater. In c++ this would look something like this (Though syntatically very incorrect):

class MyGameObject : GameObject
{
    //...
}
GameObject instance_create(class objecttype)
{
    objecttype newinstance = new objecttype();
    return newinstance
}
GameObject* gameobjectinstance = instance_create(MyGameObject);

How would I go about implementing that?

  • system variables/functions

Game Maker had system variables and functions that could be accessed from anywhere. Period. Anywhere. I'm thinking globals, but I know that's bad design. I'm thinking Having a global class, and have the variables/functions as static, but then they cannot be altered. How would I do that?

  • var

Game Maker had only one data type- a var. It could be a string, an integer, a decimal, anything. And there were system functions for conversion between those.

Lastly, how could I define the object types in some kind of script? Like, if I want to add a new type of object, create a new script? I don't think C++ can create object types at runtime, so how would I do this?


Solution

  • Using a template.

    template<typename T> GameObject* instance_create()
    {
        return new T;
    }
    GameObject* gameobjectinstance = instance_create<MyGameObject>();
    

    However, the design you have specified is highly questionable (at best) and definitely not suited to C++. You should strive to implement a well-designed system, and one appropriate to the language, not re-create a system from the past.

    I especially think that since you mention run-time interpretation of scripts, that in fact the GameMaker classes and C++ classes have nothing to do with each other. And you definitely cannot create C++ classes at run-time, nor can you pass types around at run-time, nor can you instantiate templates at run-time.

    You would be best suited simply whipping out a scripting language, such as Lua, and writing only the necessary high-performance components in C++.