I'm creating a text RPG in Java, but I think this applies for a GUI as well with regard to the data structure. Each Level is a tiled map. Each Tile object has an (X,Y) coordinate, and references to adjacent tiles, but my question is with regard to storing object and NPC data on that Tile object. Depending on the size of the map I'm loading, this seems like a lot to load, but if that is the commonly accepted practice then I'll go with it.


I'm not sure where the problem is that you're asking about. If you need the data, you have to load it. I would personally keep NPCs in a separate list, and track them separately from all the actual tile data. That's how I make these kinds of games.

Because you only need to draw the tiles on-screen; but entities (NPCs) may have collision logic, events, etc. and should always be tracked, regardless of their position.

If you're worried about performance, then I say, don't; just code it, and if problems arise, you have working code to profile and optimize.

Edit: The way that I would code it would be to have a 2D array of tiles; each tile has the XY coordinates of the image that it displays, a "walkable" attribute, etc. NPCs/entities would be kept in a separate list, and they would either hold a tile XY coordinates for the tile they're stepping on, or hold a reference to a Tile object with an X and Y.

  • \$\begingroup\$ When you say "keep them in a separate list", how do you store your reference between an NPC and a tile? For instance, a battle with a certain group of NPCs only occurs when a player enters a tile? \$\endgroup\$ – user6214 Apr 4 '11 at 17:53
  • 1
    \$\begingroup\$ I'm guessing that this question is about managing large-scale maps with tons of stuff on them. In that case, I would keep one ADT with the tile data (tile image coordinates, walkable or not, etc.) and one with a list of all the NPCs. To give a C# reference, I would probably have a 2D array of some Tile class with ImageX, ImageY, etc. properties and a List<T> of some sort of Entity/NPC class that would have Tile XY coordinates. \$\endgroup\$ – ashes999 Apr 4 '11 at 18:03
  • \$\begingroup\$ How do you store the reference between the Tile class and the NPCs in the Entity/NPC class? Is your tile coordinates your "key"? \$\endgroup\$ – user6214 Apr 4 '11 at 19:17
  • 1
    \$\begingroup\$ I think I've used two approaches: the Entity has (TileX, TileY) coordinates, and (more recently) the Entity has a Tile reference, which in turn, has a (TileX, TileY) coordinate. As for the tiles, I would store them in an array, not a hashmap. \$\endgroup\$ – ashes999 Apr 4 '11 at 19:23

Your Answer

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