Ren'Py [SOLVED]How to assign value to a renpy variable from a python function

GoldenD

Member
Sep 30, 2018
102
70
Hi guys,

have a normal default variable in renpy.
So the goal is to call a function but i would not assign the function to the variable, i would work with my "renpy" variable in the function and better, i would like to call another function from first function without passing parameter and work again in these on the "renpy" variable; Is it possible ?

Python:
default globalVariable = ""
.../...
init python:
    def firstFunction():
        globalVariable  = "Something"

        if globalVariable  == "Something" :
            return secondFunction()

.../...
#Another file
init python:
    def secondFunction():
        if globalVariable  == "Something" :
            globalVariable  = "SomethingElse"
            return True
.../...

#somewhere in renpy

if firstFunction():
    display globalVariable
 
Last edited:

barotok

New Member
Apr 30, 2019
13
10
Adding "global" to your variables should work

Python:
init python:
    def firstFunction():
        global globalVariable
        globalVariable  = "Something"

        if globalVariable  == "Something" :
            return secondFunction()

.../...
#Another file
init python:
    def secondFunction():
        global globalVariable
        if globalVariable  == "Something" :
            globalVariable  = "SomethingElse"
            return True
 

riktor

Active Member
Nov 26, 2018
906
1,161
seems you are already declaring the var in renpy via the default statement,. in your python blocks use store.globalVariable and in renpy script just globalVariable.
 
  • Like
Reactions: 79flavors

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,611
2,258
If it's just a standard RenPy variable... You can just add store. in front of the variable names.

So if you variable (or object) is globalVariable... then you can refer to it as store.globalVariable within specific init blocks. (Though I'm not 100% sure since I don't have access to my main computer right now... so you might be as well to test it).
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,979
16,236
If it's just a standard RenPy variable... You can just add store. in front of the variable names.
And it's only needed if you assign a value directly to the variable.

All the variables available in Ren'py are automatically made local to any Python block (init python: and python:) and to any inline Python ($ ...) ; said otherwise, it's like a magical global ALL_THE_VARIABLES_PLEASE in start of all embedded Python code.
Therefore, you only need to prefix them with store. when you've to remove any ambiguity.

Python:
init python:

    def myFnct():
        # Ambiguity: Is it the "myVar" made local, or are you creating a
        # purely local "myVar" ?
        myVar = 10
        # No ambiguity: It's the attribute "myVar" from Ren'py store.
        store.myVar = 10
        # No ambiguity: You haven't created a dict named myDict, but
        # there's one made local, it's it you intended to use
        myDict["whatever"] = 10
        # No ambiguity: You haven't created a list named myList, but
        # there's one...
        myList.append( 10 )
        # No ambiguity: You haven't created an object named myObj, 
        # but ...
        myObj.aFnct()
In case of doubt, add the store. prefix every time, it will works fine. But there's absolutely no need for global for any embedded Python code.
It's only the code in a ".py" file that need to both use import renpy.store as store and to use global.
 

GoldenD

Member
Sep 30, 2018
102
70
Thanks a lot,
and store. solved the problem.

For your information, I give you the exact configuration of the code :

Python:
#------------------------------------------------
#                Rpy File One
#------------------------------------------------

init python:

    class myClass:
        def __init__(self, param1="", param2=""):
            self.attr1        = param1
            self.attr2        = param2
            self.attr3        = 0


default listMyClass = [
myClass( "Param1",  "Param2"),
myClass( "Param1a", "Param2a")
]

default currentMyClass = myClass()


#------------------------------------------------
#                Rpy File Two
#------------------------------------------------

init python:

    def myFunctionOne(locParam1, locParam2, locParam3):

        # here there is no problem with listMyClass, no need of store 
        # BUT what is the difference with currentMyClass ?

        for eachMyClass in listMyClass:

            if eachMyClass.attr1 == locParam1 and eachMyClass.attr2 == locParam2:

                eachMyClass.attr3 = locParam3
                
                # BUT HERE there is problem with currentMyClass, I need store
                # If i test values of currentMyClass before this line, no values
                currentMyClass = eachMyClass
                break

        if locParam1 =="aValue":
            return myFunctionTWO()

        .../...

#------------------------------------------------
#                Rpy File Three
#------------------------------------------------

init python:

    def myFunctionTWO():

        # AND here same problem with currentMyClass, there is no values
        # I need store
        if currentMyClass.attr2 =="anotherValue":
            return 1
        else:
            return 0


#------------------------------------------------
#                Rpy File Four
#------------------------------------------------

label lblCallMyFunctions:

    if myFunctionOne(locParam1="aaa", locParam2="bbbb", locParam3=99):

        jump anotherLabel
        .../...
        # and of course i need to use currentMyClass