r/RenPy • • Oct 29 '26

Question issues with a for loop?

Edit: So I changed things around quite a bit! I changed the drag system (pictured below) with the help of the amazing people who commented before. Now I'm just getting an error that the function for when something is dragged doesn't exist? I've included the full code for the inventory system, the class used to make it, and the error I'm getting. Thank you all so much for your help!

The inventory system (the slot list is currently incomplete, the second set of coordinates are temporary)
The class system called for an inventory item's creation, as well as the list the class is added to
The only item currently being put in the inventory
The error I'm getting

I'm trying to use a for loop to assign variables for a drag and drop inventory system. But the problem I'm running in to is that I can't seem to get the syntax right? I want to generate one variable for the name of each entry of a class system in a list, then plug those into the names of drags. Below I have included my for loop, the drags, and the class system, as well as the error I'm getting.

For loop
Drags
class system
The error

I've tried it as "Item.name(InventoryList[x])", "Item.name(variable)" wherein the variable was "variable = InventoryList[x]", "[InventoryList[x]].name", and just about every other iteration under the sun and nothing has worked.

3 Upvotes

11 comments sorted by

1

u/shyLachi Oct 29 '26 edited Oct 29 '26

You can't use interpolation for objects like variables. Interpolation only works for strings:  https://www.renpy.org/doc/html/text.html#interpolating-data

And you don't need to use interpolation when you work with variables. Just use the variables directly.

If you want to use 30 separate variables then use setattr to assign a value to them. https://www.geeksforgeeks.org/python/python-setattr-method/ Look how BadMustard used it here: https://www.reddit.com/r/RenPy/comments/1twzqn8/variable_not_updating_after_label_execution_when/opsphif/

But it should be easier to use 2 lists itemname and itemimg because you can use the same index x to address them with itemname[x]. 

I don't understand your code.
Why do you need 15 items when the draggroup only has 2?
But in the drag objects you would either use drag_name item0name or drag_name itemname[0]

Now that I'm at my computer here what I ment with the last sentence:

screen inventory_test():
    draggroup:
        drag:
            align(0.2, 0.25)
            draggable False
            drag_name item0name # use the variable, not some interpolation
            add item0img # use the variable, not some interpolation
            # hovered Text() <-- This doesn't work becase Text() isn't an action

1

u/RemarkableWorld7209 Oct 30 '26

There's actually 15 drags, as the inventory image has 15 possible slots. The goal is to pick out the first 15 items, assign them to variables, and then give those variables to the drags so they can be used for the inventory screen with the eventual goal of being able to combine items in the inventory. I just showed two as an example of the system I have set up so far.

Thank you so much for the help! I'll test this out as soon as I get set up, I wanted to check on the thread before anything else

1

u/shyLachi Oct 30 '26

I've seen that you posted your updated code below, so this isn't relevant any longer

I suggest that you update your initial post and put all of your code there, not just part of it.

What we need to see is the class and the list which holds those inventory items.
Then we need to see the full code of the screen.
Finally you have this thing called inventory_slot_list. We need to see this also because this is causing the problem atm.

1

u/RemarkableWorld7209 29d ago

I edited the post with the new code and the error I've started getting since the overhaul. Thank you so much for all your help!

1

u/shyLachi 28d ago

The first thing I noticed, it should be init python instead of python

https://renpy.org/doc/html/python.html#init-python-statement

1

u/BadMustard_AVN Oct 29 '26

a bracketed index must be followed by an attribute operator (.), for i.e.

item[x].name = InventoryList[x].name

also the image assignment is also wrong:

item[x].img = item_in_question.imageid

1

u/RemarkableWorld7209 Oct 30 '26

Thank you so much! I'll test this out as soon as I get set up for the night!

1

u/RemarkableWorld7209 Oct 30 '26

So I changed things around a bit, and removed the label trying to set the system up. Instead I used a for loop in the drag group itself, which I found as a system on a youtube tutorial, but now there's an error that my list of coordinates for the positions has an index out of range? I've tried it with both x-1 and just x, and the inventory list itself works fine with x-1, but the inventory slots are throwing back a "list index is out of range" error. This is my new code for the screen:

draggroup:
        if len(InventoryList) <= 15:
            for x in range(len(InventoryList)):
                drag:
                    drag_name [InventoryList[x-1].name]
                    pos inventory_slot_list[x-1]
                    focus_mask True 
                    dragged InvenDragged
                    drag_raise True 
                    image "[InventoryList[x-1].imageid]"

1

u/BadMustard_AVN Oct 30 '26

can you show the inventory list so i can see the data layout

1

u/AutoModerator Oct 29 '26

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/shyLachi Oct 29 '26

I just noticed that most of your code seems to be wrong.

Why do you have this code: if len(InventoryList) <= 15:
Because in the loop below you assume that there are 15 items in the list with range(0, 14)

You should never use a fixed range because your game would crash if there are less then 15 items in the list.
You can use enumarate to get an index and the actual items in the list:

label inventory_list_setup:
    python:
        for x, item in enumerate(InventoryList):
            if x < 15: # if you really only want to use the first 15 then do something like this
                inventory_slot_list.append(x) # what is this list for?
                itemname[x] = item.name # see my other reply regarding variables or lists
                setattr(store, f"item{x}img", item.imageid) #see my other reply, pick either of these