[Solved] Using conditional statements within dialogue

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
alee
Newbie
Posts: 7
Joined: Sat May 05, 2018 7:17 pm
Contact:

[Solved] Using conditional statements within dialogue

#1 Post by alee »

Hi everyone! I'm working away at my game and between the tutorials, the documentation, and this board, things are going smoothly! Great work everyone!

The problem I'm having now seems like it should be simple, but I can't think of a way to do it. Most of my game is in nvl mode, with all dialogue spoken by a narrator rather than characters. More similar to interactive fiction than a visual novel, but I like ren'py and I know a little bit of python so I'm hoping to make it work.

I want to use conditional statements (if/elif/else) to change words within dialogue without skipping a line or requring click-to-continue so it's seamless.

I can use if statements to control which sentence is displayed, but most of my sentences are in full on paragraphs and I'd like to keep it cleaner if I can. Is there a way to use conditional statements in python within the dialogue without it skipping a line or requiring click-to-continue?

Something like the following:

Code: Select all

"Brushing your teeth, you contemplate your day tomorrow. The CEO had mentioned an important meeting "
if k_gender == "Man":
    "he"
elif k_gender == "Woman":
    "she"
else:
    "they"
" wanted you to attend."
I want it to appear seamless and instantaneously:
""Brushing your teeth, you contemplate your day tomorrow. The CEO had mentioned an important meeting she wanted you to attend."


However, this code will appear in the game as

"Brushing your teeth, you contemplate your day tomorrow. The CEO had mentioned an important meeting <click>

she <click>

wanted you to attend."

I know I can use if statements to choose which sentence to portray but then I'd have like 12 duplicates of the same paragraph for each choice option, and I like to keep it simple, if it's possible to do at all.

Any ideas? Thanks everyone!
Last edited by alee on Tue May 29, 2018 4:15 pm, edited 2 times in total.

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

Re: Using conditional statements within dialogue

#2 Post by Alex »

Try something like

Code: Select all

label start:
    $ k_gender = "Man"
    if k_gender == "Man":
        $ var = "he"
    elif k_gender == "Woman":
        $ var = "she"
    else:
        $ var = "they"

    "Brushing your teeth, you contemplate your day tomorrow. The CEO had mentioned an important meeting [var] wanted you to attend."
https://www.renpy.org/doc/html/text.htm ... ating-data

alee
Newbie
Posts: 7
Joined: Sat May 05, 2018 7:17 pm
Contact:

Re: Using conditional statements within dialogue

#3 Post by alee »

That is my plan if I can't do conditional statements within the dialogue. I was hoping to avoid doing that because it gets tricky in more complicated statements.

For example,
"He walks to the bank, thinking about a cute dog he saw once. The dog was fluffy and he likes fluffy dogs a lot."

This is easy to do with defining the pronoun he or she and using conditional logic the normal way. However, it's an issue when it comes to "they" because it changes not only the pronoun but the word after it (walks/likes in this example).

"They walk to the bank, thinking about a cute dog they saw once. The dog was fluffy and they like fluffy dogs a lot"

In this sort of situation (which pops up in my game in nearly every piece of dialogue), I'd like to have:
"[conditional:he walks\she walks\they walk] to the bank, thinking about a cute dog they saw once. The dog was fluffy and [conditional:he likes\she likes\they like] fluffy dogs a lot"

What I want to do might not be possible, but I'm still hopeful someone might have some ideas I haven't thought of!

Even if I do it the normal conditional statement way, I still run into the click to continue problem. I would like everything after the conditional statement to appear without clicking. Also not possible??

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Using conditional statements within dialogue

#4 Post by kivik »

Sounds like you need to:
1 design a message format for these conditions
2 create a text parser function, which will pull out the conditions into lists, and then based on the gender / pronoun parameter generate your final list of chosen text.
3 substitute it back into your message

Since it seems like an interesting problem, I've had a go at doing it:

Code: Select all

# fixed indexes for order of he, she and they as they would appear in all your messages
define pronoun_index = {"he": 0, "she": 1, "they": 2}
define narrator = Character("")

init python:
    import re
    def message_parser(message, pronoun):
        # regex to pull out all the instances of double [[]] brackets
        condition_list = re.findall('\[\[(.*?)\]\]', message, re.DOTALL)

        # loop through all the conditions
        for condition in condition_list:
            # replace the condition block with the chosen index:
                # "[[%s]]" % condition is the condition block, have to add the square brackets back since the regex pick up what's in between, e.g. [[He is,She is,They are]]
                # condition.split(",") returns a list of delimited strings ["He is", "She is", "They are"]
                # use pronounc_index[pronoun] to decide which of index from the list to replace our condition block by
            message = message.replace("[[%s]]"%condition, condition.split(",")[pronoun_index[pronoun]])
        return message

label start:
    # test code
    $ message = "[[He is,She is,They are]] going for a walk. [[He wants,She wants,They want]] to make sure to catch the sunset."
    $ output = message_parser(message, "she")
    "[output]"
    
    # do it directly with renpy.say
    $ renpy.say(narrator, message_parser("[[He wishes,She wishes,They wish]] you good luck on your journey as [[he sets,she sets,they set]] off", "they"))

    # use a label to shorten things, you could even just make a python function
    call msg("You bid [[him,her,them]] farewell as [[he,she,they]] turn.", "he")
    return

label msg(message, pronoun):
    $ renpy.say(narrator, message_parser(message, pronoun))
    return
I assume you're ok with working out the logic to put the pronoun in - the "they" is trickier since it means you need a condition to determine the pronoun rather than just storing a character's pronoun and passing it as an argument. I don't know how you plan on managing the information, but I guess you can just throw the pronoun into a global variable can use that - in which case you can further simplify your code by picking the pronoun from said variable instead of reading it as an argument each time.

alee
Newbie
Posts: 7
Joined: Sat May 05, 2018 7:17 pm
Contact:

Re: Using conditional statements within dialogue

#5 Post by alee »

This might be a little above my programming skill so it will take me a bit to work through it. However, it gives me something to work off of and try out!! Thanks so much for taking the time to think about this :)

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

Re: Using conditional statements within dialogue

#6 Post by philat »

(If you decide that the parser isn't the way you want to go) Use {nw} for the ctc part.

ETA: {nw} and extend, to be clearer.

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

Re: Using conditional statements within dialogue

#7 Post by xavimat »

EDITED TO ADD "ies"
EDITED TO ADD "self"
(Late EDIT: It lacks the pronoun "hers", "Hers". So all text should be written feminine instead of masculine as below.)

Here are two things:
1. You ask for conditionals and talk about clicks and so.
2. You show a use case that can be done in a totally different way.

1. Use {nw} and extend (as suggested above).

Code: Select all

    if gender == "male":
        "He {nw}"
    else:
        "She {nw}"
    extend "was walking by the lonely street, {nw}"
    if gender == "male":
        extend "his {nw}"
    else:
        extend "her {nw}"
    extend "face seemed very sad."
2. I''d like to add a different, simpler approach. Define the words that change according to the gender as variables themselves, and wrap them in []
I've seen this method in this forum, years ago.

Code: Select all

default he = "he"
default him =  "him"
default his = "his"
default is_ = "is"
default was = "was"
default has = "has"
default s = "s"
default es = "es"
default ies = "ies"
default self = "self"  # plural "selves"
# Capitalized....
default He = "He"
default Him = "Him"
default His = "His"

label start:
    menu:
        "Gender?"
        "Male":
            pass
        "Female":
            $ he, him, his = "she", "her", "her"
            $ He, Him, His = "She", "Her", "Her"
        "Plural":
            $ he, him, his = "they", "them", "their"
            $ He, Him, His = "They", "Them", "Their"
            $ is_, was, s, es = "are", "were", "", ""
            $ ies, has = "y", "have"

    "[He] [was] walking by the lonely street, [his] mood seemed very sad."
    "Jack look at [him] and said..."
    "[He] walk[s] to the bank, thinking about a cute dog [he] saw once."
    "The dog was fluffy and [he] like[s] fluffy dogs a lot."
    "[He] go[es] further and... cr[ies]. [He] do[es]n't know what to do."
    "[He] [has] started to run. [He] [has]n't understood."
Note that "is" is a python reserved word, so, you can use is_ to avoid problems. Remember to write your lines this way:

Code: Select all

"[He] [is_] walking by the street..."
Note also the variables "s" and "es" for the singular, empty in plural, and "ies" / "y".
Last edited by xavimat on Mon Aug 26, 2019 3:43 pm, edited 7 times in total.
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)

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Using conditional statements within dialogue

#8 Post by kivik »

xavimat wrote: Mon May 14, 2018 4:35 pm 2. I''d like to add a different, simpler approach. Define the words that change according to the gender as variables themselves, and wrap them in []
Note also the variables "s" and "es" for the singular, empty in plural.
That was my original thought which would be fine if he's only replacing pronouns and conjunctions, but he's got verbs to deal with as well which was when I stopped. I thought about creating an object with a __getattr__ function that has a list of non-standard verb spellings (e.g. cries, goes) and returns the correct variation when used: ("[vb.walk]") but it again relies on knowing all the verbs in advance during initialisation stage.

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

Re: Using conditional statements within dialogue

#9 Post by xavimat »

kivik wrote: Mon May 14, 2018 5:20 pm
xavimat wrote: Mon May 14, 2018 4:35 pm 2. I''d like to add a different, simpler approach. Define the words that change according to the gender as variables themselves, and wrap them in []
Note also the variables "s" and "es" for the singular, empty in plural.
That was my original thought which would be fine if he's only replacing pronouns and conjunctions, but he's got verbs to deal with as well which was when I stopped. I thought about creating an object with a __getattr__ function that has a list of non-standard verb spellings (e.g. cries, goes) and returns the correct variation when used: ("[vb.walk]") but it again relies on knowing all the verbs in advance during initialisation stage.
Maybe my lack of English knowledge is misleading me. But I think with the "s" and "es" variables we got lots of verbs covered.
We need is/are, was/were and probably has/have variants.
I would use your solution for more specific situations (as your e.g. "cries").
kivik wrote:but it again relies on knowing all the verbs in advance during initialisation stage.
Well, that shouldn't be a problem, the text of the VN is full written before releasing. Maybe your object could have a function to add verb variants on the go, so when the author is writing and a new verb appears that needs specific treatment, it simply writes: $ vb.add_verb("cries", "cry"). This way, there is no need to know all verbs in init phase.

But, Another solution would be to add another variable ies = "ies" (singular) ies = "y" (plural). How many other verbs are not covered? (looking here, it seems everything is covered now: http://www.grammar.cl/Present/Verbs_Third_Person.htm )

EDIT2: To form negative sentences: He doesn't / The don't.

Code: Select all

[He] do[es]n't know...
The great advantage of this method is that the author is writing actual sentences with meaning, and the only difference are the [].
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)

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Using conditional statements within dialogue

#10 Post by kivik »

xavimat wrote: Mon May 14, 2018 7:05 pm The great advantage of this method is that the author is writing actual sentences with meaning, and the only difference are the [].
Oh I definitely agree with that! And the few additional variables you suggest is very clever way of tackling the problem too. Long as author picks up the special cases then they should be fine!

In terms of knowing all the verbs - I guess I'm stuck in the mentality that our games are constantly in development, and Patreon has promoted the whole iterative release approach so I'd personally never know all the verbs in my game :P That's why I worried!

alee
Newbie
Posts: 7
Joined: Sat May 05, 2018 7:17 pm
Contact:

Re: Using conditional statements within dialogue

#11 Post by alee »

I ended up defining the pronouns at the start of the game for each character (he, his, him, he'd, he's, etc..) The game is still in writing mode, so I am just adding new pronouns as needed, as the author sends me new chapters! Then, when it comes to sentences that become complicated with the non-gender-specific pronouns (they, them, their, etc.), I am just using a standard if/else statement. It's a lot more code, and I'm a bit nervous it will introduce errors, but it's well within my programming capabilities.

Thanks everyone! I really appreciate everyone's thoughts and solutions on this! As always, this is a great message board. :)

Post Reply

Who is online

Users browsing this forum: No registered users