For loop in Screen to populate buttons?

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
User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

For loop in Screen to populate buttons?

#1 Post by noeinan »

Hello! I am trying to make a very simple shop. I have all the code for my inventory management, but am struggling with getting the shop itself working.

I have a random generator (custom class I made) that creates random toys. I want the shop to take a dictionary and iterate through a for loop, adding a button for each toy that was generated and automatically adding that toy/item into the shop's inventory. If the player clicks the button, the toy is set to the "current toy" variable, and you jump to a label where the "current toy" is removed from the shop inventory and added to the player's inventory.

However! I'm struggling a bit with the syntax. After studying for loops and how they work with dictionaries, I found there's some differences when referencing the key versus the actual value attached to that key. (The item name vs all the stats stored in the item.) Overall it seems a little more complicated than for loops iterating through a list, but I think I'm managing? However, after doing the research and making the code, I found that I'm getting an error message.

If anyone has some advice on how I can go about this better, I would greatly appreciate it! Here is the relevant code:

Code: Select all

screen toybuy:
    layer "overlay2"

    $ toy_dict = GenerateMultipleToys(3)
    $ x = 0

    text ("Which toy do you want to buy?") ypos 150 xpos 940

    vbox:
        ypos 222 xpos 944

        for key in toy_dict.values():
            'toy{}'.format(x+1) = Item(name=key.name, type="toy", subtype=key.subtype, price=key.price, quality=key.quality, tags=key.tags, used=False, reviewed=False)
            toy_store.add_item(toy)

        for item in toy_store.list_items():
            textbutton ("%s"%item.name) action [SetVariable("current_toy", item.name), Hide("toybuy"), Jump("buytoy1")]
And the error I'm getting:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/locations.rpy", line 148: expected a keyword argument or child statement.
    'toy{}'.format(x+1) = Item(name=key.name, type="toy", subtype=key.subtype, price=key.price, quality=key.quality, tags=key.tags, used=False, reviewed=False)
    ^

Ren'Py Version: Ren'Py 7.3.2.320
Fri Jul 17 17:15:34 2020
Image

Image
Image

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: For loop in Screen to populate buttons?

#2 Post by MaydohMaydoh »

You're trying to assign a class instance to a string instead of a variable and python statements should either use the one line python thingy $ or put into a python block.
Also you should default your variables instead of using $.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]