Page 1 of 1

[Solved]Changing renpy varible within a python codeblock

Posted: Fri Oct 14, 2016 10:04 am
by Hurrdurr
Alright so i'm working on this project, and I have created an inventory system.

I am however having some issues with modifying renpy vars within a python codeblock. It's not giving me any errors it just does not seem to modify the var.

If i have a renpy var and give it the name item_used and within a python block i refer to a var named item_used and modify the varible. It simply wont change the var.

My theory is that the renpy and python vars even though same name are 2 different vars. Is there a work around for this?

Best regards

// Hurrdurr

Re: Changing renpy varible within a python codeblock

Posted: Fri Oct 14, 2016 10:05 am
by Ocelot
Please, show the code you are using to declare and change variable.

Re: Changing renpy varible within a python codeblock

Posted: Fri Oct 14, 2016 10:25 am
by Hurrdurr
script.rpy

Code: Select all

init:
     used_item = "test1"
inventory.rpy

Code: Select all

init -1 python:
     def item_use():
          used_item = "test2"

Code: Select all

screen inventory_screen: 
     some irrelevant code
          imagebutton xpos x ypos y action [item_use()]

label.rpy

Code: Select all

examplechar "[used_item]"
expected that it should print "test2" always prints "test1"

I know for a fact that the button itself works as i can manipulate python code within the def and ive used other buttons that works well.

Re: Changing renpy varible within a python codeblock

Posted: Fri Oct 14, 2016 10:30 am
by Ocelot
It is Python quirk, not RenPy.

By default all writes to variables in functions will write to local variables, which are not visible outside.. To write to global variable, you need to declare it as global, like:

Code: Select all

def item_use():
    global used_item
    used_item = "test2"

Re: Changing renpy varible within a python codeblock

Posted: Fri Oct 14, 2016 10:33 am
by Hurrdurr
Thank you so much.

I will mark the post as resolved.