Variables and Actions

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
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Variables and Actions

#1 Post by DesertFox » Tue Dec 08, 2015 11:04 am

Is it possible to adjust the variable of an action within a textbutton?

I'm used to altering displayed text using "[variable]" but I'm a little stuck trying to work out if it's possible to make a variable itself change within a button's action.

For instance:

Code: Select all

textbutton "Button" action SetVariable("variable", 1)
I understand that the above would make the variable equal to 1. But what if I wanted to change what the variable itself was, based on another variable?

Code: Select all

textbutton "Button" action SetVariable("[variable]", 1)
Doing that normally would be enough, except there are instances where the variable isn't surrounded by speechmarks. For instance, when running a defined function.

Code: Select all

timer 2.0 action Variable repeat True
How would I go about altering the variable of the button from Variable1 to Variable2 without having to use booleans outside of it or copy and paste the button a dozen times if it's being used by lots of screens?
Last edited by DesertFox on Tue Dec 08, 2015 4:47 pm, edited 2 times in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Variables and Actions

#2 Post by xela » Tue Dec 08, 2015 11:38 am

Code: Select all

textbutton "[var]" action SetVariable("var", var + 1)
Like what we're doing? Support us at:
Image

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Variables and Actions

#3 Post by DesertFox » Tue Dec 08, 2015 4:45 pm

xela wrote:

Code: Select all

textbutton "[var]" action SetVariable("var", var + 1)
This is useful, but wouldn't work if I want to change the variable of a timer or an imagebutton. Perhaps if I better explain it.

I want a variable to switch to the value of another variable before switching back once that variable has been altered. So in steps it would be.

1. Variable1 = False
2. Variable2 = False
3. Variable1=Variable2
4. Variable2=True(therefore Variable1=True)
5. Variable1 =/= Variable2
6. Variable2 = False(while Variable1=True)

Code: Select all

def ChangeVariables():
    if variable_change:
        variable1 = variable2

Code: Select all

timer 1.0 action SetVariable("Variable1", True)

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

Re: Variables and Actions

#4 Post by philat » Wed Dec 09, 2015 1:38 am

DesertFox wrote:I want a variable to switch to the value of another variable before switching back once that variable has been altered. So in steps it would be.

1. Variable1 = False
2. Variable2 = False
3. Variable1=Variable2
4. Variable2=True(therefore Variable1=True)
5. Variable1 =/= Variable2
6. Variable2 = False(while Variable1=True)
Besides the fact that I don't know what you actually want to achieve, this appears to be a fundamental misunderstanding of how variable assignment works in python.

Code: Select all

a = 1
b = a
a += 1
print a, b
will print 2 1, not 2 2, which is what you seem to be assuming will happen. (Either that, or I REALLY don't understand what you're trying to do.)

To answer the actual question you asked, the simplest way is to pass a variable to a screen, although that will obviously require actually passing the variable outside the screen.

Code: Select all

init python:
    Variable1 = False

screen test(var):
    textbutton var action SetVariable(var, True)

label start:
    "[Variable1]"
    show screen test("Variable1")
    "[Variable1]"
    "[Variable1]"

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Variables and Actions

#5 Post by xela » Wed Dec 09, 2015 3:40 am

DesertFox wrote:4. Variable2=True(therefore Variable1=True)
philat is right, therefore does not work like you wish it to, not without a good amount of bad coding to overwrite default Python behavior. Simplest way is to create a bunch of functions that do what you need them to (for really complex conditioning), work off global variables and call them using Function screen action.

Code: Select all

default var1 = False
default var2 = False

screen test():
    textbutton "Flip 1" action ToggleVariable("var1")
    textbutton "All True" action SetVariable("var1", True), SetVariable("var2", True)

    timer 1.0 action SetVariable("var1", not var2)
    timer 5.0 action SetVariable("var2", var1)

and so on...
Like what we're doing? Support us at:
Image

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Variables and Actions

#6 Post by DesertFox » Wed Dec 09, 2015 8:20 am

It was mainly to do with trying to save time copying and pasting a lot of buttons and booleans that had specific actions. So, a screen appearing based on multiple booleans and then buttons for the player to choose from that altered a specific set of variables, before closing the screen and resetting so that only one set of variables has been altered and the screen was ready for another set of variables if need be.

But it might just be that using booleans and multiple buttons is needed considering.

So in this pet shop scenario, there is first an emergency boolean, and then there are multiple emergency types that you can respond to. Let's say three types - a fire, a flood, or the pets escape. And then, there are multiple choices as to how to respond that affect tertiary variables elsewhere (not important for this). The same choice buttons are used for each emergency but their variables were to change based on the emergency type:

Code: Select all

screen pet_shop:
    if emergency:
        use emergency_choice

#what I didn't want to do
screen emergency_choice:
    if fire:
        imagebutton "image" action SetVariable("FireThrowWater", True) #etc.

#what I was hoping was possible, with "ThrowWater" being altered based on functions with booleans in init python
screen emergency_choice:
    imagebutton "image" action SetVariable("ThrowWater", True) #etc.
    timer 0.1 action EmergencyChoices repeat True

init python:
    def EmergencyChoices:
        #variables
So the ThrowWater would change to 'FireThrowWater' or 'FloodThrowWater' etc. based on the function. Basically it was a timesaving exercise. So rather than changing the True/False value of ThrowWater, I wanted to change the variable it was affecting based on a boolean elsewhere. But it'll probably require specific buttons/booleans within the screen.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Variables and Actions

#7 Post by xela » Wed Dec 09, 2015 9:51 am

DesertFox wrote:But it'll probably require specific buttons/booleans within the screen.
No it wouldn't...

Code: Select all

define var1 = False
define var2 = False
define var3 = False

screen any_screen_even_if_nested_1000000000_levels():
    textbutton "Set Global var1" action SetVatiable("va1", True)

# All vars at the same time through a function:

init python:
    def all_vars_to(boolean):
        store.var1, store.var2, store.var3 = boolean, boolean, boolean

    # Or:

    def all_vars_to(boolean):
        global var1
        global var2
        global var3
        var1, var2, var3 = boolean, boolean, boolean

screen any_screen_even_if_nested_1000000000_levels():
    textbutton "Set Global var1" action SetVatiable("va1", True)
    timer 10.0 action Function(all_vars_to, True)
When you need to change something everywhere, use global/store variables (exactly the same thing in Ren'Py), if you want to control something in screens internally, use default screen statement. There is more magic with scopes within screens but that is harder to work with...
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Google [Bot]