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

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
Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

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

#1 Post 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
Last edited by Nero on Tue Mar 13, 2018 4:53 pm, edited 1 time in total.

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

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

#2 Post 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)

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

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

#3 Post 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.

Post Reply

Who is online

Users browsing this forum: NeonNights