How do I use & declare variables without the $ symbol?

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.
Message
Author
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

How do I use & declare variables without the $ symbol?

#1 Post by henvu50 »

I still haven't figured out how to use variables without using $ constantly.

Let's say I want to declare & use a variable only within a function, how would I do that?

Code: Select all


label genericFunction:

  declare variableOnlyUsedInThisFunction = "hello world"
  
  text variableOnlyUsedInThisFunction
  
How do I just declare simple variables that are only used within a function?

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

Re: How do I use & declare variables without the $ symbol?

#2 Post by Imperf3kt »

you can use python within renpy by using "python" instead of "label"

This isn't my area of expertise, but I hope this helps a bit
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

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: How do I use & declare variables without the $ symbol?

#3 Post by Remix »

Ren'Py does not use a local scope for variables in labels. All variables referenced in labels are effectively global and should therefore be created using the "default" or "define" keywords outside of any control block.

Sub-note: It is pretty rare to ever need a label as a function (except for cases where rollback/foreward or saving is required during the flow and dialogue is used to mark those points of progress). In most cases, writing the logic in Python is often a more effective approach.
Remember that labels, like screens, are subject to prediction, so events programmed in them might be ran at odd times (so saying, I *think* label prediction is more focused on displayables)
Frameworks & Scriptlets:

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#4 Post by henvu50 »

I still can't get my head around define or default. I'm currently using $ for everything, is that bad?

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: How do I use & declare variables without the $ symbol?

#5 Post by trooper6 »

Yes it is bad.

Variables should be declared outside of any label using default if that variable will change (the most common situation) or define if the variable will not change.

Code: Select all

default color = “red”

label start:
    “Today my shirt is [red]”
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

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#6 Post by henvu50 »

trooper6 wrote: Mon Nov 05, 2018 11:20 am Yes it is bad.

Variables should be declared outside of any label using default if that variable will change (the most common situation) or define if the variable will not change.

Code: Select all

default color = “red”

label start:
    “Today my shirt is [red]”
Why is using $ everywhere bad though?

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

Re: How do I use & declare variables without the $ symbol?

#7 Post by Imperf3kt »

It messes up loading since it takes place when renpy reaches it.

For example, assume a player reads through, discovers $cake_eaten=True and then saves the game and quits.
Tomorrow, they load their previous game and renpy will set all variables that have been defined or defaulted, but it won't set any that are set using $, so $cake_eaten has no value as far as renpy is concerned, and it won't get a value either, since the player already passed the place where it does get a value. When the player reaches the point where this is checked, renpy won't find the cake_eaten variable, and exit with an error message.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

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

Re: How do I use & declare variables without the $ symbol?

#8 Post by Alex »

henvu50 wrote: Mon Nov 05, 2018 4:04 am ...
What do you mean by "use a variable only within a function"?

If you want to create a function and do some calculations within it it is ok to use local variable (but it won't be saved). Functions are created in init python block, so there's no need for $-sign.

However, Ren'Py language is the internal script language, based upon set of python functions, so you can use it or use python language.

The python stuff should be placed inside a python block or prefixed with the $-sign (if it's a single line of code).

Check some links that should help you understand the whole concept:
https://www.renpy.org/doc/html/quicksta ... statements
https://www.renpy.org/doc/html/index.ht ... and-ren-py
https://www.renpy.org/doc/html/python.html
https://www.renpy.org/doc/html/statemen ... lents.html
https://www.renpy.org/doc/html/save_load_rollback.html
viewtopic.php?f=51&t=39572#p422964

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: How do I use & declare variables without the $ symbol?

#9 Post by trooper6 »

Using a python indicator (either python: or $ -- because they are equivalent) is what you use to change variables, but not to originally set them.

So this is good:

Code: Select all

default shirt_color = "red"

label start:
    "My shirt is [shirt_color], but I want to change it."
    menu:
        "What should I change the color to?"
        "Blue!":
            $shirt_color = "blue"
        "Green!":
            $shirt_color = "green"
            
    "My shirt is now [shirt_color]"
Or you could do this, which is the same but looks clunkier:

Code: Select all

default shirt_color = "red"

label start:
    "My shirt is [shirt_color], but I want to change it."
    menu:
        "What should I change the color to?"
        "Blue!":
            python:
                shirt_color = "blue"
        "Green!":
            python:
                shirt_color = "green"
            
    "My shirt is now [shirt_color]"
For less clunky, use $ when it is only one line of python code, use python: when you are doing multiple lines of python code.

BUT, declare your variables first using default. Otherwise you will have problems with saving, loading, and rollback.
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

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#10 Post by henvu50 »

It's not working.

I'm using default declared variable, then I change it's value & save, but the change doesn't get saved, it reverts to the default value.

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#11 Post by henvu50 »

It turns out you have to progress the game forward a little bit in order to get the changed variable to actually save.

Renpy only does the save when the screen first loads. So if you have a character stat window, change strength from 50 to 60, that change won't save unless you progress the game forward by one line of dialogue. Does anyone know a work around to this?

I figured it out: https://www.renpy.org/doc/html/save_loa ... after-load

Add $ renpy.retain_after_load() at the beginning of your screen.

more info here: viewtopic.php?f=8&t=52450
Last edited by henvu50 on Tue Nov 06, 2018 12:21 am, edited 1 time in total.

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: How do I use & declare variables without the $ symbol?

#12 Post by trooper6 »

How are you changing the 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

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#13 Post by henvu50 »

trooper6 wrote: Tue Nov 06, 2018 12:18 am How are you changing the variable?
I was using setvariable.

I figured it out. I have to use $ renpy.retain_after_load()

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do I use & declare variables without the $ symbol?

#14 Post by henvu50 »

Guys, it turns out that variables declared with $ can be changed & saved.

I declared the following variable in a label:

Code: Select all


label testA:

    $ stats_test1 = 55   

I was able to change stats_test1 to 99, save the game and when I reloaded the variable retained the change to the value of 99.

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: How do I use & declare variables without the $ symbol?

#15 Post by trooper6 »

You can do what you want in your coding. However, PyTom himself has said that best practices are to declare your variables with default or define and not with $ because that will cause troubles with saving/loading and rollback....but those troubles are inconsistent. You might think it is fine, but then it isn't.

But you do what you want.
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

Post Reply

Who is online

Users browsing this forum: No registered users