0

Let's say I'm programming a small RPG game. I have several instances of the Enemy class. They share common properties (they all have a name, an amount of life/strength/dexterity/a weapon, etc), but all these values are different for each enemy. I'm looking for an appropriate way to initiate these instances.

I'm new to programming and Java, so I'm looking for the best practice to properly organize information in my project.

My first idea was, when creating the instance of the game, to instantiate all the occurrences of the required enemies in the constructor of the Game object, and put everything in a vector. Something like Enemy e1 = new Enemy("goblin", 10, 14, 10, a_weapon, ...). But this can get very tedious if there are a lot of enemies, a lot of properties, it gets very hard to maintain, and I don't find very "logical" to put that in the constructor of the Game object.

I just discovered XML files, and it looks promising. So maybe I could put everything in an XML file, and parse it in my program to extract the data and create all the enemies from it. It could look like

<Enemies>
  <Enemy>
    <Name>"Goblin"</Name>
    <Strength>20</Strength>
    <Agility>20</Agility>
    <Life>20</Life>
    <Weapon>
      <Name>"Sword"</Name>
      <Damage>3</Damage>
    </Weapon>
  <Enemy>
  <Enemy>
  ...
  </Enemy>
</Enemies>

I guess I could write a function that parses the XML file, extract the data and create the vector of enemies automatically, so I just have to edit the XML file to modify the values.

However, and before I dig into this solution, I want to ask if this is the preferred method, and if not, what would be the most common way of managing this kind of situation.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
cyphics
  • 412
  • 4
  • 17
  • This is very much opinion based because it ties very closely to your data needs. XML is generally considered very wordy, with a lot of boiler plate to hold a relatively small amount of data -- but if it works for you, great! (Use a library like Jackson to parse it, btw, don't try to do it manually.) Or you could use JSON. Or you could stuff it into a database. It all depends on your use cases and is a design decision you need to make as the developer/architect. – Roddy of the Frozen Peas Jul 24 '18 at 17:31
  • As mentioned above, using JSON is a much better approach for me. In fact it depends on the design of you application. If you elect to go with an implementation that stores all the information in files, any file structure would serve you good (even CSV). Ideally for me this information should be saved in a database but since you're new I suggest to just go with the easier option. One last thing, I would suggest against using `Vector` for holding your enemies. Vectors synchronize on almost every operation and can have tremendous overhead. – akortex Jul 24 '18 at 18:16
  • 1
    Thanks to both of you. I now understand that I need to work on my architecture before I can go any further. – cyphics Jul 24 '18 at 21:59

0 Answers0