Handling Blocks & Entities Separately..

Hi there,

I’m looking for a better way to handle blocks and entities separately. I use a handler system which goes through a linked list of objects and ticks each one separately. I don’t know, but when many blocks are rendered on screen it becomes laggy. What is a better method of level design. (Yes, I’ve been recommended tiled and libgdx, I’m learning, but in the mean time I need something for straight java).

Thanks!

  • A

You need culling, which is only drawing elements that are on the screen. Also, try and reduce your draw calls to as little as you possibly can.

To add to that, LinkedLists are almost never a good idea, but the algorithmic side of things is probably where your problem is, so worry about that first.

Well the thing is, it all works great until I add a whole bunch of blocks to the screen. & Culling is something I do need, however it lags even if I just fill up the screen with blocks. Usually, and especially, when a whole bunch of blocks are together.

To be more in detail, I use this:
GameObject Super Class > Handler > Add/Remove/Tick/Render (In a linkedlist, I was recommended to use an array though). Next, I handler.render(); & handler.tick(); & in the initializing method of which ever state I’m in, I use the following to add to the handlers linkedlist: handler.addObject(new This(this, that, this, that, ObjectId.That);

I got this method of doing things from a guy on YouTube, RealTutsGML. It is just what I’ve used, for nearly a year now. I really want a new way of doing things entirely…

If it slows down with many blocks, that means your method doesn’t scale well. That’s an algorithmic problem.

Drawing blocks on the screen should be very fast, I honestly don’t understand your description, but it sounds over-complex and slow. Mind linking a video or something?

Sure thing, and sorry about the bad description. Been up for too long. :confused:
Anyways, this was the tutorial I watched a few months ago for the system I’ve been using for a while.

So watching that I immediately see this:

for(int i=0; i< object.size(); i++) {
    tempObject = object.get(i);
    ...
}

I could be mistaken, but LinkedList.get() has to iterate through up to half the list each time it is called. Massively inefficient esp. when all you are doing is iterating over the list, which is ironically kind of a strongpoint of LinkedList when done correctly:

for(GameObject obj : object) {
    ...
}

I suspect that the same occurs during collision detection, no?

I still maintain that you use an ArrayList, usage is basically the same, but it will preform better in the majority of cases, esp. when your code is otherwise an algorithmic attack on the LinkedList implementation. This is literally the worse way you can use a LinkedList. I don’t see any reason why he chose to use a LinkedList over the usually preferred ArrayList, and he is certainly not using any of LL’s strengths, but actually accidentally abusing it’s weaknesses!

Yes, I run the handler whenever collision happens, to move player, etc. I would love to try the array system, can you give me an example code of how I would make this work?

As the tutorial (what I watched of it anyway) doesn’t seem to use any of LL’s special features, you should just be able to change

[icode]LinkedList object = new LinkedList();[/icode]

to

[icode]List object = new ArrayList();[/icode]

Instant performance increase I suspect.
Do that for any LL’s you’re using.

EDIT: change object to objectList or something sensible while you’re at it.

When you’re watching tutorials, don’t just accept what the video creator is doing. Think about the code you are copying critically and see if there is a better way to do it. You should have researched linked lists and you would know that they are a bad ides to use in this situation. Learn by doing, not by copying!

I’m always instantly skeptical when I see LinkedLists. Most of the time (like in this situation) they are being used incorrectly or there is a much better alternative.

BurntPizza & opiop65:

Thank you so much for the help! I’m definitely going to give this a try. I knew the problem was with the handler, I just didn’t know that the difference between a LinkedList and an Array could be so major! ^^

If it fixes your problem, congratz. It may not however, and the next step is to break out the profiler and find out what parts of the code are slow. Google around for “jvisualvm” and have fun.