View Full Version : How to get self object instance?
10-12-2005, 02:26 PM
Many times I need a struct member function to pass along a reference to another struct function, sending the calling struct as a reference.
For instance, in an object hierarchy, I might want one struct to create another object (struct instance) and pass itself to be stored in the "parent" variable of the new object. In most languages the calling object instance can be referred to as "this" or simply by the general struct name, but this isn't working.
10-12-2005, 11:27 PM
How about an example of whats not working for you?
10-12-2005, 11:39 PM
ok here's an example
mychild = (anode())
child.myparent = this
In this case, the child's parent gets set to undefined instead of getting an pointer to the parent node. If I use "anode" instead of this, the parent is set to the anode structure rather than to the particular anode instance that is the parent
10-12-2005, 11:53 PM
You could use a function argument to include the struct itself, like this:
name = "Original",
fn spawn me =
myChild = anode name:"Child" myParent:me
local test = anode()
format "Root : %\n" test.name
format "Child : %\n" test.myChild.name
format "Childs parent : %\n" test.myChild.myParent.name
10-13-2005, 03:03 AM
Haha, yeah I guess that works :)
However, this requires that you be calling the function from outside of its class. But using that concept, you could just have a func varaible called "this" or "me" and then initialize it upon creation so that any member funcs that needed it later on didn't have to be told the name
You could also adpot the singleton approach:
fn SingletonRDG = (
struct SingletonGenerator (
fn init = (
fn setPos newPos:[0,0,0] = (
fn getPos = (
if theInstance == undefined then (
format "Instance is undefined: init SingletonRDG()\n"
format "Singleton allready exists: returning %\n" theInstance
format "rudi: %\n\t %\n" (SingletonRDG())
the function generates the struct instance and therfore nows that it exists.
Inside the struct could spawn childs and stuff as it is a selfcontained object.
If you want to controll things, you talk to the object through its 'interface' (it is not interface, though!):
(SingletonRDG()).setPos newPos:[random 0 100, 0, 0])
10-13-2005, 01:12 PM
It doesn't even make sense for one instance to have a reference to another instance when you're talking about a singleton!
maybe I am wrong.
It allows only one instance of a class (= struct).
I found it usefull. But maybe it is the wrong name :)
10-13-2005, 03:36 PM
The only difference between a struct and a class is that if you don't specify whether the members are public or private, a struct assumes they are public while a class assumes they are private. They are really exactly the same in all other ways, it does not mean that a struct does not have multiple instances. In MAXScript everything is considered a struct because the concept of private data doesn't even seem to exist in MAXScript.
The problem that I was talking about only exists when you have many instances of the same class. In order to make a tree data structure where the root node can be traversed to find the child nodes, each node (which is a different instance of the same struct) must have an array which contains pointers to all it's child nodes. But, for a child to be able to traverse back up the tree to it's parent, each node also needs to have a parent field containing the reference to it's parent.
In this case, the code inside of any given struct instance would need a variable that pointed to that specific struct instance that it could report to other struct instances (of the same or different struct definition).
ok! I thought you don't like my singelton, I was searching weeks to get something like this running ... and it would be 'gooney' if it wasn't the thing I was looking for ;)
But I posted it for this reason:
I found the way of wrapping a struct inside a function usefull, because:
a struct cant return itself - because of the missing 'this'.
the wrapping function can intance the struct and set the instance inside the struct, so the struct knows who it is, or set the caller inside the struct so the struct knows who its parent is.
and this can be done without calling functions from the outside, which is a 'private' fake.
As you could call them from the outside, but if you don't do it, they are private :)
And comming from ECMA/flash I miss 'this', too :(
10-13-2005, 03:54 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.