I run into a new problem and again I guess it is rooted in my bias of programming ideas.
I tried to create a small inventory for my detective story. The idea is, when the player enters certain locations he/she could find items. When looking into the inventory the detective should get a small list of the found items.
So I created a small class for items and after that, I build a list of objects out of said class.
Code: Select all
#############################################################################
# definition in classes.rpy #
#############################################################################
init python:
## Definition for items
class c_Items(object):
def __init__(self, s_name, i_weight, i_noOwned):
self.s_name = s_name
self.i_weight = i_weight
self.i_noOwned = i_noOwned
def AddItem(self):
i_maxWeight = 50
if self.CurrentWeight + self.i_weight > i_maxWeight:
return
else:
self.i_noOwned += 1
@property
def CurrentWeight(self):
i_currentWeight = 0
for elt in L_inventory:
i_currentWeight += (elt.i_weight * elt.i_noOwned)
return i_currentWeight
## Create an empty List of items
L_inventory = []
## Fill the list with 10 placeholders
t = 0
while t < 10:
L_inventory.append(c_Items("none", 0, 0))
t += 1
Code: Select all
############################################################################
# after the game started in script.rpy #
############################################################################
label start:
call variables
menu:
"Go to Morgue":
$ s_location = "Morgue"
if L_inventory[9].i_noOwned == 0:
"You have found a sewing-kit."
$ L_inventory[9].AddItem()
"Display inventory":
call displayInventory
label variables:
#Starting location
$ s_location = "Basement"
#The items I want to use
$ L_inventory[0] = c_Items("pot", 1, 0)
$ L_inventory[1] = c_Items("knife", 2, 0)
$ L_inventory[2] = c_Items("firestarter", 1, 0)
$ L_inventory[3] = c_Items("paracord", 2, 0)
$ L_inventory[4] = c_Items("rain poncho", 4, 0)
$ L_inventory[5] = c_Items("compass", 1, 0)
$ L_inventory[6] = c_Items("flash light", 2, 0)
$ L_inventory[7] = c_Items("cotton bandana", 2, 0)
$ L_inventory[8] = c_Items("duct tape", 1, 0)
$ L_inventory[9] = c_Items("travel sewing-kit", 1, 0)
return
label displayInventory:
python:
s_help = "" #a string that should be usable in renpy
n = 0 #an integer to point out which item of the list should be used
for elt in L_inventory: #run throu every elt of the list L_inventory
if elt.i_noOwned > 0: #each item knows how often it is in the posession of the player, if the count is >0 it should be attached to the string
s_help += str(L_inventory[n].i_noOwned) + " " + str(L_inventory[n].s_name) + ", " #what I thought, should work for the writing of the string
n += 1
if s_help == "":
"The inventory is empty."
else:
"[s_help]" #the string didn't show the right values
"Your bag has a weight of [L_inventory[0].CurrentWeight] units." #the weight is correct thou
return
But as soon as the player enters the morgue, the sewing-kit is added and the inventory shows, that the detective has 0 pots. And that the inventory has as the correct weight.
My thought was, that the loop in "displayInventory" would go through every element (elt) in the list one by one and ignores every elt with a value of "i_noOwned == 0"
So please, can somebody tell me where I got wrong?