Notifications
Clear all

[Closed] How to get self object instance?

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.

9 Replies

How about an example of whats not working for you?

-RyanT

ok here’s an example

 
struct anode
(
mychild,
myparent,
fn spawn=
(
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

You could use a function argument to include the struct itself, like this:

(

struct anode
(
	name = "Original",
	myChild,
	myParent,
	
	fn spawn me =
	(
		myChild = anode name:"Child" myParent:me
	)
)

local test = anode()
test.spawn test

format "Root : %
" test.name
format "Child : %
" test.myChild.name
format "Childs parent : %
" test.myChild.myParent.name

)

Cheers,
Martijn

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

 rdg

You could also adpot the singleton approach:

 	fn SingletonRDG = (
 		struct SingletonGenerator (
 			myDummy,
 			fn init = (
 				myDummy=dummy()
 			),
 			fn setPos newPos:[0,0,0] = (
 				myDummy.pos=newPos
 				myDummy.pos
 			),
 			fn getPos = (
 				myDummy.pos
 			),
 			start=init()
 		)
 		if theInstance == undefined then (
 			format "Instance is undefined: init SingletonRDG()
"
 			theInstance=SingletonGenerator()
 		)else(
 			format "Singleton allready exists: returning %
" theInstance
 			theInstance
 		)
 	)
 	--
 	format "rudi: %
	 %
" (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])

Georg

It doesn’t even make sense for one instance to have a reference to another instance when you’re talking about a singleton!

 rdg

It doesn’t?
maybe I am wrong.

It allows only one instance of a class (= struct).

I found it usefull. But maybe it is the wrong name

Georg

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).

 rdg

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

Georg