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: 1289
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life, Coffea Beans
Github: namastaii
Skype: Discord: lunalucid#7684
Soundcloud: deadseed
itch: lunalucid
Location: USA
Contact:

Referencing Ren'py variables within Python block

#1 Post by namastaii » Tue Jul 30, 2019 3:26 pm

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: 1298
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 » Tue Jul 30, 2019 7:40 pm

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:
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

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

Re: Referencing Ren'py variables within Python block

#3 Post by namastaii » Tue Jul 30, 2019 8:27 pm

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: 3603
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 » Tue Jul 30, 2019 9:38 pm

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: 1289
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life, Coffea Beans
Github: namastaii
Skype: Discord: lunalucid#7684
Soundcloud: deadseed
itch: lunalucid
Location: USA
Contact:

Re: Referencing Ren'py variables within Python block

#5 Post by namastaii » Tue Jul 30, 2019 9:44 pm

Okay thank you I'll work with that :)

Post Reply

Who is online

Users browsing this forum: hell_oh_world