How to add random items to Inventory dynamically?

Hello guys,

I’m working on an inventory system for my game, and I’ve been following some tutorials on how I can do it, but all the tutorials that I have found works with some kind of pickable objects.

I want to do something different, I want to generate random items and straight push them into my inventory items list.

I made a simple class to store some data like properties, rarity and the path of the Sprite. That class is named Drop.

public class Drop
{
    public int id;
    public string iconPath;
    public Sprite icon;
    public enum Tipo
    {
        Helmet, // 0
        Armor,  // 1
        Bottom, // 2
        Gloves, // 3
        Boots, // 4
        Ring, // 5
        Shield, // 6
        Sword, // 7
        HeavySword, // 8
        Dagger, // 9
        Bow, // 10
        Axe, // 11
        HeavyAxe, // 12
        Hammer, // 13
        HeavyHammer, //14
        Necklace // 15
    }

    public Tipo tipoEquip;

    public enum Rarity
    {
        Comum, //0
        Incomum, //1
        Raro, //2
        Epico, //3
        Lendario //4
    }

    public Rarity raridade;

    public enum MainAttribute
    {
        Atk, //0
        Def, //1
        Vida, //2
        AtkSpeed, //3
        Acerto, //4
        CritChance, //5
        CritDamage, //6
        MoveSpeed //7
    }

    public MainAttribute mainAttribute;

    public bool hasAtk;
    public bool hasDef;
    public bool hasVida;
    public bool hasAtkSpeed;
    public bool hasMoveSpeed;
    public bool hasAcerto;
    public bool hasCritChance;
    public bool hasCritDamage;

    public float atk;
    public int def;
    public float vida;
    public float atkSpeed;
    public float moveSpeed;
    public int acerto;
    public float critChance;
    public float critDamage;

    public int mainValue;
}

I’m having some ideas of how I can do the generation, but all of them seems to be wrong, the best is this:

I thought that I could create a code that instantiates a prefab of an item and assign a Drop class to it, fill the values, generate some kind of id, and then add this item to the inventory, and eventually save that information in json to reload when needed.

Would that work? Can you guys help me with this?

It sounds like everything you list above is totally doable.

The basic process of procedural generation (of ANYTHING, weapons or levels or whatever) is to:

  • create a template object
  • randomly mutate certain properties of it
  • add it to whatever management system you have set up.

Here’s the general process of inventories:

These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

They contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

1 Like

Thanks for your detailed answer, all of that points are extremely valid, and I’m writing some of them down, actually yesterday I’ve started programming a mini inventory kinda thing, and it’s working the way I’ve described above.

I’m doing an auto combat, when an enemy dies it spawns a loot, and that loot is added to the inventory, and that is working fine, my question now it’s about the storing part, I don’t know how to even start on this lol.

It might be useful to do some diagramming to think about the problem.

This helps you move to designing what you DO with these things, rather than what they are.

  • create the item
  • read the item for display
  • read the item for use in various ways
    —> combat?
    —> opening something?
    —> eating it?
  • give it to someone else
  • discard it?

etc