Main Menu

setName

Started by fireside, February 26, 2008, 04:17:09 PM

Previous topic - Next topic

fireside

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

EgonOlsen

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.

fireside

#2
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.
click here->Fireside 7 Games<-

EgonOlsen

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


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


fireside

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