Ren'Py Storing Variables

JamieSadistic

Well-Known Member
Dec 19, 2018
1,417
3,511
Hey guys.
I'm currently working on the coding aspect of my first VN "A Fine Day to Die". It has 3 different end paths, 3 different lifestyle paths and 8-10 love interest paths. I'm not exactly sure how to go about coding and storing the variables for each choice that would lead down a specific path and eventually the ending written for the path chosen. Any help would be extremely appreciated.
Thanks!
 

bobdickgus

Active Member
Apr 9, 2020
711
1,933
Well I am not a python or renpy guy really, although I often unren games that don't have a walk though or modify or edit out silly minigames.

Seems to me you have all your potential variables in mind already.
So just declare them
$ BranchA = False
$ BranchB = False
$ BranchC = False
$ LifeA = False
$ LifeB = False
$ LifeC = False
$ NPCAAffection = 0
$ NPCBAffection = 0
etc.....

Code:
if NPCAAffection >= 4 and LifeA == True

     blah1



or

if NPCAAffection >= 4

    if LifeA == True

        blah1

    if LifeB == True

        blah2

    if LifeC == True

        blah3

else
    no blahs for you

etc...
 
Last edited:
  • Red Heart
Reactions: JamieSadistic

JamieSadistic

Well-Known Member
Dec 19, 2018
1,417
3,511
Well I am not a python or renpy guy really, although I often unren games that don't have a walk though or modify or edit out silly minigames.

Seems to me you have all your potential variables in mind already.
So just declare them
$ BranchA = False
$ BranchB = False
$ BranchC = False
$ LifeA = False
$ LifeB = False
$ LifeC = False
$ NPCAAffection = 0
$ NPCBAffection = 0
etc.....

Code:
if NPCAAffection >= 4 and LifeA == True

     blah1



or

if NPCAAffection >= 4

    if LifeA == True

        blah1

    if LifeB == True

        blah2

    if LifeC == True

        blah3

else
    no blahs for you

etc...
Just read this and linked stuff, it should be all you need.
https://f95zone.to/threads/newbie-guide-to-basic-variables-in-renpy.22709/
I do have all the variables in mind already, I have them typed out for clarity as well. Thank you so much for the example.... I probably should of done a search before posting this but after looking through all the Renpy online documentation, I was getting frustrated.
The link is awesome as well. Thanks for the reply man!
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,581
2,219
So just declare them

$ BranchA = False
$ BranchB = False
$ BranchC = False
$ LifeA = False
$ LifeB = False
$ LifeC = False
$ NPCAAffection = 0
$ NPCBAffection = 0
etc.....

I slightly disagree with this statement, as I think it's better to declare all variables using either define (value is never going to change while the game is running) and default (value is changed by code).

So I'd say it should be more like:

Python:
default BranchA = False
default BranchB = False
default BranchC = False
default LifeA = False
default LifeB = False
default LifeC = False
default NPCAAffection = 0
default NPCBAffection = 0
# etc.....

Any help would be extremely appreciated.

The main thing is to consider your goals.
You talk about "3 endings", "3 lifestyles" and "8 to 10 love interests".


3 endings...

Wouldn't the endings be dictated by the lifestyle and success/failure of your love interests?
You might not need separate variables to track the endings, as other variables will already have you covered.

Depending on what you have planned for those endings, I can't help but feel that 8-10 love interests is going to make things more complicated than a simple 3 endings. But that's fine - I don't know your project, so it's okay if I'm confused.


3 lifestyles...

You have a couple of options here. Let's say the lifestyles (based on your project title) are "Dom", "Sub", "Switch". Or maybe "Dom", "Sub", "Vanilla".


Your first option is a single lifestyle variable.

default lifestyle = "switch"

Okay... important lesson here. If you are going to use string (text) variables for tracking this... make sure the values are always lowercase. "Switch" and "switch" are NOT the same to a computer. It will make your life infinitely easier if your values are consistent. Especially when you're trying to find a bug where the game doesn't work because you accidentally used a capital letter within the value.

Continuing on...

Now... as your game progresses and the player makes choices... you have the option to change the value of the lifestyle variable based on those decisions.

That could be a direct choice...

Python:
default lifestyle = "switch"

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ lifestyle = "dom"

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ lifestyle = "sub"

        "Do nothing":
            "You wait to see what he does next."

Or it could be an indirect choice...

Python:
default lifestyle = "switch"

default sam_love = 0
default dave_love = 0


label start:

    scene black with fade

    # much later on within the game...

    if dave_love >= 6 and sam_love <= 3:
        $ lifestyle = "dom"

    if sam_love >= 5:
        $ lifestyle = "sub"

But... using strings generally isn't a good idea. At the very least, it's more prone to typos as well as just being more typing.

If you went this route... you could use a simple number instead. Where 1 is used instead of "dom", 2 is used instead of "sub" and 3 is used instead of "switch" (or "vanilla").

Python:
default lifestyle = 3

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ lifestyle = 1

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ lifestyle = 2

        "Do nothing":
            "You wait to see what he does next."

Obviously for this to work, you have to remember which is which both when you are setting the value and checking it.


Your second option is three variables...

Programming tends to lead to lots of checks whether something is true or false... and your choice here is no exception. Though the python programming language (and therefore RenPy) specifically uses True and False. It's case sensitive and those capital letter matter.

But you could go with...

Python:
default dom = False
default sub = False
default switch = True

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ dom = True
            $ sub = False
            $ switch = False

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ dom = False
            $ sub = True
            $ switch = False

        "Do nothing":
            "You wait to see what he does next."
            $ dom = False
            $ sub = False
            $ switch = True

Though as I type this, I'm kinda terrified that sub and switch might be reserved words within the python language. So maybe role_dom, role_sub and role_switch might be better variable names.

I am assuming here that the lifestyles are mutually exclusive and therefore the player by settings the player to one, the other two must be False.

Which brings up another option...


Your third option is only two variables...

"switch" and/or "vanilla" in this context is just someone who is neither a dom or a sub or who is both. Since "dom" and "sub" are really the only thing that matters... those might need to be our only variables for lifestyle.

Python:
default role_dom = False
default role_sub = False

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ role_dom = True

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ role_sub = True

        "Do nothing":
            "You wait to see what he does next."

    # later in the code, after more chances to set "dom" and "sub"....

    if role_dom == True and role_sub == True:
        "Player is a switch."

    if role_dom == True and role_sub == False:
        "Player is a dom."

  if role_dom == False and role_sub == True:
        "Player is a sub."

    if role_dom == False and role_sub == False:
        "Player is vanilla."

Though depending on your game's needs, maybe both values being True or both values being False could signify a "switch" too.

As with any programming language, there are alternative ways to structure those final four checks. A more experienced programmer might nest those if statements within each other...

Python:
    if role_dom == True:
        if role_sub == True:
            "Player is a switch."
        else:
            "Player is a dom."
     else:
        if role_sub == True:
            "Player is a sub."
        else:
            "Player is vanilla."

My conclusion...

If it were just a choice between "dom" and "sub"... I'd use True/False and have a single variable.

But since you said 3 lifestyles... I'd again go with a single variable... this time a number. Where dom=1, sub=2, switch=3. (maybe vanilla=0 ?). Dunno if that makes sense for your game, but presuming lifestyle is what I think it is... then a number solution makes sense to me. If you're more comfortable using strings/text... then use strings.

However... maybe lifestyles are not mutually exclusive. Maybe my example of "dom" AND "sub" being a "switch" make sense in the context of your design... so my example of combinations using True/False may suit you better.


8-10 Love Interests...

99 times out of a 100 this is going to be a number. You start at zero and add 1 or 2 or whatever each time you earn points with that love interest.

The other 1 out of a 100 is going to be a simple Yes/No solution... or in computer terms... True or False.

But in all likelyhood, it's going to be that number... and since I haven't covered numbers is much detail yet - I may as well assume number here.

Python:
default dave_love = 0
default sam_love = 0
default marvin_love = 0
default george_love = 0
default marty_love = 0
default milton_love = 0
default lou_love = 0
default wilbur_love = 0

label start:

    scene black with fade

    "You see Sam stood by the lockers."

    menu:
        "He looks tasty":
            $ sam_love += 1

        "I'm unimpressed":
            $ sam_love -= 1

        "Whatever":
            pass
  • $ is RenPy for "do python command"
  • += 1 is shorthand for "add 1 to this variable".
  • -= 1 is shorthand for "subtract 1 to this variable".
  • pass is RenPy for "do nothing". You only use it when no other command fits the situation.
Beyond that, it's only about checking the values of those numbers as the game progresses. There are already examples of if checks above.

Just know that:
  • == checks to see if two things are equal.
  • != checks to see if two things are not equal.
  • < checks to see if the first thing is less than the second thing.
  • > checks to see if the first thing is greater than the second thing.
  • <= checks to see if the first thing is less than or equal to the second thing.
  • >= checks to see if the first thing is greater than or equal to the second thing.
Finally, there's checking things in combination with each other using and, or and not.

if role_dom and milton_love >= 6: or perhaps if wilbur_love == 0 or george_love <= 3:.


I would add a word of caution here too. It's very easy to get into a habit of gating off content based on number values.

Python:
    # somewhere in the code...

    if dave_love <= 6:
        jump ch3_skip_shower_scene


label ch3_shower_scene:

    # lots of pretty graphics and text.


label ch3_skip_shower_scene:

    "And then I went to bed."

But a lot of devs will do this check based on the maximum that value can be at that moment. So if the player could have potentially done +1, +1, +1, +2, +1 and +1... the maximum is currently 7. It's very easy to write if dave_love >= 7: or if dave_love < 7:. Except for a player to see that content, you've now made it so the player must have played the PERFECT game so far and not made a single bad choice. Not only have you made a walkthough pretty much mandatory for players - all those extra scenes you wrote for what happens if the player makes the "other" choice is pretty much wasted effort on your part. The trick is making "bad choices" rewarding too (not easy) or at least reducing the threshold down from 7 to maybe 4 or 5.


I know that's a lot to get through... so congrats if you made it this far.
 
Last edited:

JamieSadistic

Well-Known Member
Dec 19, 2018
1,417
3,511
The main thing is to consider your goals.
You talk about "3 endings", "3 lifestyles" and "8 to 10 love interests".


3 endings...

Wouldn't the endings be dictated by the lifestyle and success/failure of your love interests?
You might not need separate variables to track the endings, as other variables will already have you covered.

Depending on what you have planned for those endings, I can't help but feel that 8-10 love interests is going to make things more complicated than a simple 3 endings. But that's fine - I don't know your project, so it's okay if I'm confused.
While you're partially correct that the endings are dictated by the lifestyle, they're also depending on which love interest you pick to pursue. It's not a typical story about trying to have sex as much as possible with everyone you can though. The endings have alterations depending on one central choice of the whole game. If you do it, the endings are relatively happy...if you don't do it, the endings are catastrophic. They are going to be a life or death situation. The catastrophic choices will have one path which will lead to suicide, one path that will lead to prison or psychiatric institutionilization. The other path will turn you into a hermit, dying in isolation.

3 lifestyles...

You have a couple of options here. Let's say the lifestyles (based on your project title) are "Dom", "Sub", "Switch". Or maybe "Dom", "Sub", "Vanilla".


Your first option is a single lifestyle variable.

default lifestyle = "switch"

Okay... important lesson here. If you are going to use string (text) variables for tracking this... make sure the values are always lowercase. "Switch" and "switch" are NOT the same to a computer. It will make your life infinitely easier if your values are consistent. Especially when you're trying to find a bug where the game doesn't work because you accidentally used a capital letter within the value.

Continuing on...

Now... as your game progresses and the player makes choices... you have the option to change the value of the lifestyle variable based on those decisions.

That could be a direct choice...

Python:
default lifestyle = "switch"

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ lifestyle = "dom"

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ lifestyle = "sub"

        "Do nothing":
            "You wait to see what he does next."

Or it could be an indirect choice...

Python:
default lifestyle = "switch"

default sam_love = 0
default dave_love = 0


label start:

    scene black with fade

    # much later on within the game...

    if dave_love >= 6 and sam_love <= 3:
        $ lifestyle = "dom"

    if sam_love >= 5:
        $ lifestyle = "sub"

But... using strings generally isn't a good idea. At the very least, it's more prone to typos as well as just being more typing.

If you went this route... you could use a simple number instead. Where 1 is used instead of "dom", 2 is used instead of "sub" and 3 is used instead of "switch" (or "vanilla").

Python:
default lifestyle = 3

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ lifestyle = 1

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ lifestyle = 2

        "Do nothing":
            "You wait to see what he does next."

Obviously for this to work, you have to remember which is which both when you are setting the value and checking it.


Your second option is three variables...

Programming tends to lead to lots of checks whether something is true or false... and your choice here is no exception. Though the python programming language (and therefore RenPy) specifically uses True and False. It's case sensitive and those capital letter matter.

But you could go with...

Python:
default dom = False
default sub = False
default switch = True

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ dom = True
            $ sub = False
            $ switch = False

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ dom = False
            $ sub = True
            $ switch = False

        "Do nothing":
            "You wait to see what he does next."
            $ dom = False
            $ sub = False
            $ switch = True

Though as I type this, I'm kinda terrified that sub and switch might be reserved words within the python language. So maybe role_dom, role_sub and role_switch might be better variable names.

I am assuming here that the lifestyles are mutually exclusive and therefore the player by settings the player to one, the other two must be False.

Which brings up another option...


Your third option is only two variables...

"switch" and/or "vanilla" in this context is just someone who is neither a dom or a sub or who is both. Since "dom" and "sub" are really the only thing that matters... those might need to be our only variables for lifestyle.

Python:
default role_dom = False
default role_sub = False

label start:

    scene black with fade

    menu:
        "Stand up to him":
            "You slap his hand away."
            $ role_dom = True

        "Cower backward":
            "You take a step backward and sink to your knees."
            $ role_sub = True

        "Do nothing":
            "You wait to see what he does next."

    # later in the code, after more chances to set "dom" and "sub"....

    if role_dom == True and role_sub == True:
        "Player is a switch."

    if role_dom == True and role_sub == False:
        "Player is a dom."

  if role_dom == False and role_sub == True:
        "Player is a sub."

    if role_dom == False and role_sub == False:
        "Player is vanilla."

Though depending on your game's needs, maybe both values being True or both values being False could signify a "switch" too.

As with any programming language, there are alternative ways to structure those final four checks. A more experienced programmer might nest those if statements within each other...

Python:
    if role_dom == True:
        if role_sub == True:
            "Player is a switch."
        else:
            "Player is a dom."
     else:
        if role_sub == True:
            "Player is a sub."
        else:
            "Player is vanilla."

My conclusion...

If it were just a choice between "dom" and "sub"... I'd use True/False and have a single variable.

But since you said 3 lifestyles... I'd again go with a single variable... this time a number. Where dom=1, sub=2, switch=3. (maybe vanilla=0 ?). Dunno if that makes sense for your game, but presuming lifestyle is what I think it is... then a number solution makes sense to me. If you're more comfortable using strings/text... then use strings.

However... maybe lifestyles are not mutually exclusive. Maybe my example of "dom" AND "sub" being a "switch" make sense in the context of your design... so my example of combinations using True/False may suit you better.


8-10 Love Interests...

99 times out of a 100 this is going to be a number. You start at zero and add 1 or 2 or whatever each time you earn points with that love interest.

The other 1 out of a 100 is going to be a simple Yes/No solution... or in computer terms... True or False.

But in all likelyhood, it's going to be that number... and since I haven't covered numbers is much detail yet - I may as well assume number here.

Python:
default dave_love = 0
default sam_love = 0
default marvin_love = 0
default george_love = 0
default marty_love = 0
default milton_love = 0
default lou_love = 0
default wilbur_love = 0

label start:

    scene black with fade

    "You see Sam stood by the lockers."

    menu:
        "He looks tasty":
            $ sam_love += 1

        "I'm unimpressed":
            $ sam_love -= 1

        "Whatever":
            pass
  • += 1 is shorthand for "add 1 to this variable".
  • -= 1 is shorthand for "subtract 1 to this variable".
  • pass is RenPy for "do nothing". You only use it when no other command fits the situation.
Beyond that, it's only about checking the values of those numbers as the game progresses. There are already examples of if checks above.

Just know that:
  • == checks to see if two things are equal.
  • != checks to see if two things are not equal.
  • < checks to see if the first thing is less than the second thing.
  • > checks to see if the first thing is greater than the second thing.
  • <= checks to see if the first thing is less than or equal to the second thing.
  • >= checks to see if the first thing is greater than or equal to the second thing.
Finally, there's checking things in combination with each other using and, or and not.

if role_dom and milton_love >= 6: or perhaps if wilbur_love == 0 or george_love <= 3:.


I would add a word of caution here too. It's very easy to get into a habit of gating off content based on number values.

Python:
    # somewhere in the code...

    if dave_love <= 6:
        jump ch3_skip_shower_scene


label ch3_shower_scene:

    # lots of pretty graphics and text.


label ch3_skip_shower_scene:

    "And then I went to bed."
But a lot of devs will do this check based on the maximum that value can be at that moment. So if the player could have potentially done +1, +1, +1, +2, +1 and +1... the maximum is currently 7. It's very easy to write if dave_love >= 7: or if dave_love < 7:. Except for a player to see that content, you've now made it so the player must have played the PERFECT game so far and not made a single bad choice. Not only have you made a walkthough pretty much mandatory for players - all those extra scenes you wrote for what happens if the player makes the "other" choice is pretty much wasted effort on your part. The trick is making "bad choices" rewarding too (not easy) or at least reducing the threshold down from 7 to maybe 4 or 5.


I know that's a lot to get through... so congrats if you made it this far.
The dom/sub/switch roles aren't the lifestyles I'm specifically having in the game, they will possibly be featured though. However, even though the titles of the lifestyles aren't what I'm doing, I can easily substitute them with the lifestyles I need. The coding all looks exactly like what I need, with slight changes to scenarios and choice labels obviously.
You're right when you said it's a lot to get through and while I skimmed through it on here, I copy and pasted it into my notepad so I can go through it more extensively. I thank you with extreme gratitude for the depth you went to for your explanation. I never expected someone to actually take the time to put that much detail into a response and for that I'm eternally grateful.
 

crabsinthekitchen

Well-Known Member
Apr 28, 2020
1,550
8,801
luckily I'm not a game dev or I would just create a bunch of variables for every decision and think about what to do with them later

Python:
default choice1 = ""
default choice2 = ""

menu:
    "Option 1":
        $ choice1 = "1"
    "Option 2":
        $ choice1 = "2"
    "Option 3":
        $ choice1 = "3"

menu:
    "Second decision 1":
        $ choice2 = "1"
    "Second decision 2":
        $ choice2 = "2"

or a slightly cleaner approach
Python:
default events = set()

menu:
    "Stop and say hi":
        events.add("girl1_said_hi")
        jump day1_late_for_work
    "Keep walking":
        jump day1_work_on_time


menu:
    "Stay at work":
        jump day5_work_overtime
    "Go home early":
        events.add("day5_left_early")
        jump day5_home_early


# 5 updates later
if "girl1_said_hi" in events and "day5_left_early" in events:
    # getting fired from work and no longer able to hook up with the boss because you missed two important choices
if "girl1_said_hi" not in events:
    # girl 1 and girl 9 talk about how rude you were on the first day
    
# or maybe you decide that these decisions were meaningless and ignore them for the rest of the game
Most people don't store variables this way (Philly does something similar for tracking choices but he uses relationship points too) but I like this more than using numbers because it fucks with people and I like it when people have no idea what's going on and have to read the actual dialogue instead of just watching their relationship points go up. You can change dialogue in the ending based on some minor choice from the beginning of the game even if you didn't plan to do it when you started. You can completely ignore some of the choices that seemed important but aren't relevant to the story without telling that the choice is irrelevant. Only you would know what's going on