Erik Leppen
Member
I'm trying to wrap my head around the new 2.3 functionalities and end up getting errors I have no clue about. Can someone please tell me how to actually use these new functions in order to gain any advantage over the old way of doing things?
What i want to do is couple any scripts that "belong" to an object, as methods of that object. However, when I try to define them, and then call them, the "self" variable doesn't point to the instance that owns the method. How do I get access to that instance?
In GM2.2 I had an object "nodeobject" and a function "node_unlink(node)". Now I want to have a node object with a method "unlink()" in each instance, so that instead of node_unlink(mynode), I can say mynode.unlink(). This sounds reasonable, right? So here's what I try to do to accomplish that.
The "node" script with the methods:
Another script where I define some function that happens to need the unlinking functionality of nodes:
Event code in an event of an object called "game":
The error I'm getting is that the variable "game.track" is not defined. Er, no, I don't want game.track, I want node.track, that's why I said "self.track" in the definition of the unlink() method. So, how should I get the instance that owns the method from within the method, no matter where it's called? I found a method_get_self, but that requires the method as its argument, but I don't think I can reference the method from within its own code... (or can I? If so, how?)
I tried replacing self with nothing (i.e. just using "track"); doesn't work. I also tried replacing self with newnode; also doesn't work.
How should I go about this? Or am I doing this all wrong? Is there a good tutorial about how to use this whole "method" stuff? Remember, the reason I want this is organisational. I had a working game in 2.2, but I want to benefit from the new stuff.
Any insights welcome
What i want to do is couple any scripts that "belong" to an object, as methods of that object. However, when I try to define them, and then call them, the "self" variable doesn't point to the instance that owns the method. How do I get access to that instance?
In GM2.2 I had an object "nodeobject" and a function "node_unlink(node)". Now I want to have a node object with a method "unlink()" in each instance, so that instead of node_unlink(mynode), I can say mynode.unlink(). This sounds reasonable, right? So here's what I try to do to accomplish that.
The "node" script with the methods:
GML:
function node_create_gm23 () {
var newnode = instance_create_depth(0, 0, 0, nodeobject);
newnode.track = noone;
newnode.index = undefined;
newnode.unlink = function () {
if instance_exists(self.track) { //this gives the error
self.track = noone;
self.index = undefined;
}
}
return newnode;
}
GML:
function nodes_unlink_all(array_of_nodes) {
var nodelen = array_length(array_of_nodes);
for (var nodeind = 0; nodeind < nodelen; nodeind += 1) {
var nod = array_of_nodes[nodeind]; //this is a nodeobject instance - I checked
nod.unlink();
}
}
GML:
var no1 = node_create_gm23();
var no2 = node_create_gm23();
nodes_unlink_all([no1, no2]);
I tried replacing self with nothing (i.e. just using "track"); doesn't work. I also tried replacing self with newnode; also doesn't work.
How should I go about this? Or am I doing this all wrong? Is there a good tutorial about how to use this whole "method" stuff? Remember, the reason I want this is organisational. I had a working game in 2.2, but I want to benefit from the new stuff.
Any insights welcome