Author Topic: setName  (Read 2082 times)

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
setName
« on: February 26, 2008, 04:17:09 pm »
I'm not quite clear if the setName requires a unique name or not.  I'm interested in using simple collision to check for a type.  Since the ID is unique, and returned,  I can set the name to a type, and can get it that way, I think.  Will that work, or do you have another suggestion?
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: setName
« Reply #1 on: February 26, 2008, 04:45:26 pm »
It has be unique, but if you want to encode a type information in it, just add a number behind the type's name and increase it for every object.

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Re: setName
« Reply #2 on: February 26, 2008, 09:28:06 pm »
Let's see.  I'm just checking what else I can do.  Will the ID work on an extended class somehow and tell me what the extended class is?  Like, I have a class box that is extended from Object3D, can I find that out from the ID?  Actually, it's extended from GravityObject which is extended from Object3D.
« Last Edit: February 26, 2008, 09:29:43 pm by fireside »
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: setName
« Reply #3 on: February 26, 2008, 10:11:24 pm »
No, but you can use the instanceof operator (albeit some claim that this isn't very good code and to a degree, i agree, but sometimes it's all you can do...). You can do something like

Code: [Select]
if (yourObject instanceof Box) {
   System.out.println("It's a box!");
}

Or add an abstract method to your common base class (derived from Object3D of course) that offers a way to identify the type and make all your objects extend this class and override that method with a specific implementation that says "box" for box, "sphere" for sphere etc.

There are other, more object oriented approaches. Sometimes, the double dispatch pattern can be handy for example. But that's another  discussion... ;D


Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Re: setName
« Reply #4 on: February 26, 2008, 10:49:12 pm »
I think the abstract base class method would be the best.  There might be other things I will need to add in the future.  Thanks.
click here->Fireside 7 Games<-