What is the difference between default, define, global, python, init, and $?

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
LiveTurkey
Regular
Posts: 104
Joined: Tue Dec 27, 2016 10:50 pm
Location: Brooklyn
Contact:

What is the difference between default, define, global, python, init, and $?

#1 Post by LiveTurkey » Mon Jul 16, 2018 9:05 pm

I can't seem to understand this.

Right now, I want to make an array variable called deckOfCards. Since a deck of cards is pretty static, I want to make it a global variable that any minigame I create can access.

Here is the one example of code that would call this deckOfCards variable:

Code: Select all

label game:
    ...
    init python:
    	...
        for k in deckOfCards:
        ...
I can't seem to figure out how to do this relative simple task.

First I tried

Code: Select all

default deckOfCards = ['A', 'B', 'C','D','E']
and placed that before my start label. However, I get nameError, deckOfCards is not defined

I tried replacing

Code: Select all

default
with

Code: Select all

define
, same exact error.

What is the difference between both of these? What happens if I don't include either and just do deckOfcards = X?

I know that when you create a variable inside a function, it can only be used inside of that function, unless you make it a global variable. Global variable can be used in all functions right? Are "labels" and "screens" functions? If you create variable inside of a label or a screen, can they be used outside of that label or screen?

I'm assuming not to both because

Code: Select all

init python:
    global deckOfCards
    deckOfCards = ['A', 'B']
gives me the same exact error as before. So how do I create a variable that can be seen by all functions, screens, and labels?

How come you can't define a global variable in the same line that you declare it?

Is there any difference between putting your code after a python: and just putting a dollar sign before each line? Init just makes whatever code is in the block run before all other code right? You can do init with python or without? I'm a little confused about all this stuff.

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: What is the difference between default, define, global, python, init, and $?

#2 Post by trooper6 » Mon Jul 16, 2018 10:46 pm

In order to declare a variable you use either define or default, outside of any block.

-You use define for variables that will not change, default for variables that will change.
-global is used inside of functions in order to get access to the variables you have previously declared.
-init is used when come code has to be run before other code. For example, if you define a class, you want to make sure that is run before you declare any classes. Generally I put all my class and function definitions inside init blocks.
-The only different between $ and python is that $ is for only one line, and python can be used for a block.

I'll put in some code as an example in a few seconds.
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
Imperf3kt
Lemma-Class Veteran
Posts: 3038
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#3 Post by Imperf3kt » Mon Jul 16, 2018 11:11 pm

I suspect, since you have defined "deckOfCards" as a list, that the items in your list are undefined, and so, "devkOfCards", is still undefined. Try defining A, B, C, D and E

*Disclaimer*
I am an absolute novice when it comes to Python, I mostly get by, by reading documentation but never actually fully understanding it.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py
Free Android GUI - Updated occasionally

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: What is the difference between default, define, global, python, init, and $?

#4 Post by trooper6 » Mon Jul 16, 2018 11:24 pm

So create a brand new project and but this code in a fresh script.rpy file.
It works and should show you all these commands in context.

Code: Select all

define p = Character('Perry')

define days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] #This is a define because it won't change
default current_day = 0 #This is a default because it will change during the course of the game
default gold = 0 #This is a default because it will change during the course of the game

init python:
    def advanceDay(): #this is a function that needs to be defined before anything else so it goes into and init. It is also python code.
        global current_day #we want this function to be able to have access to the global current_day rather than create its own, so we use global here
        global gold #we want this function to be able to have access to the global gold rather than create its own, so we use global here
        gold += 10
        if current_day == 6:
            current_day = 0
        else:
            current_day +=1

screen info(): #Here is a screen where we want to display the day and the gold.
    frame:
        xalign 1.0
        yalign 0.0
        vbox:
            text "Current Day: {0}".format(days[current_day]) #screens can have for loops and also do python style string interpolation
            text "Gold: [gold]" #screens can also do Renpy style string interpolation

## The game starts here.

label start:
    scene bg underpassD
    show screen info()
    p "Hello! What day is it? Right now?"
    $p("It is {0}".format(days[current_day])) #if you want to to python style string interpolation, which you do if you want to access list elements, use a python equivalent for the say statement
    p "I have [gold] gold pieces." #if you are using Renpy style string interpolaiton you don't need a python equivalent
    $advanceDay() #Calling a function is a python statement, so you know $
    p "What day is it now?"
    $p("It is {0}".format(days[current_day]))
    p "I have [gold] gold pieces."
    $advanceDay()
    p "We are advancing a day! [current_day]"
    $advanceDay()
    p "We are advancing a day! [current_day]"
    $advanceDay()
    p "We are advancing a day! [current_day]"
    $advanceDay()
    p "We are advancing a day! [current_day]"
    $advanceDay()
    p "We are advancing a day! [current_day]"
    $advanceDay()
    p "We are advancing a day! [current_day]...just so you can see the rollover. You are looking at the screen in the top right, correct?"
    p "Okay, game over."


    return
You may also want to look at the documentation that describes default, define, etc: https://www.renpy.org/doc/html/python.html#python
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
LiveTurkey
Regular
Posts: 104
Joined: Tue Dec 27, 2016 10:50 pm
Location: Brooklyn
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#5 Post by LiveTurkey » Tue Jul 17, 2018 12:28 am

trooper6 wrote:
Mon Jul 16, 2018 11:24 pm
So create a brand new project and but this code in a fresh script.rpy file.
It works and should show you all these commands in context.

You may also want to look at the documentation that describes default, define, etc: https://www.renpy.org/doc/html/python.html#python
Are variable defined in labels or screen local only to that label/screen? What about a variable defined in the start label?

What is a label and what is a screen in python terms? Is it a function?

Can you use global variable in labels and screens?

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: What is the difference between default, define, global, python, init, and $?

#6 Post by trooper6 » Tue Jul 17, 2018 12:36 am

Don't define variables inside of labels. Define your variables outside of any block using define or default.

If you define a variable inside a screen, that is called a screen variable and it is local only: https://www.renpy.org/doc/html/screens.html#default

labels and screens aren't python things, they are Ren'Py things...so I don't tend to try to think of them as python things.

Can you use global variable in labels and screens? If I understand you correctly, yes...and I do in the code I provided. I declared the variable gold, for example, and used it inside the start label and the info screen.
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
xavimat
Eileen-Class Veteran
Posts: 1446
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#7 Post by xavimat » Tue Jul 17, 2018 5:36 am

Your first attempt was almost correct. It had only one error: the init word.
This code works:

Code: Select all

default deckOfCards = ['A', 'B', 'C','D','E']
label game:
    ...
    python:
    	...
        for k in deckOfCards:
        ...
Note that I only have changed one simple thing: I've deleted the "init" word in the "python" line.

When you tell renpy to execute something in "init", then it executes before the main menu, even if you have placed it (mistakenly) "inside" a label. If the word "init" is there, it is executed at init time, no matter what. Your error was that the "init python" block was executed before the default (or define), so the variable was not still defined.

Try not to mix renpy language and python language. Actually, renpy includes three languages: for labels, for screens and ATL.

All variables defined in labels, default, define or init are global in renpy. Only variables defined inside screens are local to the screen.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
LiveTurkey
Regular
Posts: 104
Joined: Tue Dec 27, 2016 10:50 pm
Location: Brooklyn
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#8 Post by LiveTurkey » Tue Jul 17, 2018 1:31 pm

xavimat wrote:
Tue Jul 17, 2018 5:36 am
Your first attempt was almost correct. It had only one error: the init word.
This code works:

Code: Select all

default deckOfCards = ['A', 'B', 'C','D','E']
label game:
    ...
    python:
    	...
        for k in deckOfCards:
        ...
Note that I only have changed one simple thing: I've deleted the "init" word in the "python" line.

When you tell renpy to execute something in "init", then it executes before the main menu, even if you have placed it (mistakenly) "inside" a label. If the word "init" is there, it is executed at init time, no matter what. Your error was that the "init python" block was executed before the default (or define), so the variable was not still defined.

Try not to mix renpy language and python language. Actually, renpy includes three languages: for labels, for screens and ATL.

All variables defined in labels, default, define or init are global in renpy. Only variables defined inside screens are local to the screen.
Yep that fixed it.

On a slightly unrelated note, does order ever matter? Does a screen have to be above the area it was called (in code)? Out of labels, screens, functions, etc does order matter for any of them?

User avatar
Alex
Lemma-Class Veteran
Posts: 2515
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#9 Post by Alex » Tue Jul 17, 2018 3:50 pm

This definitely should help to understand how things in Ren'py goes.
viewtopic.php?f=51&t=39572
https://www.renpy.org/doc/html/index.html

User avatar
xavimat
Eileen-Class Veteran
Posts: 1446
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#10 Post by xavimat » Tue Jul 17, 2018 6:32 pm

LiveTurkey wrote:
Tue Jul 17, 2018 1:31 pm
On a slightly unrelated note, does order ever matter? Does a screen have to be above the area it was called (in code)? Out of labels, screens, functions, etc does order matter for any of them?
Order only matters in every phase:
- In init phase: init, define, default, image are executed. If you have different files with these, the order is (almost) alphabetical (unicode-order, actually). So the init phase of a "definitions.rpy" file will be executed before the init phase of a "script.rpy" file.
Better don't use double zero at the start of your filenames ("00mydefinitions.rpy"), because renpy uses that to ensure that its definitions are executed before yours.
You can use a number init 2: or init -5 python: if you need, for example, custom classes defined before subclasses. But this is only useful in very complex games with a lot of definitions, if you can have your classes in one file, put them in the logical order you need.

- screens (and transforms) are read by renpy but not executed until some label triggers them (with show screen or call screen) Screens can be predicted, so the code inside the screen shouldn't have side effects (like changing directly a global variable or calling directly a function; that can be done with Actions).

- labels are executed in the order you decide. Usually, label start is the first one, and then, with jump or call, the flow of the program goes on.
Some people puts labels after labels without explicit jumping. Renpy will go on to the next line if a label ends, but I think this is not good practice:

Code: Select all

label start:
    "something"
label chapter_1:
    "More something"
label chapter_2:
    "The last something"
    "END"
    return
I prefer to put a jump even if it's obvious. I can reorganize labels in the future without breaking anything. And maybe add some small branching in there.

Code: Select all

label start:
    "something"
    jump chapter_1
label chapter_1:
    "More something"
    jump chapter_2
label chapter_2:
    "The last something"
    "END"
    return
When your game grows, rpy files can be split to have things in order and easy to find. I usually create a "definitions.rpy" file and put there all my defaults, defines and init. And other myscreens.rpy with customs screens. But it's up to you and your type of game. For example, a file "battle.rpy" can hold all the mechanics of the combat if your game has it. I use a "connect.rpy" file in my projects with Internet connection. And so on.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3038
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: What is the difference between default, define, global, python, init, and $?

#11 Post by Imperf3kt » Tue Jul 17, 2018 8:14 pm

xavimat wrote:
Tue Jul 17, 2018 6:32 pm
- labels are executed in the order you decide. Usually, label start is the first one, and then, with jump or call, the flow of the program goes on.
Some people puts labels after labels without explicit jumping. Renpy will go on to the next line if a label ends, but I think this is not good practice:

Code: Select all

label start:
    "something"
label chapter_1:
    "More something"
label chapter_2:
    "The last something"
    "END"
    return
I prefer to put a jump even if it's obvious. I can reorganize labels in the future without breaking anything. And maybe add some small branching in there.

Code: Select all

label start:
    "something"
    jump chapter_1
label chapter_1:
    "More something"
    jump chapter_2
label chapter_2:
    "The last something"
    "END"
    return
When your game grows, rpy files can be split to have things in order and easy to find. I usually create a "definitions.rpy" file and put there all my defaults, defines and init. And other myscreens.rpy with customs screens. But it's up to you and your type of game. For example, a file "battle.rpy" can hold all the mechanics of the combat if your game has it. I use a "connect.rpy" file in my projects with Internet connection. And so on.
To an extent, its actually very useful for looping or returning to a reused label.
In my opinion, depending on how ypur game is structured, it is only necessary for the replay feature, to add jumps and returns to labels that directly flow into the next.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py
Free Android GUI - Updated occasionally

Post Reply

Who is online

Users browsing this forum: No registered users