- Home /
Referencing the object that a construtor is currently constructing/working on.
In the code below, I would pass the name of the object which is being constructed through the n parameter in order to create a record in the currentDialogue dictionary which would pair that object's ID with its actual name. Assuming that the object has already been created and is referenceable when the constructor works on it.
class Node{
function Node (i : String, n : Node){
id = i;
currentDialogue[i] = n;
}
var id : String;
}
If I wanted to construct an object:
n1 = Node ("Why have you returned?",n1);
My question is twofold:
Is it possible to use a reference to the object which is currently being constructed in the constructor for that object's class?
If it is possible, Is there a better method than what is used in my sample code?
Answer by Bunny83 · Jun 13, 2011 at 10:57 PM
You can't pass a reference to the created object because it's returned be the constructor. So outside of the constructor the object available after the constructor has finished.
But you can use this
:
class Node{ var id : String;
function Node (i : String){
id = i;
currentDialogue[i] = this;
}
}
Inside of all non static functions of a class, this references the current instance. It's one of the most important things in OOP ;)
Thank you. I thought there would be a very simple solution. I'm new to coding, so I'm ignorant to some of the most basic concepts.
Your answer
Follow this Question
Related Questions
Calling inherited constructor 3 Answers
Recognizing a Mouse Click between Scripts 1 Answer
Cannot use custom type as key for dictionary 0 Answers