New inventory system using BGUI.

Ive made an inventory system using BGUI, use any of scripts and images all you want.
All the images and scripts are packed in, though I think for it all to show right the images need to be put in the same working directory as the .blend.

The system has 36 slots, 3 pages, can be changed for more or for a different arrangement.
It automatically identifies any object that’s picked up as long as there’s an image with the name of the object just like it is plus “_inv.png”. So say you have a object named BlasterRifle, the image would need to be BlasterRifle_inv.png. Also the inventory window can be moved while GE is running.
I’m looking for any and all suggestions, criticism, and help, I would really like to redo the updateInv and initInv functions to loop through the objects to update and init instead of my way which is about 50 lines per function. Although when I first made the update functions they would slow logic to almost a standstill every time the inventory was opened or an item was grabbed, but now it doesnt even increase logic use by 5%. And FPS hang around 55-60 for me. Hope this helps someone out there. Im going to keep updating as I progress. Enjoy!

Here’s the link to the updated bug free zip if anyone would rather that.
http://www.mediafire.com/file/r53a6fjmzrohtof/bgui_inventory.zip

Edit: Solved thanks to SolarLune’s amazing help.

Attachments

inventory1.blend (87.7 KB)


My guess before looking at your inventory system is that you’re possibly modifying objects (deleting inventory entries) in the same loop that you’re looking through. This will lead to references of bad data, for example:

array = ['banana', 'apple', 'orange']

for fruit in array:

     array.remove(fruit)

Will possibly return an error because at one point, the array may be pointing to an invalid fruit - this probably only will happen if the array is stored between game frames, but it’s still better to make a copy of the list when you want to modify the original, like so:

array = ['banana', 'apple', 'orange']
 
copy = array[:]

 for fruit in copy:

     array.remove(fruit)

The copy is iterated over, so you’re going through each element once, but you’re removing things from the original list.

EDIT: I couldn’t really diagnose the problem - my graphics card doesn’t seem to like drawing things directly to the GUI (it’s kind of old and buggy). Still, if you have references to deleted objects that you’re referring to at any point, that can cause problems. It sounds like you might be referring to invalid game objects, in which case you can use the gameobject.invalid property to check to see if it’s still valid.

Does it matter if im using a dictionary?

inventory = {"s1":"anItem", "s2":"blank"}
bge.logic.globalDict[own,"inventory"] = inventory

heres a part of my addItem function


for slot in inventory:
        if inventory[slot] == "blank":
                inventory[slot] = cont.sensors['pickUp'].hitObject

That addItem function might very well be the problem, as if some code refers to the object stored in inventory[slot] anywhere, but that game object is no longer existing, then Blender will throw that error. You may want to check when referring to the inventory if the object is valid, with inventory[slot].invalid.

Omg YES YES YES! it was all because I wasnt doing


inventory[slot] = str(cont.sensors['pickUp'].hitObject)

When the items were picked up they all have an end object actuator, so you were right that game object no longer existed and so the original code was making a “slot” point to a non existent item, now it works fine. Im gonna keep working on this and hopefully this will turn out to be a great inventory system.

Thanks again Solar you’re genius.

Heh - I’m no genius. I’ve just worked with the BGE for awhile. You learn these things with time. An inventory isn’t the easiest thing in the world to make either - you’ve got skill to be able to do it, so good job.