0
\$\begingroup\$

I have an "Item" class here that is used to create items.

public class Item {
    private String name;
    private int id;
    private int worth;

    //constructor
    Item(String _name, int _id, int _worth) {
        name = _name;
        id = _id;
        worth = _worth;
    }

    public String getName() {
        return name;
    }
}

This works fine and dandy.. but I don't know how to give them unique ID's, I thought about using

private static Item[] items;

and then when they're created add them to this array, but is that the only way? Also where do I put my code to initialize all the various items in the game? Should it be in the main method, or have a loader of some sort, or have them declared straight away in the Item class using

private static Item apple = new Item("Apple",0,3);

?

\$\endgroup\$
4
  • \$\begingroup\$ Why do you need id's? \$\endgroup\$
    – clabe45
    Aug 21, 2017 at 4:25
  • \$\begingroup\$ We need more information. \$\endgroup\$
    – clabe45
    Aug 21, 2017 at 4:27
  • \$\begingroup\$ What other way would be easy to reference? \$\endgroup\$
    – baiomu
    Aug 21, 2017 at 5:00
  • \$\begingroup\$ What is the context in which you are referencing? \$\endgroup\$
    – clabe45
    Aug 21, 2017 at 13:52

3 Answers 3

2
\$\begingroup\$

Most games don't hardcode things like item data. They instead put all the game data into files. At game start those files are read, parsed and used to generate all the item objects. The unique ID's would be part of those game data files.

This has a lot of advantages. It allows non-programmers to add content without having to make code changes, it makes your content updates much leaner, it makes the game more modding friendly, it allows you to create external tools to edit your item database, it makes code reuse easier etc. etc..

I thought about using private static Item[] items;and then when they're created add them to this array, but is that the only way?

There is never only one way to do something in programming. What you could do instead:

  • Use an ArrayList<Item>. ArrayList is an object-oriented wrapper around arrays. One of its biggest advantages is that you can create one without having to state a fixed size (it grows automatically when you .add items by internally re-allocating the content to larger and larger arrays).
  • Use a HashMap<Integer, Item>. This class is a bit slower, but doesn't waste any space when you have unused ID numbers. You can also use a different key than an integer. A HashMap<String, Item>, for example, allows you to use human-readable names instead of numbers as item ID's.

There is no best solution. Only the best solution for your game.

\$\endgroup\$
1
  • \$\begingroup\$ +1 There is never only one way to do something in programming. \$\endgroup\$
    – clabe45
    Aug 21, 2017 at 13:49
0
\$\begingroup\$

Since it looks like you wouldn't have any trouble making this ID to be auto incremented, the solution for this post should do the job for you.

Regarding to where to put your items, we would need more info to give you some advice. However and as a general rule, things hold on some kind of manager and factory pattern are usually the way to go.

\$\endgroup\$
0
\$\begingroup\$

Building on Philipp's correct answer, here's a generally simple way of doing it.
(Similar to how Minecraft does it)

public class Item
{
    public final String name;
    public final int id;

    public Item(String name, int id)
    {
        this.name = name;
        this.id = id;
    }
}

This is the class that all the items will extend or use.

public class Items
{
    private Items() {}

    public static final Item APPLE = new Item("Apple", 0);
    public static final Item PEAR = new Item("Pear", 1);
    public static final Item BANANA = new ItemBanana();
}

This will contain all the items in the whole game.

public class ItemStack
{
    public final Item item;
    public int stackSize;

    public ItemStack(Item item)
    {
        this(item, 1);
    }

    public ItemStack(Item item, int stackSize)
    {
        this.item = item;
        this.stackSize = stackSize;
    }
}

This is what you'll have the array of in your inventory classes. Like the player class will have an array of ItemStacks. Here's an example:

private final ItemStack[] stacks = new ItemStack[10];

This is how Minecraft does it and how countless other games do it as well. Some games even have the ItemStack extend Comparable<?> and have the inventories auto-sort as items are added, etc.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .