Invoking Constructors - Grisgram/gml-raptor GitHub Wiki

From Version 2.3 and later, raptor will support invoking your constructors for struct classes you create.

To do this, you have to tell the Savegame System, which constructors to call.
This is no complicated task, you only have to "register" your class with the savegame system, so it knows, that this is a constructor that needs to be invoked, when the game gets loaded:

Just just have to call construct(classname) in the constructor of your class. That's all, this class is then registered.

function MyInventoryItem() constructor {
    
    construct("MyInventoryItem");  // This registers the class with the Savegame System

    // define your class, (static) methods, and everything as you did before.
    // just add that savegame_construct call at the beginning of your constructor and you're done.

}

However, as GML does not have a clear object oriented design, there is one drawback you have to accept:


The constructor must be invoked without any arguments.


The call to savegame_construct registers the class name (the parameter must be exactly the class name you want to construct, and it is case-sensitive!) and a marker for every instance of this struct-class is set when saving the game. Upon load, the Savegame System finds those markers and invokes the constructor of the class before assigning all the loaded member values to it.

Invoking constructors and restoring classes from a savegame faces some limitations you must be aware of. Read about it in Unrecoverable classes.

⚠️ **GitHub.com Fallback** ⚠️