1

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?

Keelx
  • 907
  • 3
  • 17
  • 26

2 Answers2

2

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++.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Thanks, and yes I know that that design is 'questionable'. I may not have been clear, but I'm not actually going to use this design, I was just wondering about it. But the creation of object types using scripts is actually something I may do in the future. – Keelx Oct 28 '11 at 22:17
  • @Keelx: As long as you define "type" to mean something other than a C++ class, there's nothing stopping you from making types via script. You'll just have to use a mechanism provided either by another language or written yourself. – Puppy Oct 28 '11 at 23:17
1

Game Maker enables us to control game functionality through "objects", each composed out of "events", which are triggered at certain times during a game. Within events are "actions". Firstly, it's worth noting that comparing Game Maker development with C++ is like comparing chalk and cheese. However, theoretically speaking, I'd imagine you could mirror GM functionality (albeit very inefficiently) in C++ as follows:

The base object class could look something like this:

class CObjectBase
{
public:
    CGameSprite* sprite;
    int          x, y;
    ...

    virtual void onEventCreate( void ) {};
    virtual void onEventDestroy( void ) {};
    ...
    virtual void onEventKeyPressedUp( void ) {};
    virtual void onEventKeyPressed...
    ...
    ... (there are lots of these)

    // The draw event in GM (from memory) had in-built functionality:
    virtual void onEventDraw( void ) 
    {
        CGameEngine::getSingleton()->DrawSpriteAtLocation( sprite, x, y );
    }
};

You'd derive from this class and override the functions ("events") that are relevant to your object (the statements that compose these functions are your "actions"). Then there would be some sort of object instance manager singleton class which holds a list of all object instances in the current "room" and loops through each every frame (and handles instancing), triggering relevant events by calling their respective functions.

Interestingly, this actually roughly demonstrates why a system like Game Maker lacks a degree of efficiency. There is additional, unnecessary overhead that exists in order to keep options open for the developer. The bloated base object that all objects derive from is often overkill for specific situations. For example, imagine an object with just two events used out of 50 - the object manager still blindly checks for all these other events even if they aren't utilised. Obviously optimisations can be made, but overall, the breadth of the engine ultimately results in reduced performance.

As for your query relating to a single 'var' type, as has been stated already, this is more a characteristic of scripting, not C++. This proves that Game Maker cannot simply be modeled by C++ alone.

Alex Z
  • 2,500
  • 2
  • 19
  • 23
  • It's been a long time, but -1 for Singleton. – Puppy Aug 13 '12 at 12:52
  • Sorry, but that's not enough of a justification for a -1 IMO. Is there something *wrong* with the answer, bar your distaste for the notion of a singleton? (which, I might add, was really just for illustrative purposes) – Alex Z Aug 14 '12 at 04:24
  • Appearing to advocate one of the most permanently crippling "patterns" ever devised is certainly enough for a -1. – Puppy Aug 14 '12 at 13:14