Page 1 of 1

[Resolved] List item bug

Posted: Thu Jun 14, 2018 11:38 pm
by Avo
Hi Renpy pros!

I've run into a snag recently. I'm pretty new to renpy and moderately new to python as a whole.

I followed the cookbook guide over here for an inventory
(I removed the weight functionality though)

Important bits of code from there that I'm using look like this.

I modified InvItem to include a def __str__

Code: Select all

    class InvItem(object):
        def __init__(self, item, amount):
            self.item = item
            self.amount = amount
        def __str__(self):
            return str(self.item)
I am having trouble displaying items in the backpack though.

https://i.imgur.com/O1KSecq.png

Test code for this is here.

Code: Select all

            if not backpack.inventory:
                text "Backpack Empty"
            else:
                text "Backpack Contents:"
                for i in range(len(backpack.inventory)):
                    $ temp = backpack.inventory[i]
                    text "[temp]"
            textbutton ("Add Poison") action Function(backpack.add_item, item_poison)
            textbutton ("Add Potion") action Function(backpack.add_item, item_potion)
I'm kind of stuck here. I just wanted it to display text (for now) for the item that is in the backpack.

Re: List item bug

Posted: Fri Jun 15, 2018 2:48 am
by kivik
Change this:

Code: Select all

text "[temp]"
To this:

Code: Select all

text "[temp.item]"

Better yet, use for loop as intended for lists of items:

Code: Select all

                for item in backpack.inventory:
                    text "[item.item]"
The way for loop in python works is, it iterates over a list and put the content into your iterator variable. So the above code just loops through your inventory, and for each run of the loop, item becomes the next item. Doing for i in range(len(backpack.inventory)) is actually creating a list with the range function, creating a list of numbers from 0 to (length of your inventory - 1), then iterate over the list of numbers. It's wasteful and unnecessary when you're already using a list :)

Re: List item bug

Posted: Fri Jun 15, 2018 11:02 am
by Avo
Hey, that fixed her!

I had to throw a def __str__ at the item class but now im actually getting strings rather than memory addresses

https://i.imgur.com/yJp2zeC.png

Thank you so much Kivik! I was making it way too difficult with the range(len stuff, you're right. I was using backpack.inventory before but it was giving me the same junk as before, I didn't try the item.item. :D