In most languages with a class system, there's some way to call the "base" version of a method from within an overriding method. It's usually called something like super
or base
. I know GML's "object-oriented" features are new and still not entirely there, but I was wondering whether there was a way to do the same thing in GML.
To be clear, I know how to do this with event inheritance (the event_inherited
function), but here we're talking about the functions of structs, not the event responses of game objects. I also know that this can be done with struct constructors; I'm wondering if there's an equivalent for the struct's other functions.
An example of what I'm talking about. Let's say I have the following two kinds of structs:
function Animal(_name) constructor {
name = _name;
static Speak = function() {
show_debug_message(name + " makes noise");
}
}
function Dog(_name) : Animal(_name + " the dog") constructor {
static Speak = function() {
base.Speak(); // <-- THIS DOESN'T WORK, OBVIOUSLY
show_debug_message("(The kind of noise a dog would make)");
}
}
And I do this:
var bonzo = new Dog("Bonzo");
bonzo.Speak();
Then the output I'm looking for would be:
Bonzo the dog makes noise
(The kind of noise a dog would make)
GameMaker does not have a conventional base
keyword, but static variables are still variables, so you can get the parent field before you "override" it in a child constructor:
function Animal(_name) constructor {
name = _name;
static Speak = function() {
show_debug_message(name + " makes noise");
}
}
function Dog(_name) : Animal(_name + " the dog") constructor {
static Animal_Speak = Speak;
static Speak = function() {
Animal_Speak(); // <-- THIS DOESN'T WORK, OBVIOUSLY
show_debug_message("(The kind of noise a dog would make)");
}
}
function SmallDog(_name) : Dog(_name + " (small)") constructor {
static Dog_Speak = Speak;
static Speak = function() {
Dog_Speak();
show_debug_message("(except high-pitched)");
}
}
var dog = new SmallDog("Bonzo");
dog.Speak();
Bonzo (small) the dog makes noise
(The kind of noise a dog would make)
(except high-pitched)