PART 1
If you want to make a game, you will probably want your player to be able to pick up items.
Storing properties on an item is OK for simple games where you have just a player who can pick up points, or ammo:
player['ammo'] += 1
But when you start trying to model an inventory, this is a pain to try and handle.
One of the best methods for making an inventory is using a dictionary.
***SIMPLE DICTIONARY
my_inventory = {"9mm_ammo":12,"body_armor":1,"medkit":4}
You can wrap this over several lines to make it more readable:
my_inventory = {"9mm_ammo":12,
"body_armor":1,
"medkit":4}
You can see that the inventory holds 12 clips of 9mm ammo, a set of body armor and 4 medkits.
An empty dictionary looks like this:
my_inventory = {}
***ADDING ITEMS
If you want to add new items to the dictionary you have to check if it is already in there:
item_string = "teddybear"
if item_string in my_inventory:
my_inventory[item_string] += 4
else:
my_inventory[item_string] = 4
Otherwise you’ll get a key error (trying to add to an item that doesn’t exist).
A shorter way of doing this is to use the built in .get() function.
my_inventory[item_string] = my_inventory.get(item_string,0) + 4
This checks if item string is there, if not it uses the default setting:
.get(item to check for, default setting)
.get() can be very useful when working with dictionaries.
***REMOVING ITEMS
Removing items could get tricky, as we could end up with -5 9mm ammo if we’re not careful.
So we can use the built in function max() to set a minimum value. This will always choose the highest of two options.
my_inventory[item_string] = max(0,my_inventory.get(item_string,0) - 4)
If the result is less than 0, it will be set to zero automatically.
Because we used the .get() method it will even set non existent items to zero.
So if item_string = socks
It will add a zero sized stack of sock object to the inventory.
We could remove the string from the dictionary using del, but we don’t need to. As long as it has a value of zero it won’t be represented in the inventory.
***PREPARING THE INVENTORY FOR DISPLAY
For displaying your dictionary we could break it down in to a list:
display_list = []
for key in my_inventory:
stack= my_inventory[key]
for item in range(stack):
display_list.append(key)
This will produce a list like this:
We need to get a list because we want each object to be displayed individually, not as a stack.
***DISPLAYING THE INVENTORY
You will need to create some inventory items on a hidden layer, and name them the same as your inventory dictionary keys. Next you’ll use scene.addObject() to add those to your scene.
scene = own.scene
y_offset = 0.0
for item in display_list:
item_box = scene.addObject(item,own,0)
item_box.worldPosition.y -= y_offset
y_offset += 1.2
This will add the objects and offset them by 1.2 blender units on the y axis. Like this:
This might not be very useful, as a long inventory will go off the bottom of the screen so we can offset on the x axis too.
scene = own.scene
y_offset = 0.0
x_offset = 0.0
for item in display_list:
if y_offset > 6.0:
y_offset = 0.0
x_offset += 3.5
item_box = scene.addObject(item,own,0)
item_box.worldPosition.y -= y_offset
item_box.worldPosition.x += x_offset
y_offset += 1.2
That’s better:
***Continued below>>>