Asset - Scripts GML-Classes - GML script providing OOP features.

NikkoTC

Member
What is it?
GML-Classes is a script that provides some OOP functionality that allows you to define classes, their constructor and destructor, and call parent methods in overridden methods.

Why does it exist?
The main reason is convenience, in particular the convenience of calling parent methods.

When writing code, it may be necessary to implement more complex structures, with several levels of method inheritance.
But structures, due to the lack of an easy way to call the parent method, complicate the task.

To fix that, a special script called GML-Classes was created.
It is based on structs, but makes them more class-like in use.

Thanks to GML-Classes you can do this:


GML-Classes provides the following features:
  1. Constructor
    The constructor initializes the object (class instance) at the time of its creation.
    Here you can initialize data, reserve memory, resources, assign refs, etc.

  2. Destructor
    The destructor is executed when the object is destroyed.
    Thanks to it, you can deinitialize the object as you need.
    For example, break refs, free memory, resources, etc.

  3. Calling parent method
    The ability to call the parent methods allows you to get rid of duplication of the parent code in the overridden methods, which reduces the amount of code and, as a result, makes the code more maintainable and easier to read.

Highly recommended to check out the documentation.

Links:
 
Last edited:

NikkoTC

Member
rIKmAN, Thanks! I fixed it.

Has anyone already tried to work with this `extension`?
I would like to know if there are any problems with GMCS.
 

NikkoTC

Member
Just discovered GMCS doesn't work when exporting HTML5. The reason is that script_get_name returns undefined and because of this the required structures are not initializing. I have no idea how to fix it.
GMS2 Manual page did not help me.
Does anyone have any ideas?
 

NikkoTC

Member
I've added a special keyword "singleton".
It means that only one instance of this class can exist.
Instances of classes marked as singleton will be created when your game starts.
GML:
class cSingleton define {
    singleton;
    
    function _constructor() {
        show_debug_message("cSingleton constructor");
    }
    
    function do_something() {
        show_debug_message("cSingleton do_something");
    }
}
You can access to this instance like this:
GML:
global.cSingleton.do_something();

GML-Classes v1.0.3 is available on GameMaker Marketplace.
 

NikkoTC

Member
GML Classes v1.1.0

I have completely reworked gml_classes, now it's much better.

Warning:
To update, you will need to change the code in which you already used the previous version of GML-Classes.

Changes:
  • Class names no longer need the special cls_ or cX prefix (where X is any uppercase letter).
  • No longer need to write "register_as_super(classFunc)".
  • Now you can call the parent method using the "super" keyword, like this:
    GML:
    super.doSomething();
  • Removed "super" function, now it's a keyword as described above.
  • Removed "singleton" keyword (It's better to use functions):
    GML:
    /// @return {Struct.AssetManager}
    function AssetManager_inst()
    {
    static inst = create(AssetManager);
    return inst;
    }
    
    class AssetManager define
    {
    /// @ignore
    _constructor = function() {
    ...
  • Removed "cast" function. Use built-in "is_instanceof" instead.
  • Added function to get method names of class. "class_get_methods(classID)".
  • Added function "class_has_method(classID, methodName)" to check if class has method.
  • Added function "is_class_inst" to check if value is a class instance struct.
Tested on Windows VM and HTML5 with Runtime v2023.4.0.113 and v2023.6.0.139
 
Last edited:
Top