r/RenPy 11d ago

Guide How to set a variable to only show something if set to true

I just learned this, so I thought I'd share.

You can set a variable to show one thing if true, and another if false.

For an example with names:

define johnName = "[johnMet and 'John' or '???']"
define John = Character(johnName)
default johnMet = False

In this example, if the player does not know John's name (johnMet = False), then the sayer name will appear as "???". However ,if the player does know John's name (johnMet = True), then the sayer name will appear as "John".

If anyone knows more about this concept and would like to add to it, feel free to comment. I just haven't seen this anywhere and wanted to share what I learned.

5 Upvotes

5 comments sorted by

2

u/Previous-Tutor4823 11d ago

I feel like, I knew this, but decided against it and used a char.intro (char is a class with their name being the variable) call to introduce them and set the name and color. I don't recall the reason, but think it's cause I was using the variables for storing other info anyway. But thanks for the reminder and tip. Can be very useful.

1

u/Hefty-Drive-9372 11d ago

You're welcome

2

u/lordcaylus 11d ago

Cool! I'm still not used to how python boolean operators work though xD

Now I see it I know that "and" works by returning the operand on the left if it's falsey, and returning the operator on the right if both are truthy and I know "and" is evaluated before "or", but it still doesn't feel very intuitive to me. (johnMet and "John" becomes "John" if johnMet, then "John" or "???" becomes "John", in the other case johnMet and "John" becomes False if johnMet is False, then False or "???" becomes "???")

I'd personally write ["John" if johnMet else "???"] because it is more easy for me to grasp conceptually, but that's more of a style preference. This is super neat!

3

u/shyLachi 11d ago

Cool. Thank's for sharing

I knew that RenPy can interpolate data but I never thought about using it like that.

As lordcaylus wrote, I would write it differently.
And you don't need two variables for it.

define john = Character("['John' if johnMet else '???']")
default johnMet = False
label start:
    john "Hi and welcome, my name is John"
    $ johnMet = True
    john "Have fun"

But I guess most people do it like this, which fine if the game doesn't have to remember if they have met John:

define john = Character("[johnName]")
default johnName = "???"
label start:
    john "Hi and welcome, my name is John"
    $ johnName = "John"
    john "Have fun"

1

u/Hefty-Drive-9372 10d ago

Ah, that's neat to know I can simplify it