February 3, 2011

Dev Update : Fun with Inventory

Hi there,

First of all, thanks to the 150 people who downloaded the test build. Be sure that the future public versions will be much more advanced than this first draft. I also want to thank whoever linked to this blog on Reddit; it was well timed and provided a hell lot of new visitors.

Anyway, coding has resumed. I'm currently implementing how items will be handled by the game and how modders will be able to add all sorts of new items into shops. This is also the first step toward the economic system (as, of course, we need items for the factories to produce something). I am afraid it will be a fairly technical article, so feel free to skip if you're not interested in the work behind the game itself.



If you are a coder yourself, I would be interested in how you deal with this issue.

Handling tons of different item kinds in a game can be tricky. From what I have seen  so far, coders (at least in non-roguelike games) tend to create a class for each category. That's something I will try to avoid as it doesn't scale very well, and as it wouldn't be portable to my future projects without rewriting the whole item management. Instead, I am going to use a similar system that is used in Oblivion, Fallout 3 and other Bethesda games, separating the item itself and its effect(s).

Basically, when I want to create a new object, let's say a First Aid Kit, I will first use the Effect Editor window to create a healing effect (+25HP). Then I will enter the Item Editor window, create the First Aid Kit, link it to the Healing effect and set its price, durability, sprite, and such. In a fantasy setting, if I want to create an armor of strength, i just need to create an effect Strength+1, and link it to whatever armor i want.


In other words, the Effect record contains game-related modifiers (stats/skills/needs altered and such) while the Item record contains only engine related data (cost, sprite used, durability, ..). So instead of having to rewrite how the whole item management system is handled when I want to port my engine to another game, all I will need to rewrite is the Effect part.

Another good thing about this is that effects may be linked to other things than items. Not sure I will implement this, but they could be linked to let's say zones, so that hanging out in a bar will reduce the need to have fun, or in hospital will increase health. I could also link this to special NPC, so interacting with a doctor could heal the player.

The only downside of this system is that it's a bit slower than having everything in the same class/record. When a NPC (or the player) is using an object, the engine has to find the related effect in the database instead of directly retrieving the data from the object itself. But in practice the difference is marginal. You'd need hundreds of items used by turn with a pool of thousands of effects to see a notable difference speed-wise, and even so, it's quite easy to speed things up and avoid to do a search in the database each time an object is used.


At the moment, Effects can allow to:
  • Alter any statistic (strength, agility, ...)
  • Alter any skill (weapon mastery, trading, ...)
  • Alter any need (increase/reduce hunger, sleepiness, ...)
  • Communicate with other NPC at more or less long ranges (radio, cellular phones)
  • Heal or harm a character
  • Increase/reduce fatigue
  • Make the character addicted to a product (with a % chance of success)
  • Increase/reduce funds

More effect types will be added when they become relevant. I have yet to find a way to put a duration to the effects, but it shouldn't be a big deal.

Anyway, I should be done with that before the weekend. Then I will have to add factories, depots and a lot of things at the same time. This last part will probably take a little while. It's at least one new sector type, 2 buildings types, 2 job types to add at the same time, not counting adding the needs to buy and move stuff to NPC. It's quite a long run as all of this need to be ready at the same time with no possibility to show or check anything before all of this is ready.  

4 comments:

  1. Hi Jacob, nice to see you here :).

    Yeah, having effects separate from items is good because this way you could "modify" items (some crafting/repair/transmute system) without replacing the original object, just by altering "effects".

    ReplyDelete
  2. Attaching effects to items is very good for modding, and using effects elsewhere is handy. That's a big plus. Most modern commercial games do it, because they separate content generation by artists and level designers from engine programming by programmers.

    From a coding perspective, no matter how you code the effects/items relationship and logic, you still have to write the logic somewhere.
    In essence you are substituting a monolithic item entity in a class hiearchy with an single item entity composed of hieararchy of effects.

    This is composition and it is a very powerful pattern but has drawbacks too that makes coding logic much harder than it seems.
    It depends on the degree of freedom you want to allow. Can an item be composed of all kinds of effect objects or are some effects combinations forbidden?
    If any set of effects is ok (eg: an item can be use for many different purposes), this will lead to huge headaches for writing the effects interaction logic and the AI understanding of items.
    If some effects combinations are not allowed then that's basically equivalent to making an item class hierarchy without saying so.

    ReplyDelete
  3. @Jacob:

    0.2.1 with health pack and other things that will keep you alive should be out Sunday (or Monday). The effect and item editors will be disabled, though (too messy, buggy for release).

    @RoguedJack/Deon:

    At the moment, only 1 effect (no matter how complex it is) can be added to items. This is only temporary, though. Most of the code to handle several is already in place. It's just that the editor itself isn't, some effect types are not handled yet and I want to publish my next release soon.

    Freedom is good, I see no point in forbidding a modder (or me) to have objects with self contradictory effects if they really want to. Of course, I see the issue you're explaining (for the very good reason that currently scratching my head on this matter).

    I've come with a 'mmmmokay i guess' solution to the AI part. NPC won't look at the effects at all, too much calculations required with such a large amount of NPC anyway. Items in this game have a "category" label set on design time, like 'Healing', 'Food', 'Communication', 'Stress Reduce', or 'Player Only'... Usually corresponding to a Need or a specific action. I should change that by a list instead of being a single element now that I think about it (to accommodate the use of multiple effects in a better way). Anyway, they will use/buy the items based on this indicator instead of trying to figure what the item is exactly doing.

    Yeah, of course, I imagine that someone could kill the whole city by creating an object labeled as food but wearing a deadly poison instead. Well, this is part of the fun, and more the concern of whoever want to mod the game than me :p

    Applying multiple effects in itself is already a pain especially when I want my effects to able to work for several turns. Instant/OneUse Health
    packs and coffee are fine, but items working over times (drugs or low paced healing) not so much. Not sure yet how i'll tell my NPC not to eat all their stock of such item/effect in a row instead of waiting for them to take effect, probably with some kind of cooldown: Don't use something twice until related effect(s) is finished.

    Anyway, thanks for your input, it's appreciated.

    ReplyDelete