Monday, 19 September 2011

Evolution of the code #1

Confound this inventory code!

Sometimes everything gets more complicated than it should...

I have decided to use the term "inventory object" rather than a character because in this game characters are derived from lockers! Hey, both are entities capable of holding objects right?  :D

How it works:  In simple terms, an "inventory object's" inventory list holds instances of a class which, in it's turn holds the reference of the actual inventory object type that should be created along with other data. Thus when an object is picked up from the ground it is removed from the world and in it's place a class is added to the inventory list which holds the item's data. In the same manner when one object needs to be discarded it removes the entry from the list and creates the actual object in the game world.

Initially i thought this was a bad idea! During my first attempt to code the inventory system, I somehow figured that simply making the "inventory object" hold instances of items should work out well. How wrong i was. One of the major flaws is that every item in the inventory must have an instance on the map. That is a major resource hog and not optimized at all. Ignoring that, this would also make a very bad idea for mappers as they would have to create every instance on the map, set it as invisible and manually add it to and "inventory object's" inventory list.

Thinking about it a bit i thought of this idea of replacing it with a more simpler class as described above. After that i discovered that was the initial inventory system implemented in neoaxis. Lesson learned - lurk more code before attempting to reinvent the proverbial wheel.


So why is this all complicated? 

The interface
The interface is based on a few custom controls that are more complex than i would like... Fallout has two types of inventories: Player inventory where the player interacts with the items he owns or multiple "inventory objects" where items are swapped between them.

When a player inventory is created on the screen, the gui control list must be filled with a "slot" class for each item in his inventory. This slot class must know which item in the inventory it links to, if the user wants to relocate that item into another slot (or another inventory) or similar events.

That takes us to what i like to call "holders". Each slot must remain in a "holder". Holders can be the actual gui inventory on the screen or holders for primary or secondary weapons, armors and such. Thus, each holder that is considered an inventory must have a link to the actual "inventory objects". Therefore we assure a way to swap items between these "inventory objects".

I know two objects to "barter" are more than enough, but with a few extra lines of code, I used lists to make it possible for any number of "inventory objects" to trade. I like how it turned out! :D


 The items
So now that we can manipulate these lists a bit we need to actually make the player hold something. After lurking a bit from the existing neoaxis code i have found that the best way to do this is to store an ActiveHeldItem variable. By my logic, this should be updated every time the player switches between the primary and secondary weapon slots and when the player set a new item in one of these slots. This is where i need to work on a bit more... :D

I will get to those in the next update...

No comments:

Post a Comment