• 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!

GML [Solved] Is there a way to refer to the 'this' of a struct?

H

Hieran_Del8

Guest
EDIT: My apologies, I posted in the wrong forum. I Meant this for "Programming".

I'm creating a tree structure, and wanted to set the leaf nodes to point to their parent, using something akin to the this keyword. As of now, I had written:
GML:
function _tree_node(_data) constructor {
    parent = undefined;
    children = [];
    data = _data;
   
    appendNode = function (node) {
        array_push(children, node);
        node.parent = self;
        return self;
    }
   
    insertNode = function (node, pos) {
        array_insert(children, pos, node);
        node.parent = self;
        return self;
    }
   
    removeNode = function (node) {
        for (var i = array_length(children) - 1; i >= 0; i--) {
            if (children[i] == node) {
                node.parent = undefined;
                array_delete(children, i, 1);
                break;
            }
        }
        return self;
    }
   
    copy = function () {
        var newNode = new _tree_node(data);
       
        var childCount = array_length(children);
        for (var i = 0; i < childCount; i++) {
            var newChild = children[i].copy();
            newNode.appendNode(newChild);
        }
       
        return newNode;
    }
}

function _tree() : _tree_node(undefined) constructor {
    copy = function () {
        var newNode = new _tree();
       
        var childCount = array_length(children);
        for (var i = 0; i < childCount; i++) {
            var newChild = children[i].copy();
            newNode.appendNode(newChild);
        }
       
        return newNode;
    }
}
But I'm not confident that returning self or setting the child's parent to be self actually is anything but the constant -1. I realize in with statements, there's special interpretation of what self means, but am I using self correctly so far?
 
Last edited by a moderator:
Top