Shop OBEX P1 Docs P2 Docs Learn Events
smart way to find index of object? — Parallax Forums

smart way to find index of object?

msrobotsmsrobots Posts: 3,709
edited 2013-02-07 23:31 in Propeller 1
In spin I can declare multiple instances of a object all sharing code and data and having a own var section.
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

  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-07 23:07
    First of all I would simply add this ID to the start function, because this has to be called anyway.
    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:
    DAT
      objCounter byte 0
    
    VAR
      byte objectID
    
    PUB start
      objectID := objCounter++
      ....
    
  • msrobotsmsrobots Posts: 3,709
    edited 2013-02-07 23:31
    Yes.

    That will work. Thanks. Somehow I had the feeling it has to simple as that, but was mindfrozen.
    And it is really the way it should be, as objects should never have an idea about how they are used!

    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
Sign In or Register to comment.