Python function not changing variable's value, but works with renpy functions

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
Kaira
Newbie
Posts: 12
Joined: Thu Mar 09, 2017 10:35 am
Projects: Pinewood Island
Tumblr: kaira-dream-spirit-art
Deviantart: dreamer-t
Location: Canada
Contact:

Python function not changing variable's value, but works with renpy functions

#1 Post by Kaira »

I'm working on an inventory system for my game, but for some reason one of the values isn't changing within the function :/ Hopefully someone can help me out o.o'

The objects/variables are created just before the start label:

Code: Select all

default inventory = []
default selected_item = None
The current function's purpose is to discard an item from the player's inventory. The button call looks like this:

Code: Select all

textbutton "Discard" action Function(discardItem,inventory,selected_item)
#textbutton "Discard" action [RemoveFromSet(inventory,selected_item), SetVariable("selected_item",None)]
The commented out section is the previous version of the button which works, but some buttons require some more complex actions so I would like to keep the actions to the functions.

And the function itself is in a python init and looks like this:

Code: Select all

    def discardItem(inventory,selected_item):
        inventory.remove(selected_item)
        selected_item = None
Doing this removes the item from the inventory, but it doesn't change the value of "selected_item" to "None"

I'm not sure if this is something specific to python, which I think it could be from what I've been reading on it, but why does renpy's function work then? And is there some way I could call that function from my python code?

I already tried this (in the python function) and it didn't work, selected_item was still set to the discarded item.

Code: Select all

        SetVariable("selected_item",None)
The actual screen looks like this currently (before any items are selected). The screen is a work in progress >.> (this is just the prototype)
Image
Item selected
Image
And this is what it looks like when the item is discarded/deleted from inventory.
Image
However, "Current Item" section should say "None" like the first image, but it doesn't since selected_item wasn't reset. Which means if the player attempts to discard the item (which no longer exists in inventory) the game will crash.

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: Python function not changing variable's value, but works with renpy functions

#2 Post by DannX »

In the function code as you have it, the last line is creating a local variable and setting it to None. If you want to modify an external variable (declared with default) you must use global statement:

Code: Select all

default selected_item = None #default declaration

init python:

    def discardItem(inventory):
        global selected_item #this tells python to use the defaulted variable instead of creating a local one
        inventory.remove(selected_item)
        selected_item = None #Now set the global variable to None
Be careful with argument and variable names, they can't be the same.

EDIT: Whoops. I forgot to edit my own code
Last edited by DannX on Thu Mar 22, 2018 12:41 pm, edited 3 times in total.

User avatar
Kaira
Newbie
Posts: 12
Joined: Thu Mar 09, 2017 10:35 am
Projects: Pinewood Island
Tumblr: kaira-dream-spirit-art
Deviantart: dreamer-t
Location: Canada
Contact:

Re: Python function not changing variable's value, but works with renpy functions

#3 Post by Kaira »

DannX wrote: Thu Mar 22, 2018 9:49 am In the function code as you have it, the last line is creating a local variable and setting it to None. If you want to modify an external variable (declared with default) you must use global statement:

Code: Select all

default selected_item = None #default declaration

init python:

    def discardItem(inventory,selected_item):
        global selected_item #this tells python to use the defaulted variable instead of creating a local one
        inventory.remove(selected_item) #remove from the list the argument you passed to the function
        selected_item = None #Now set the global variable to None
Be careful with argument and variable names, it often leads to this kind of problems.
That sort of works, but only if I don't set "selected_item" as one of the arguments passed in the discardItem function. If I use the way you wrote it returns an error:
Parsing the script failed.
File "game/script.rpy", line 34: name 'selected_item' is local and global.
The function does work though if I do this:

Code: Select all

init python:

    def discardItem(inventory):
        global selected_item
        inventory.remove(selected_item)
        selected_item = None
...so it's okay to use global in this instance? I know usually you should avoid using global variables, but it seems like for this it's okay. Or that it's not really a global variable, but the command tells it access a variable from a higher scope?? Anyways, it works xD

Main main bit of programming experience comes from C++ so Python is still a bit weird to me. '___'

Thanks for the help. :)

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: Python function not changing variable's value, but works with renpy functions

#4 Post by DannX »

Oops, sorry, that's why I wrote that last line about being careful with the arguments as they cannot be the same as the variable you're accessing, and I forgot to edit my code.

Post Reply

Who is online

Users browsing this forum: No registered users