[SOLVED][LIBGDX] Efficiency problem

First of all, I need to mention - this is all theoretical. Thus, there will be no codes. I just need an advice.
OK, Here it is:

I have a Cache class that caches the entities in my game.

Now, I have classes that draw these entities. Thus, they need to get the references for those entities from the cache memory.

The problem is, in my game, some entities come and go. It means that if they leave the screen(scrolled-out or the player shoots at them and kills them), they are out of the game and thus they are removed from the original memory. Each action on entities, whether is creating them or destroying them in also performed on the cache memory. For instance, if I decide to create an entity, a copy of this entity is sent to the cache memory.

Now, because of this, I need, in each frame of the game, to get the updated version of the cache memory. I cannot just get the cache once and that’s it.

So my question is: How can I keep the entities in the drawers classes updated without performing a query to the cache memory every frame?

If it’s impossible then what should I do: perform a query to the cache memory a lot of times or to go straight to the original memory where the actual entities are stored?

I would be really happy if there is a way to keep working with the cache memory, but to me, efficiency comes first

EDIT:

Alright guys I managed to solve this by implementing an Observer pattren. Everytime an entity is gone, the cache class notifies all the observers in the drawers class, managing to keep the the entities that needs to be drawn updated!

What is Cache class and what it does?

“I have a Cache class that caches the entities in my game.”

Basically it is meant for performence improvement - instead of addressing directly to the original source, a clone of that memory is created and provided to the client.

This class creates a clone of the original source and provides it to the relevant classes

So it creates copy of the entity?

Its really hard to understand what you mean because so of loose use of terminology.

Have you tried the Pool class?

Alright guys I managed to solve this by implementing an Observer pattren. Everytime an entity is gone, the cache class notifies all the observers in the drawers class, managing to keep the the entities that needs to be drawn updated!

I don’t get what you are struggling with here?

If you keep it simple, you can just have 1 list of enemies that is shared between all classes. Remember it is passed as reference, so if you add an enemy to the list in one class any references get updated.

A Cache class that caches entities? So, a list?

There is a client involved now? lol.

I think before you ask help on how to achieve something, you might want to get an understanding of what you, yourself want to achieve.


Now if I read into this a bit more, despite it being solved but whatever.

You have a class that is some sort of Entity Handler or Manager that holds a reference to all your in-game entities. You also have a separate class that is used to render Entities.

So the problem you are having is (or were having), how does one “decouple” both these classes to keep calls between them to a minimum, or non existant? After all, calling render code inside the Entity Handler/Manager class itself is not exactly ideal, even although performance is trivial and it is purely a code structure and program design issue.

Observer pattern is not really the answer here, imagine you have 20 enemies on the screen, terrain that can be destroyed and many particles/projectiles all flying about the screen. You are running towards 20 enemies with an AK-47 firing a round approx every 0.6s.

Now assuming you manage to empty a magazine worth of bullets (varies, I don’t sell AK’s for a living) and kill all the enemies, between the enemies dying, the bullets creating/being destroyed, the limbs blowing off…you just fired 100’s of events to quite possibly more classes than really necessary. Assuming all these things are entities, which they would be in most cases; this is very inefficient. Even more so if we take into account that the enemies would (hopefully) be shooting back (possible friendly fire?), calling more events.

Unless you have a more than 1 class that needs to know that an entity just spawned or was destroyed (which is entirely possible but you never really mentioned anymore than rendering), then you are far better off just giving your Entity Handler/Manager an instance of the renderer they can use to render entities. Have the renderer do all the work, remove all render code from the entities and abstract it away into a sprite class.

So you can literally just do:


// Grab dragon using entity factory
Dragon d = EntityFactory.createNew(EntityType.DRAGON);
// Add it to the list of entities
entities.add(d);
// 1 line call, renderer knows the rest
renderer.draw(d.getSprite());