I’ve got the inner workings figured out, in the abstract at least; early prototype works by passing item’s unique ID within the database to a function so that all data needed to either store or spawn in an object can be quickly accessed. Things such as equipping/unequipping, dropping/picking up and moving between containers is no problem. All I really need to express the contents of an inventory is a dictionary.
What I’m wondering is, should I develop the inventory system into a class of it’s own? I have to write all sorts of functions to deal with the aforementioned dict anyway, so I thought maybe just dump everything into a class and call it a day. Way I can totally dance around this: leave inventory as just an attribute of the object that holds the dictionary and write all inventory-specific functions to a separate module, then import only the functions I need when I need them and leave it at that, as I don’t really need any of them constantly at hand.
So what do you think is more optimal, in your experience?
my inventory is a object but is ran by my UI manager (position / rot etc) - I spawn it in a overlay scene and set it’s owner and it does the rest from there
From experience, it is better to use a class, in a separate file.
Depending on how experienced you are, or get, you could be able to test the design without having to run Blender.
This is something I am currently doing on a project, and this as proved to be very valuable and saved me a lot of struggle.
Of course the design has to be ever so slightly different, where you must think about correctly laying out your code.
But overall it is so powerfull that it is worth the pain.
I quote:
Writing tests is painful but it hurts so good!
In the case where you still couple your class to BGE, it is not a big issue. Your logic will still be encapsulated in one piece of software that does one job and does it good.
Yes it can, but the purpose of objects is to put the methods/implementation next to the data. One is not better than the other, it is just a matter of writing things in a more logical way.
# this accepts a dictionary using keywords as input to create objects
# I would use upbge and create unique names per game object to avoid issues later and use
# the slot number of the inventory as a key to store the dictionary
# so the inventory as far as structure is concerned is a dictionary of dictionary.
def Name(obj, prop, objDict):
obj.name = objDict[prop]
def Color( obj, prop, objDict):
obj.color = objDict[prop]
def Mass( obj, prop, objDict):
obj.mass = objDict[prop]
helperFunctions = { "Name":Name, "Color":Color, "Mass":Mass }
def newObject(objectDict, owner):
newOb = own.scene.addObject( objectDict['SpawnName'], owner, 0)
for key in objectDict:
newOb[key] = objectDict[key]
if key in helperFunctions:
command = helperFunctions[key]
command( obj, key, objectDict )
I can use a database to draw item templates from, then assign a separate, unique ID to each item and store them in a savegame-specific dict. Get mesh and base values from template database and use savegame dict to reference individual instances. Then store items in the inventory so I can get access like so
inventory[slot] = itemDict[item_id]
Then things can be setup so selecting a slot returns relevant item data. I like it.
What I’m going to test: subclassing dictionary so I can get this functionality + any extras needed.
What i don’t get it why people always want to use the objects as storage, you got a GD so use it as the database. referencing to the right objects is not hard, data can be called on the fly,etc.
i store my inventory data in the GD as a dict with categories like this:
Where do you guys (everyone) stand on writing inventories to a text file stored on the HD somewhere the user couldn’t find it and alter it? I tested this awhile back and had good results.
The main thing is that when the user quits the game, shuts off the PC, then starts and plays the game again, the data is still there. Guns, bullets, money, etc.
Another advantage is you can use blend files as levels. Instead of libloading. The new blend (level) can read from the text file and load all the data. This allows you to keep the blend files small enough to run on weaker PCs, while making huge games.
Libloading doesn’t prevent you from having levels in files.
And yes, writing data to the disk is something most software application do, games are in that category. But don’t expect the user to be unable to find his savefile, as long as it is on disk you cannot hide it…
And although data persistance is not part of the original question, using classes/objects you can make methods to dump on disk/load from disk the data. You can do it without classes too, it’s up to you…
It’s a single player game I’m working on so people can only cheat at the expense of their own fun c:
Heck, I’ll even look into making modding easy if I have the time for it.
I got down to business
Currently I’m going with a grid system that’s already smart enough to know not to add items that won’t fit. Working on adding the drag-n-drop thingie.
The code is pretty lightweight so it has that going for it. The inventory itself is a subclass of dict and an attribute of object. So all I have to do for any one game object to have a container (or even multiple ones) is create a new instance of inventory and pass some parameters(display name, grid size, etc).