Can't create a proper string out of a list of items [solved]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
HochElf
Newbie
Posts: 7
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Can't create a proper string out of a list of items [solved]

#1 Post by HochElf » Fri Aug 16, 2019 7:26 am

Greetings, me again.
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
		
		

Now I tried to add an item to the inventory and then show it via a menu choice

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

As long as the inventory is empty, the string is empty and the player gets the right message.
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?
Last edited by HochElf on Fri Aug 16, 2019 10:07 am, edited 1 time in total.

HochElf
Newbie
Posts: 7
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Re: Can't create a proper string out of a list of items

#2 Post by HochElf » Fri Aug 16, 2019 10:07 am

I just find the solution myself. As always, as soon as you ask for help and take a break it pops into your mind. -_-

I just rewrote the line that creates the string.

Code: Select all

s_help += str(elt.i_noOwned) + " " + str(elt.s_name) + ", "
simpler and functional. *facepalm*

Post Reply

Who is online

Users browsing this forum: hell_oh_world