• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

A few thoughts on overriding methods of lightweight objects

B

Bořek Tuleja

Guest
As 2.3.0 is about to be released I've been thinking a lot about this topic. We already know inheritance will be supported for lightweight objects but following this post https://help.yoyogames.com/hc/en-us/articles/360026355292-GML-Update-2019-Questions-Answers, very little has been said about method overriding.

Imagine you have two lightweight objects, one inherits from the other and you want both of them to implement a method called "move".

Code:
function Dynamic(speed) {
   move = function() {
       ...code...
   }
}

function Player(speed) {
   Dynamic(speed);
   
   move = function() {
       ...code...
   }
}
The Player object needs to implement its own version of the "move" method but now it actually only reassigns the "move" property so the reference to the original method will probably be lost.

I believe there might be a solution to this that's gonna work. You just have to edit the code a bit.

In the post mentioned above there was another question:

* The current proposed syntax presents a risk of unwanted behaviour if the new operator is omitted. For example:

function Vector2(_x, _y) {
x = _x;
y = _y;
mag = function() {
return sqrt(x*x+y*y);
}
}
From here both var v = new Vector2(0, 0); and var v = Vector2(0, 0); are legal if called from within an instance (the latter questionably so only because of the default return value), but the latter will silently do unwanted behaviour. Would you introduce some sort of class keyword to prevent the second call from compiling, or let it slide and become a logic bomb, or handle it some other way?

ANSWER - We will address this as a warning in the IDE when functions that are marked as a constructor (details will follow on how this will be done) are used outwith a new operator or functions that are being used in other code as constructors are called in a normal way, note: it would still be legal to call a constructor function directly from within another constructor.
So as you can see it's legal to call a constructor without the new operator within another constructor. And here comes my idea:

Code:
function Dynamic(speed) {
    move = function() {
        ...code...
    }
}

function Player(speed) {
    super = Dynamic(speed);
    
    move = function() {
        ...code...
        
        super.move();
    }
}
You just add a new property called super and assign the Dynamic constructor without the new operator. Now the super property could possibly hold a reference to the parrent object. The question is if the player's implementation of "move" doesn't reassign the parrent's method as well.

What do you think? Would this work?
 
B

Bořek Tuleja

Guest
By the way you don't want to use the new constructor because then you would have to write something like this to access parrent's properties:
Code:
function Dynamic(_x, _y, speed) {
    x = _x;
    y = _y;

    move = function() {
        ...code...
    }
}

function Player(x, y, speed) {
    super = new Dynamic(_x, _y, speed);
   
    move = function() {
        ...code...
       
        super.move();
    }
}

var player = new Player(0, 0, 5);

player.super.x = 4;
 
Last edited by a moderator:
Top