Object Problem

Hello all. Sorry about the vague topic title, however I really had no idea what to call it. I’ve never seen a problem like this before.

I’m using LibGDX and Java 8.

I was working on the level editor for my game and created an ArrayList to store the level objects. I also had another ArrayList that stored any objects that I selected. I was trying to make it so that any selected objects had a red box around them, and otherwise they just had an orange box. The problem occurred when I tried to use the contains() method on the selected objects ArrayList. It was telling me that every one of my objects was contained in that ArrayList, even though when I printed out the size, it only said one. I spent quite a while trying to figure the problem, and stepped through my code multiple times. Eventually, I tried printing out the two objects in the list and I got this:

com.exavolt.game.world.objects.Wall@0
com.exavolt.game.world.objects.Wall@0

I don’t remember what the @0 part is, but I think that if they are different objects, they should be different. Or at least it usually isn’t 0 and with only two different objects it should be different for each. I spent a little while looking around in my code to see if anything weird was going on, but I couldn’t find anything, so I stripped it down to the most basic thing I could:

objects = new ArrayList<WorldObject>();
objects.add(new Wall(4, 6, 2, 2));
objects.add(new Wall(-2, -2, 3, 3));

and the stuff in wall is just

private Rectangle bounds;
	
public Wall(float x, float y, float width, float height) {
	bounds = new Rectangle(x, y, width, height);
}
	
public Wall(Rectangle bounds) {
	this.bounds = bounds;
}

when I printed that, I got the same thing.

I then took that most basic section and created a new java project and put it in there without any LibGDX stuff. I got what I should be getting:

com.dragon.blah.main.Wall@28d93b30
com.dragon.blah.main.Wall@1b6d3586

Thinking that there was some possibility that it may just be some kind of error in the project configuration, I copied all my code to a separate LibGDX project, however I ended up with the bad output. I tested this on both Windows and Mac and got the same results on each. What could I be doing in my code that is causing this? Thanks for the help!

The part after the @ literal is the hashcode, so it seems that LibGDX is overriding the hashcode somewhere, and setting it to 0. You can get your IDE to generate a new hashcode for your class.

And if you want to rely on printing, override the toString method and return a descriptive string, I prefer this. For example, it could be


@Override
public String toString()
{
    return "[x=" + bounds.x + ", y=" + bounds.y + ", w=" + bounds.w + ", h=" + bounds.h + "]";
}

I had a thought and went and checked it out and it’s all good now. My Wall class extends my WorldObject class which extended the Ashley AI Entity class. When I removed that extends for the Entity class, it was fixed. Looks like the hashcode for it was determined by the Entity ID, which ended up all being 0. Thanks SHC for your reply :slight_smile: