Inheritence Refresher.

This is the inheritance tree of my game.

Actor->Object
->Character->NPC
->PC

Now if I store all my actor objects in one ‘queue’ that gets processed in the logic cycle,
What is the cleanest way to determine what type the current object is?

For example, if the current object in my iteration is a character i’m going to want to draw a health bar.
Thanks in advance.

LSP says you shouldn’t be trying to determine the exact type, and if you have to then you’ve done something wrong elsewhere. Either delagate the special case logic into the subclass (ie. have an Actor.draw() method) or store your Characters seperate from your Actors.

I’ve answered my own question. I think the best solution is to pass the graphics object to the current object and let it draw itself.

If I have a group of packages I always import, is there a way to import them as a group?

package = new package(java.util.ArrayList, java.awt.Color, javax.swing.JLabel);
import Main.package;

Something like that?

That ‘shortcut’ involves more typing than the original 0_o

Use an IDE (such as Eclipse) and you won’t have to worry about the import keyword ever again.
For example:
Type “new jl” followed by the ‘ctrl+space’ shortcut, and Eclipse will:-

a) Search the class path for all classes beginning with the letters “jl”, and if more than one is found, suggest to you the most likely.
b) Auto-complete the classname (along with correcting any incorrectly cased letters)
c) insert the appropriate import statement to use the class.
d) search the constructors, and if more than one is found, suggest to you the most likely.

I’m not saying you are wrong in doing that, but by doing so you are mixing the Model and the View; taboo in the world of MVC - but done all over the place elsewhere.

First, you have a sub-class of Actor called Object. Is this just for the thread or is it actually called this? Because you will probably encounter conflicts with java.lang.Object. I’d rename (i.e. GameObject or ActorObject).

Second, for helping with imports you can always use

import java.awt.*;

instead of

import java.awt.Graphics;
import java.awt.Image;

Third, if you want to keep the drawing and acting code separate you could use the Visitor design pattern which uses double dispatch. You keep the same structure where the Actor has a draw method, but it just calls to the Drawer to draw and passes itself in. The Drawer then does the rendering itself. i.e.

public interface Actor
{
    public void abstract draw(Drawer g);
}

public class GameObject
{
    public void draw(Drawer g);
    {
        g.draw( this );
    }
}

public class NPC
{
    public void draw(Drawer g);
    {
        g.draw( this );
    }
}

public class PC
{
    public void draw(Drawer g);
    {
        g.draw( this );
    }
}

public class Drawer
{
    public void draw(GameObject obj)
    {
        // code ommitted
    }
    
    public void draw(NPC obj)
    {
        // code ommitted
    }
    
    public void draw(PC obj)
    {
        // code ommitted
    }
}

The downside is that every concrete subclass of Actor needs it’s own draw class method so that the JVM can call the correct draw method.

Personally, I just implement the draw method in the Actor and never change any of the Actors variables (i.e. so that the draw method is read only).

instanceof

How efficient / mainstream is this for a game?

instanceof is refreshingly efficient (particularly when compared to “proper” reflection, which is usually quite slow), amounting to a handful of instructions IIRC. However it’s generally seen as a Bad Thing and can rapidly turn your game into an unmaintainable mess if you’re not careful.

I use instanceof all the time - but probably I’m just a bad programmer. :persecutioncomplex:

If that’s all you care about then just use an int field to indicate the type and switch on it :stuck_out_tongue:

instanceof is rarely the right option.

As a rule of thumb I only use instanceof if I have no real alternative. It might be bad style, but sometimes you just have to use the best of a bad situation.

I use instanceof alot. A quick search on my current project revealed that I’ve used instanceof 93 times. There are around 200 files.

This has been discussed before (http://www.java-gaming.org/index.php/topic,18750.0.html)
Note that the int field/switch solution is not faster than using instanceof in most of the cases.