1
\$\begingroup\$

I am developing a simple text-based game from scratch in Java. In this game, the user is able to collect items and store them in an inventory.

My question is: how should I organize the internal structure of my game so that I can load these items in an efficient way?

I need to be able to create fresh instances of an item whenever the user acquires an item of that type, or when instantiating any feature in the game that contains items. In order to do so, I feel like I need some sort of "master list" of all items that I can just copy out of by indexing into the correct location in the list.

I've come up with two potential solutions:

  • Hard-code this entire giant list into a data structure within my game

  • Somehow load these items from an XML file at startup and then populate the "master list" of items on the fly

Pros of option 1:

  • Relatively easy
  • No IO time
  • Allows me maximum efficiency in designing the actual item objects

Cons of option 1:

  • Hard to maintain
  • Bloats source code
  • Very tedious to implement

Pros of option 2:

  • Easy to add/remove/modify items
  • Allows the user to customize the game if they want to

Cons of option 2:

  • Requires that I design my objects much more carefully so that they can be built on the fly and stored

  • Requires that I perform a deep copy from the master list each time I need a new copy of a specific item

  • I have to write an "item-builder" that is capable of translating the XML input into an actual item object in memory.

Neither of these ideas feels very good to me. Is there something obvious I've missed?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Most games that need a lot of items go with option 2. The process of saving and loading items is called serialization and deserialization, so you're likely to find useful resources and techniques if you search those terms. Ultimately though, StackExchange isn't the best place to ask "should I use option A or B?" questions. You're a skilled developer, you've found two working options, and accurately assessed their pros and cons. You can make either one work. The judgement of which set of pros is more palatable and which cons more tolerable for your needs is up to you alone. \$\endgroup\$ Commented Jul 11, 2020 at 2:25
  • 1
    \$\begingroup\$ Thank you for your advice. I'll try and build some test-benches for both systems and see how easy they are to work with in a vaccum. After that, I suppose I can always "black box" this part of the code to keep if from getting to entangled with the rest of the software. Thanks! \$\endgroup\$ Commented Jul 11, 2020 at 5:39
  • \$\begingroup\$ you can use annotations to automatically map xml-content to java-objects, see also vogella.com/tutorials/JAXB/article.html \$\endgroup\$ Commented Jul 23, 2020 at 6:01

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.