Skip to content

How to use

NikkoTC edited this page Sep 4, 2023 · 4 revisions

Install

To start using GML-Classes, just add gml_classes script to your project.

Class definition

To define class use a special keywords class and define.

class MyClass define
{
	//...
}

Class inheritance

If you need to inherit from another class, then use keyword extends like this:

class MyClass extends MyParentClass define
{
	//...
}

Constructor and destructor

A constructor is a method that is called when an instance of a class is created.
A destructor is a method that is called when an instance of a class is deleted.
The constructor method must be named _constructor, and the destructor - _destructor.

class MyClass define
{
	_constructor = function() {
		// initialize all variables here
	}
	
	_destructor = function() {
		// deinitialize here, free memory
	}
}

Calling parent method

To call parrent method use keyword super.

class MyClass extends MyParentClass define
{
	_constructor = function(name) {
		// call parent constructor
		super._constructor(name);
		// do something else
	}
	
	_destructor = function() {
		// call parent destructor
		super._destructor();
	}
	
	doSomething = function(arg0, arg1, arg2) {
		// call parent method
		super.doSomething(arg0, arg1, arg2);
		// do something else
	}
}

Class instance

To instantiate a class, you must use the create function instead of the new keyword as you would for structures, otherwise the class instance structure will not be initialized properly and you will get an error. To delete class instance you must use destroy function instead of delete keyword.

// instantiate class
inst = create(MyClass, "Teapot");

// use it
inst.doSomething();

// don't forget to delete it with the `destroy` function
destroy(inst);