I’m making an RPG in C++ and the items (loot/gear) will have immutable base versions, but then the player can get instantiations of them which can change with use and modification. These mutable instantiations will be saved in the DB. But I’m wondering if the base versions should be defined in JSON, or a separate DB (with the same schema), or maybe the same DB (seems dangerous), or if I should just hardcode them in C++ in the ItemFactory.

How have you approached this problem in your games? How do game engines do it? I’m using SDL2 so I’m doing most of these systems from scratch.

  • RightHandOfIkaros@lemmy.world
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    12 hours ago

    I like to use Unity, and I love using class inheritance where possible.

    If I know a group of items will all share similar properties but might have one or two different methods, I create the base “Item” class with all the values that all items will share in the game, and then create a new class for each item that has a different property all on its own.

    For example, a healing item has a healing method that a coin item would not have, but both of these inherit from the Item base class and thus can simply be detected as an item by the systems that only care about whether an object is an item or not. Such as when an object is picked up by the player and placed in the inventory. This keeps everything pretty simple for my purposes. Not sure if this gives you any ideas.

      • RightHandOfIkaros@lemmy.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        11 hours ago

        Pretty much, yes. I generally have such few item types in my game that it doesn’t really matter if I hard code them.or not.

        For example, Item might contain parameters like item-id, item name, carry weight, etc. Parameters that every item would need to have. Then HealthItem would inherit from Item, and thus would include all its base parameters and methods for being added to the inventory, etc. HealthItem would then also have new parameters like heal-amount and a method for healing its user.

        Something like a health potion that heals 5 health and a medkit that heals 50 health would both use the same HealthItem class, and be stored in the game engine as separate prefabs. These prefabs would then be instantiated into the game for entities to interact with them.