smart way to find index of object?
msrobots
Posts: 3,709
In spin I can declare multiple instances of a object all sharing code and data and having a own var section.
I then can use 4 different sub objects in my main code
what I am looking for now is a way for my sub-object to find out if it is called as sub[0] or called as sub[1] etc.
I know that I can put a method into my sub-object to set a VARiable and call this from my main-object aka
but I would like to avoid this.
So I need some brainstorming here.
Any hint welcome
Mike
OBJ sub[4] : "subobject"
I then can use 4 different sub objects in my main code
sub[0].Start sub[1].Start ...
what I am looking for now is a way for my sub-object to find out if it is called as sub[0] or called as sub[1] etc.
I know that I can put a method into my sub-object to set a VARiable and call this from my main-object aka
sub[0].setID(0) sub[1].setID(1) ...
but I would like to avoid this.
So I need some brainstorming here.
Any hint welcome
Mike
Comments
And it is really the way it should be, as objects should never have an idea about how they are used! This is why objects do not have access to variables, data sections and functions on the levels above!
But ... wait ... there is a possibility!
A DAT section is unique per object. So you can simply have a counter in the DAT-section and the current value is copied to the ID by the start function and then increased for the next instance:
That will work. Thanks. Somehow I had the feeling it has to simple as that, but was mindfrozen.
I absolutely agree with you. And for exactly that reason I need this. I am playing around with some multiprop projects and want to build some generic 'wrapper' I can use to 'serialize' objects by replacing the original with a wrapper around a communication-cog and having the real object running on another prop.
My Goal is to replace the subobjects without the need to change the main code. For that reason I want to avoid to pass it as additional parameter in a Start method.
So ein Dankeschoen aus Californien nach Hessen...
Enjoy!
Mike