Referencing Ren'py variables within Python block

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
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Referencing Ren'py variables within Python block

#1 Post by namastaii »

This is probably a silly question but I couldn't find enough information on other posts or Google that helped me. I'm having a hard time referencing external variables in python functions or definitions. If I have it declared on the same page, it's fine but if it's not on the same page, the game will tell me it doesn't exist. An example:

Code: Select all

init offset = -3

default my_variable = "Hi"

init python:
    def print_myvar():
        print(my_variable)
something like this would work but if default my_variable was on another .rpy page and the def print_myvar on another, it doesn't work. Do I use global? I'm not 100% versed on mixing python with ren'py language and how they interact but I need my_variable to be as global as possible because it is editable by the user and used in multiple places/functions/etc

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Referencing Ren'py variables within Python block

#2 Post by Remix »

You would need to make sure the file creating the variable is parsed before the file using it... basically name them in the order you want them.

Alternatively, you could adjust the python init to be higher than zero, with: init 1 python:
Frameworks & Scriptlets:

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Referencing Ren'py variables within Python block

#3 Post by namastaii »

the my_variable is in a init -3 and the other is just simply in an init python

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Referencing Ren'py variables within Python block

#4 Post by trooper6 »

When creating your own classes or functions, it is its own scope. So if you want to do something with a variable in that class or function, you have to either: 1) pass the variable in, 2) having that variable be global, or 3) use your own local variable.

So

Code: Select all

default my_variable = "Hi"

init python:
    def print_myvar():
        global my_variable
        print(my_variable)
or

Code: Select all

default my_variable = "Hi"

init python:
    def print_myvar(print_var):
        print(print_var)
        
label start:
    $ print_myvar(my_variable)
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Referencing Ren'py variables within Python block

#5 Post by namastaii »

Okay thank you I'll work with that :)

Post Reply

Who is online

Users browsing this forum: Google [Bot]