Advertisement

Flow for creating a basic inventory system from scratch...

Started by November 26, 2017 04:56 AM
3 comments, last by cjmarsh 7 years ago

I have an inventory system that I am trying to make as part of my first programming venture.

Creating the inventory doesn't seem to pose the challenge, but rather the control flow and code organisation that is needed. I have done some research, but I can't find the answers that I need. Instead, I have become confused, which you will see if you read my sheets I've been scribbling on trying to figure out how to work it.

Most of the information that I think anyone would need to take part in this discussion can be found in that link. I am using flawed logic, but I don't know how it is flawed, since I am coming from the perspective of someone who is new to code and does not have a lot of experience with the optimal interactivity between functions and the logic that should be used in these situations.

Essentially, I hope to gain some seeds of wisdom from someone else that may have been in my position or a nudge in the right direction from someone else who, in fact, can see where my logic is flawed and where it needs to be shifted.

I'm really not seeing anything that immediately sticks out as "flawed". It makes perfect sense to me that both the Room and Player objects need to have their own way of managing items (Lists). It is also logical that once the player picks up the object, it cannot exist on the floor and in the player's inventory. You could tag your items with IDs and simulate moving it from the room's inventory to the player's.

 

Item is removed from the room's inventory and then a new item object is added with the removed item's ID, thus creating the same item. 

 


roomInventory.Remove(item) 
playerInventory.Add(new Item(item.ID)); 

EDIT: ID can be a string, integer, or however you want to organize your items. 

 

Advertisement

Copy the item from the rooms list to the characters list.  I'm a C++ guy with little C# experience but I would make my items unique_ptrs and just std::move them from one to the other.  Another idea, have a item system that holds ALL items in the level & on the player with an extra variable for it's parent ID.  Either the player, NPC or room, or static object in a room like in a desk drawer for example.  Then you just change the items parent ID.  When/if levels change you bring forward only the players items to the next/new level.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

It might be helpful to view the items as two separate things: items that are spawned and on the ground, and items that are acquired and in the player's inventory. If it were me, I'd use an Event System and throw out an event when an object was "picked up" by the player. There would be classes controlling the items on the ground and the player's inventory, each subscribed to that event to destroy and create the item appropriately, imitating a transfer.

This topic is closed to new replies.

Advertisement