[Resolved] List item bug

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
Avo
Newbie
Posts: 6
Joined: Thu Jun 14, 2018 11:20 pm
Contact:

[Resolved] List item bug

#1 Post 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.
Last edited by Avo on Sun Jun 24, 2018 2:49 am, edited 1 time in total.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: List item bug

#2 Post 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 :)

Avo
Newbie
Posts: 6
Joined: Thu Jun 14, 2018 11:20 pm
Contact:

Re: List item bug

#3 Post 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

Post Reply

Who is online

Users browsing this forum: Amazon [Bot]