Page 1 of 1

[SOLVED]Re-Define Ren'Py Value In Python Function

Posted: Tue Mar 13, 2018 4:11 pm
by Nero
I'm wondering if there is a way I could redefine Ren'Py value like this one:

Code: Select all

default bob = True
And lets say I want it to be set to false but inside a python function how do I do that?
This wont work:

Code: Select all

def make_bob_false():
    bob = False

Re: Re-Define Ren'Py Value In Python Function

Posted: Tue Mar 13, 2018 4:42 pm
by Lord Hisu
I don't know how much do you know about programming, but I'll try to explain it as simple as possible.

It's all about scope. Think of scope as a box with all the things you can use. When in the main scope, that one where you define characters, screens and other variables, you can access everything you declared.
A function have its own scope. So inside the main scope, you can work and access all the variables and functions declared, but inside the function, you can only access variables declared inside the function itself (actually, if it's a true global variable, you can access but not modify it inside the function - but you don't actually need to know this). It's like a box in a box. When in the second box, you can only work with what's inside the second box.

There's a way of saying to Python "hey, I wanna work with that variable outside of my scope", so you can alter the global value of the variable inside the function.

Code: Select all

default bob = True

def make_bob_false():
    # If you don't do this, you will create a whole new variable called bob inside the function scope, and you throw it away when the function returns.
    # This way, you're telling Python that you want to alter the value of the already created bob.
    global bob 
    bob = False
    
EDIT:

You could also do this...
This way, you're working with a different bob inside the function, but in the end, the value returned is assigned to the original bob in the main scope.

Code: Select all

default bob = True

def make_bob_false(bob_inside_scope):
    # do something with bob_inside_scope
    if bob_inside_scope != False:
        bob_inside_scope = False
    return bob_inside_scope

bob = make_bob_false(bob)

Re: Re-Define Ren'Py Value In Python Function

Posted: Tue Mar 13, 2018 4:52 pm
by Nero
Not a complete noob but I have some missing parts when it comes to coding good thing this forum exists so I can learn new stuff :) . Anyways thanks I understand what you are saying.