Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 06:31:43 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)I need help programming an elegant inventory system
Pages: [1]
Print
Author Topic: I need help programming an elegant inventory system  (Read 2287 times)
jbarrios
Level 4
****


View Profile
« on: February 08, 2022, 08:25:20 PM »

I've been racking my brain over this for a while now.  I could use some help.

I want to make a RPG style inventory system.  I want to make it as elegant as possible.

Here are some example of items I want to track:
  • Broad Sword.  Weapon.  5 damage
  • Short Spear.  Weapon.  3 damage
  • Linen Shirt.  Armor.  1 defense
  • Chain-mail.  Armor.  3 defense
  • Healing Potion.  Consumable.  +25 health
  • Flash Bomb.  Consumable.  Deals blindness to target
  • Gold Ring.  Trade.  Only for selling

Here are some interactions with items I need to do:
  • Attacking with an equipped weapon
  • Receiving an item from a chest
  • Buying and selling from shop owners
  • Using consumables in combat

Hopefully you see the gist of what I'm trying to achieve here.

Of course some items would have shared stats.  Things like value and weight.  Other things are specific.  The linen shirt wouldn't need to track "Attack power"

Consumable items like Potions are tricky.  One potion may give a temporary stat boost.  Another may stick an enemy in place.  Is there a better way of doing this than using a switch statement to check every... single... consumable?

I was thinking using class inheritance would be useful:
Item
--Weapon
--Armor
--Consumable
----Healing?
----Attack?

If you have solved this problem or have a useful resource I would love some help.
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #1 on: February 09, 2022, 08:18:34 AM »

I designed a system like this a little while ago. It's gone through a few iterations and might not be 100% done yet, but where I've landed right now is a data structure for each item that has these properties:

- Display info (name, icon, description, etc.)
- List of required stats to be able to use (minimum strength value for a heavy weapon, etc.; can be an empty list)
- Valid slots to equip ("hand" for a sword, "chest" for a tunic, "item" for a potion, etc.; can have multiple valid slots like "hand 1", "hand 2", and only one piece of equipment can be in each slot)
- Stat bonuses granted while equipped (+ attack power for weapon, + defense for armor, empty list for a consumable)
- Combat abilities granted while equipped, with a maximum use count for each (a weapon might grant an unlimited-use special attack; a potion might have 3 charges of invoking a healing ability before being depleted)

A separate data structure describes the abilities that are granted by having something equipped (or in the case of my game, may be innately usable by a character or granted through other means):

- Display info (name, visual/sound effects, etc.)
- Resource cost (3 mana to cast a heal spell, etc.; can be an empty list)
- Valid targets (self, ally, enemy, single/multiple)
- Effect on target. This is where it gets interesting. Each ability can be of type "change resource", "apply status", "remove status", or "compound", which is a list of one or more of the other three types for items that do multiple things at the same time.

Most abilities are of type "change resource", which identifies a target resource type (health, mana, stamina, etc.) and a formula for how to change it. Can be a positive or negative change. I built a pretty robust pseudo-scripting system that can describe stuff like "take the actor's strength stat, subtract the target's defense stat, clamp that to a value of zero or above, and subtract it from the target's health resource". A formula evaluator takes the ability's effect description and the actor and target data structures, reads the relevant values from them, and applies a change to the target's state described by the evaluated outcome of the formula.

It works pretty well, and I haven't found it too cumbersome to work with in my scenario editor. There are some unusual things about how my system works; for example, I have no true consumables, so if you own a potion and use it in combat, you'll end up with the same potion recharged and able to be used again in the next encounter. I also designed the formula system so that it could automatically print out a human-readable description of exactly what each thing does, so the player has complete information about the algorithms used.
Logged

jbarrios
Level 4
****


View Profile
« Reply #2 on: February 09, 2022, 07:55:40 PM »

Thank you ThemsAllTook,  that's helpful
Logged
Thaumaturge
Level 10
*****



View Profile WWW
« Reply #3 on: February 11, 2022, 08:51:38 AM »

Consumable items like Potions are tricky.  One potion may give a temporary stat boost.  Another may stick an enemy in place.  Is there a better way of doing this than using a switch statement to check every... single... consumable?

One potential way to do this, I think, is to have such consumables add a script-object to a list of script-objects objects within the target to which they're being applied. The target-objects would then run the relevant methods within these script-objects, and remove them once their effects are done.

For example, a stat-boost item might have a script-object that acts like so:
  • On being added, increase the relevant stat in the target.
  • On being removed, decrease the relevant stat in the target.
  • On being updated, run down a timer.
    • When the timer runs out, mark the script-object as being "done".
Logged

jbarrios
Level 4
****


View Profile
« Reply #4 on: February 13, 2022, 07:22:56 PM »

That makes a lot of sense Thaumaturge.  It also solves another problem I've been thinking about:

I'd like the player to temporarily modify their weapons.  Rubbing a poison potion on a knife for example.  Putting a function that alters the stats would take care of that as well.
Logged
Thaumaturge
Level 10
*****



View Profile WWW
« Reply #5 on: February 14, 2022, 02:00:54 AM »

I'm glad if I've helped, then. ^_^
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic