I have class GameState laid out to basically hold an Array of Players, an Array of StarSystems, and a few other fields that need to persist from save to save. Each GameObject keeps track of its own id, which is also its index in its array as well as the id's of relevant gameobjects.

public class GameState implements Json.Serializable
    private Array<StarSystem> systems;
    private Array<Player> players;

    //Methods used to access data and some utilities

Each StarSystem in the GameState stores an Array of Planet's and a Star or three, which in turn hold information on temperature and habitablity. Class Star extends from Planet and has 0 habitablity. The StarSystem class also has references to all Fleets present in the system, their owners. Fleet class holds a list of Ship instances that are in the fleet and relevant information for each ship.

I designed this system for two reasons:

  1. I wanted to be able to call `Json.toJson(Object)` once when I save the game rather than go through and individually save every object.
  2. Before this my code was very unorganized and I had weird references to various things strewn through the code. This is cleaner.

Unfortunately this has also dropped the FPS down from 35-40 to 20-25, which ticks me off and would probably tick any user off as well. In other words, I am asking how I can improve this design?

Notes: The indexed nature means that for a Planet.ProductionQueue to add a new Fighter to Fleet SomeFleet of Player A, it must first call GameInstance.getInstance().getState().getSystem(parent_system).getFleet(fleet_target_id).addShip(new Fighter(owner_id, getLeastPowerfulCommandShip().getID()))

Note that I am also using libGdx, and that the Json class I spoke of is part of the library.


The problem is not the hierarchy of your game, the problem is that you are descending the entire hierarchy for every request.

I'm guessing that players look at a single system at a time, and that a player would "select" a fleet to add a Fighter to it.

Therefore, store local variables: StarSystem current, and Fleet selected Update these variables when the player changes the current fleet/star system. You don't need to descend the entire hierarchy to access an object.

Note: the objects selected should still be stored in the data structures you mentioned.

| improve this answer | |
  • I started out doing this but the libGdx Json class sent me into an infinite loop when serializing/saving the game state. I tried customizing serialization using Json.Serializable, For example, I might have instantiated a Ship by passing the Fleet it is in into its constructor and adding it to a list of Ship's in said Fleet. The problem occurred when I called Json.toJson(fleet) and it automatically read all the fields and tried to serialize them. It would, for example, read Ship[0] into a Json array and read its field parentFleet and serialize that into Ship[0] and so on and so forth. – JDSweetBeat May 26 '16 at 19:13
  • I am writing a Stack Overflow question on how to fix this problem. I think this answers why it is dropping the rate, though. – JDSweetBeat May 26 '16 at 19:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.