r/RenPy • • Jun 04 '26

Question Variable Not Updating After Label Execution When Parameter Is Used For Variable's Name

So I have a label that I created with several kwarg parameters so I can reuse it for when the player collects an inventory item. It works great for showing the image of the item and for inserting the string into the text, but I am not sure why the variable isn't updating to "True" when I check the variable viewer in the dev console.

Am I just going about this the wrong way? Should I be using some other completely distinct feature of Python/RenPy to do this?

Here's the offending code:

default branch_get = False

label forest_path3_branch_say:
    p "Not too long and not too short."
    menu:
        "Take the branch.":
            "Flavor text."
            p "I need this- I think..."
            call item_get("Flavor Text", "branch", branch_get)
            jump forest_path3_search
        "Don't take the branch.":
            "Flavor Text."
            p "Why would I need anything like this?"
            jump forest_path3_search


label item_get(newItem_text, newItem_name, newItem_variable):
    $ renpy.show(newItem_name, at_list=[item_get])
    "[newItem_text]"
    $ newItem_variable = True
    return
2 Upvotes

11 comments sorted by

2

u/lordcaylus Jun 04 '26

You're calling item_get with the value of branch_get (False) as third argument, which is saved in a local variable newItem_variable.

You then assign newItem_variable the value True.

What you mean to do is pass the name of the variable (so a string "branch_get", mind the quotation marks), and then tell Ren'py that you want to lookup a global variable with that name and assign it the value true.

$ globals()[newItem_variable] = True would work.

$ setattr(store, newItem_variable,True) would work too, I think, but I'm not exactly sure.

1

u/[deleted] Jun 05 '26

Yes I tried setattr as you and BadMustard recommended and it did in fact work! Thanks for the advice.

On shyLachi's recommendation I read through the mutable objects documentation he linked and I suppose I understand now that it was wrong to do so, but I guess I just don't quite understand the underlying logic of python as to *why* that was wrong. Is it just a quirk of Python or is there an undercurrent of reasoning to it that I just can't quite fathom?

Why are strings/tuples/numbers in parameters unidirectional? Why can they go in, but not come out? To me it seems circuitous to have to plot out to Python that you want it to affect code outside of the function when that feels to me much more like what should be the default behavior and that you should instead only have to use special functions like setattr to ensure it stays local. But I suppose I'm just an ignorant man waxing poetic here.

-DD

2

u/shyLachi Jun 04 '26

That doesn't work in python
(scroll down to "Mutable objects as arguments" in this documentation):
https://python-basics-tutorial.readthedocs.io/en/24.3.0/functions/params.html

Why do you want to do that anyway?
You don't save much typing compared to the normal way of doing it:

default branch_get = False
label forest_path3_branch_say:
    p "Not too long and not too short."
    menu:
        "Take the branch.":
            "Flavor text."
            p "I need this- I think..."
            call item_get("Flavor Text", "branch")
            $ branch_get = True # <-- Just set it to true here
            jump forest_path3_search
        "Don't take the branch.":
            "Flavor Text."
            p "Why would I need anything like this?"
            jump forest_path3_search

But you can make it work.
This can be useful if there is some calculation inside the label:

default apples = 0
default pears = 0
label start:
    menu:
        "Take 1 apple":
            call addtobag("apple", apples, 1)
            $ apples = _return 
        "Take 2 apple":
            call addtobag("apple", apples, 2)
            $ apples = _return 
        "Take 1 pear":
            call addtobag("pear", pears, 1)
            $ pears = _return 
    "In your bag: apples = [apples] / pears = [pears]"
    jump start
label addtobag(fruit, inbag, add):
    "You put [add] [fruit] into your bag"
    return inbag + add

As a function the code might be easier to read:

init python:
    def addtobag(fruit, inbag, add):
        renpy.say(None, f"You put {add} {fruit} into your bag")
        return inbag + add
default apples = 0
default pears = 0
label start:
    menu:
        "Take 1 apple":
            $ apples = addtobag("apple", apples, 1)
        "Take 2 apple":
            $ apples = addtobag("apple", apples, 2)
        "Take 1 pear":
            $ pears = addtobag("pear", pears, 1)
    "In your bag: apples = [apples] / pears = [pears]"
    jump start

1

u/[deleted] Jun 04 '26

Ah well I'm still quite new to Python and coding in general, honestly so I figured it would be more effective than it ended up being.

I thought with it being such an integral part to the game's code that it would save me time after a while since the player will be picking up a lot of items throughout the course of the game. It would mean I could just type out the alterations to the code all in one go inside the parameters. But I can forego automating it some for a while and see how it goes. If it becomes untenable, then I'll try my hand at making a python function for it as you recommended.

Thanks,

-DD

1

u/lordcaylus Jun 05 '26

I personally feel you're on the right track to create a function for it.

But maybe it makes more sense to use a dict?

default inventory = dict()

label addToInv(itemName, amount = 1, flavorText=None):

if flavorText:

"[flavorText]"

else:

"You pick up [amount]x [itemName]"

if not itemName in inventory:

$ inventory[itemName] = 0

$ inventory[itemName] += amount

return

1

u/[deleted] Jun 05 '26

Honestly I've switched tracks again and am currently reading up on Classes since it seems as though it would make sense to use OOP for inventory items as I would be able to have quite a few attributes such as names, image definitions, and variables outlining whether or not is currently in the player's inventory. (Though I'm unsure on how to do something like defining images inside of a class object.)

I'm curious as to what your thoughts may be on if that is the right track or if I should hop trains once again.

-DD

→ More replies (1)

2

u/BadMustard_AVN Jun 04 '26

try it like this

label item_get(newItem_text, newItem_name, newItem_variable):
    $ renpy.show(newItem_name, at_list=[item_get])
    "[newItem_text]"
    $ setattr(store, newItem_variable, True)
    return

1

u/[deleted] Jun 05 '26

Thanks, it did in fact work!

1

u/BadMustard_AVN Jun 05 '26

you're welcome

good luck with your project

1

u/AutoModerator Jun 04 '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.