Everything about Global Variables [CLOSED]

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
YeOlde_Monk
Newbie
Posts: 9
Joined: Wed Jun 04, 2014 9:58 am
Completed: --Work in progress--
Projects: *Doesn't_have_name_yet*
Organization: Tolma4 Team
Location: Russia, Tambov
Contact:

Everything about Global Variables [CLOSED]

#1 Post by YeOlde_Monk »

Hey fellas.
I know that maybe I am littering the forum, but I really tried my best to look through forum with my problem.

So, the question of the day is: how can I define a variable that whole game can see it? 'Cause what I was doing is this:

Code: Select all

$ var = 0
But the thing is I wrote it in the script.rpy, but the other part of game/code is in another tab/page/list or whatever this ".rpy"-thing called.
So I thought about global variables (sounds logical to me). BUT!
Googlin' through Internet I didn't actually found tutorials for them, just tiny bits of everything. So from what I saw, I entered this:

Code: Select all

$ global var
$ var = 0
To my surprise, he ate it. But there still was same error, when it came to 'point-adding moment'.
Of course, I'd found some python code, but it's pretty hard for me (at least it looks like it).
I've tried as well copying part of the code from some game with money (it definitely must be a global variable, isn't it?) Failed that as well... Must be losing some part at the start...

So is there any simpler way to define and use global variable in Ren'Py, or the only way to do that is to learn Python?
Last edited by YeOlde_Monk on Sun May 10, 2015 6:15 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Everything about Global Variables

#2 Post by philat »

You misunderstand what global variables are. Global variables are a python concept, not renpy per se, and distinguish variables used globally from ones used within a function.

Code: Select all

def foo(bar):
    bar += 1
    return bar

print foo(1)
For example, bar in the above is a local variable. Trying to access bar outside of the function (e.g., a print bar command) will throw an error, as bar does not exist outside of the function.

Code: Select all

def foo():
    global bar
    bar += 1
    return bar

bar = 1
print foo()
Here, bar is a global variable. By designating bar as a global variable, foo() knows to access a bar that is in a different scope. If you didn't have the global bar line, the function would throw an error that bar is not defined.

In any case, this is just to help you understand what global variables are and is mostly not relevant to renpy scripting. Renpy reads all .rpy files as if they constitute one large file. All .rpy files have access to variables declared in other files. The only thing you have to do is make sure that variables are initialized properly before they are used. The way to do this is to declare them either in init blocks or right after label start. I myself prefer to do them in init blocks, but if you have complex classes or other instances where rollback and save may be compromised, declaring things after the start label is safest.

Code: Select all

init python:
    var = "default value"

label start:
    $ var = "default value"

User avatar
YeOlde_Monk
Newbie
Posts: 9
Joined: Wed Jun 04, 2014 9:58 am
Completed: --Work in progress--
Projects: *Doesn't_have_name_yet*
Organization: Tolma4 Team
Location: Russia, Tambov
Contact:

Re: Everything about Global Variables

#3 Post by YeOlde_Monk »

Well...))) Who would've thought it was so simple?) Now it's working! Thank you very much! Guess now I have everything to complete my game)

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot