RPG Game Item & Inventory System | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

RPG Game Item & Inventory System

So I've been trying to think of how to make my own item and inventory system in an RPG Text-Game for a while, but I'm kind of stumped as to how to implement it. Does anyone have any experience from the real world or a system you've made that worked for you? My main problem comes from creating items for an enemy drop when they die/faint. Class hierarchy is Item->Weapon, Item->Armour, Item->Potion. I want a random chance to generate any item, but how would it know to make a short sword, as opposed to a long sword? If you have any input, I'm willing to listen :) Also, other languages feel free to explain your structure as well, if it fits.

4th Sep 2017, 3:57 AM
Zeke Williams
Zeke Williams - avatar
3 Answers
+ 4
I've done something like this before but I never looked up any tutorials so idk the standard way of doing these things. (I got the idea when a Dev sent me all the images of his game and the file names were all numbered.) *Everything I write is done with Unity, but hopefully it gives you some ideas* For randomly generating: What I did was have a method that loads the randomly generated weapon into the map, where the enemy died. I had it inside the enemies script, where if he dies i randomly select an item to drop (and amount of items, favored to 0). I had each item represented as an int in a folder that contains all the items. Example/ 0 would be a hp pack. 1 would be ammo pack. 2 would be short sword. etc.. Then I could just do: int item = getRandomItem(); // where getRandomItem determines the odds and item to drop if (item != 0) Instantiate(Resources.load<GameObject>(item), transform.position); // What this does is Create the 3d gameObject and load it into the scene from the Resources folder at the position of the enemy I know it doesn't seem neat to have 0, 1 etc.. to represent all the items, but I would have it sorted in files and by number ranges. You could also have some words that represent certain number ranges if you want to separate each item type in its own files. for example, if(item < 20) // create a sword Instantiate(..etc ("Sword/"+item)); else if (item < 50) // create a bow with each numbers inbetween being a different bow model. For Inventory: I actually created my own ArrayList, I just called it Inventory. public static Inventory; Then It would have it's own add, remove, etc.. methods. Only since the player could only have x amount of items, that'd be the size of the array. (The Inventory class would use an array of type Item, internally) When he picked up an item, I just added it to the list. Vise versa for dropping. The current weapon that the player is holding would be stored as an index. int curWep = 0; // player holding the wep at pos 0 in the array
4th Sep 2017, 4:09 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Yes I had item rarity. I also represented this with a number (an int). class Item{ int rarity; // 0 = common, 1 = uncommon } You could use an Enum to keep track of what each rarity is as a number: Enum rarities{ common = 0, uncommon = 1, rare = 2 } If I remember correctly I actually chose the rarity when I created the item as well. I tried to avoid choosing a random weapon rarity when the item is created (like in the Item classes constructor) because I was thinking that could cause weird behaviour when the game is loaded from a saved file. (I would need to handle the issue of the item changing rarities when loading the game) So it would be something like this: class Enemy{ void spawnItem(){ int item = getRandItem(); int rarity = getRandRarity(); } } Those methods could be part of a different class though: class RandomGenerator{ public static int getRandItem(){} public static int getRandRarity(){} } Then when you create the item, like I wrote before with: GameObject itemCreated = Instantiate(Resources.load(item)); Since each item would have a rarity, I could do something like this: (Again, C# Unity) itemCreated.getComponent(Item).setRarity(rarity); Where get component gets the item script attached to the Object I loaded into the game. Then I set the rarity of the weapon there. Along with any other stats needed. (That are randomly generated)
4th Sep 2017, 4:45 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Wow, thanks for you answer, @Rrestoring faith! I haven't seen any tutorials either, and I haven't had any success with finding any articles on the matter either :( See, I was thinking about doing the same exact thing, where I would have files for each of the type of items: one for weapons, one for armor, and one for misc. or potions. I would then have the enemy class have an array of 3 items where each element is one type of item that could potentially be dropped. Then I just get a random one to select from each file, if it drops it at all, and load it's data into the proper item type object. @Rrestoring faith, did you have rarity for each item too? If so, how did that work?
4th Sep 2017, 4:36 AM
Zeke Williams
Zeke Williams - avatar