0
\$\begingroup\$

Currently I am using arrays to handle all of my game objects in the game I am making, and I know how terrible this is for performance. My question is what is the best way to handle game objects and not hurt performance?

Here is how I am creating an array and then looping through it to update the objects in the array:

public static ArrayList<VboCube> game_objects =  new ArrayList<VboCube>();
/* add objects to the game */

while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
        for (int i = 0; i < game_objects.size(); i++){
            // draw the object
            game_objects.get(i).Draw();
            game_objects.get(i).Update();
            //world.updatePhysics();
        }
}

I am not looking for someone to write me code for asset or object management, just point me into a better direction to get better performance. I appreciate the help you guys have provided me in the past, and I dont think I would be as far along with my project without the support on stack exchange!

\$\endgroup\$
  • 6
    \$\begingroup\$ In which part of your code do you have perfomance problems? Did you profile? \$\endgroup\$ – Maik Semder Nov 3 '12 at 21:40
  • 3
    \$\begingroup\$ Are you sure your performance problems come from the way you store entities? You may want to look at the way the Artemis framework store entities. It uses what they call a Bag. From the doc: Collection type a bit like ArrayList but does not preserve the order of its entities, speedwise it is very good, especially suited for games. Maybe you should check it out :) \$\endgroup\$ – nathan Nov 3 '12 at 21:48
  • \$\begingroup\$ I will take a look at this Nathan, thank you for the recommendation. \$\endgroup\$ – user22020 Nov 4 '12 at 2:04
1
\$\begingroup\$

As far as storing and retrieving game objects is concerned, you could use a native array but an ArrayList is already nearly as fast as it gets.

Now I'm making a guess, not knowing what your list of game objects contains nor how the objects are drawn and updated, but I'd venture that maybe you do not need to update and draw all objects on every frame. That or the draw or update code itself is inefficient.

HTH

\$\endgroup\$
  • \$\begingroup\$ All the update code does is update the location of the object, with that of the jBullet collision object. The update code is so basic that it would not be the cause I dont think. If an ArrayList is the best method I guess I need to look at other places I can improve on code wise. \$\endgroup\$ – user22020 Nov 4 '12 at 2:08
  • 3
    \$\begingroup\$ In this regard, I strongly support Maik's suggestion that you use a profiler. It's the best way to avoid optimizing parts of your code that are already fast enough. \$\endgroup\$ – Thomas Dufour Nov 4 '12 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy