[Solved] Showing certain dialogues depending if the flag is triggered

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
Fabio101
Newbie
Posts: 2
Joined: Tue Feb 06, 2018 6:28 am
Contact:

[Solved] Showing certain dialogues depending if the flag is triggered

#1 Post by Fabio101 »

Complete Renpy novice here. Basically, in my current game I have something that goes along like this:

Code: Select all

menu ask_witnesses:

    "Question1":
        $ question1 = True
            jump question1

    "Question2":
        $ question2 = True
            jump question2

    "Question3"
        $ question3 = True
            jump question2
    
    "Decision" if question1 = True and question2 = True and question3 = True:
        jump decision
Simple right?

What I want to implement is use those flags that will affect the dialogue in those questions. In my brain, it goes something like below and I don't know how to make it work.

Code: Select all

if question1 = True and question2 = False and question3 = False: (basically any combination of 1 true and 2 false flags)
        
        detective "Hmm... suspicious"

if question1 = True and question2 = True and question3 = False:
if question1 = True and question2 = False and question3 = True:

        detective "Hmm... really suspicious"

if question1 = True and question2 = True and question3 = True:

        detective "he's the culprit!"
That's what I want to implement above but for some reason, I don't know how to make it work. I will be pasting the code in all 3 questions as the detective's comment and I want it to flow seamlessly without a menu or something and searching it seems "impossible" (I just don't know what to write exactly the problem).

Thanks for the help.
Last edited by Fabio101 on Fri May 18, 2018 3:27 pm, edited 1 time in total.

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

Re: Showing certain dialogues depending if the flag is triggered

#2 Post by kivik »

Hey, welcome to the forum!

First of all, it's really important that you differentiate these two operations:

Code: Select all

question1 = True
question1 == True
The first one with a single = sign assigns whatever's on the right to whatever's on the left of the operator.
The second one with two = signs is a comparison, it returns True or False depending on whether the statement evaluates to True. So your line:

Code: Select all

"Decision" if question1 = True and question2 = True and question3 = True:
needs to be

Code: Select all

"Decision" if question1 == True and question2 == True and question3 == True:

With that out of the way we can do the more advanced stuff!

Another cool feature of comparison operators, is that they do this:

Code: Select all

$ a = False
$ b = True
if a:
    "a evaluated to True"
if b:
    "b evaluated to True"
Output would be "b evaluated to True"

if variable_name is a shorthand for evaluating variables, it's the same as if variable_name == True - if the variable_name is a boolean value (True or False).

But what if it's not a boolean?

Code: Select all

python:
    a = 0
    b = 1
    c = 2
    d = ""
    e = "Hello world!"
if a:
    "a evaluated to True"
if b:
    "b evaluated to True"
if c:
    "c evaluated to True"
if d:
    "d evaluated to True"
if e:
    "e evaluated to True"
This will output b, c and e evaluated to True.

Now I've never asked what's really happening but I know it's what happens, so my way of understanding it is: if you put a variable on its own in an if statement, depending on the type of variable it is, it'll do different evaluations. So the above would be equivalent to:

Code: Select all

if a != 0:
    "a evaluated to True"
if b != 0:
    "b evaluated to True"
if c != 0:
    "c evaluated to True"
if d != "":
    "d evaluated to True"
if e != "":
    "e evaluated to True"
For numbers, 0 returns False, everything else returns True (including negatives); strings, "" returns False, everything else returns True.

Why did I teach you this? Because we can use it to simplify your code. We'll now set the variables to 1:

Code: Select all

menu ask_witnesses:

    "Question1":
        $ question1 = 1
            jump question1

    "Question2":
        $ question2 = 1
            jump question2

    "Question3"
        $ question3 = 1
            jump question2

Then we can do this:

Code: Select all

if question1 and not (question2 + question3):
        detective "Hmm... suspicious"

if question1 and (question2 + question3) == 1:
        detective "Hmm... really suspicious"

if question1 + question2 + question3 == 3:
        detective "he's the culprit!"
You can work out the variations and the corresponding maths by adding your digits together. But also you can do something like this:

Code: Select all

# if question1 is True and any one or more of the other questions is True:

if question1 and question2+question3+question4:
Hope that's informative and will be helpful in the future!

Fabio101
Newbie
Posts: 2
Joined: Tue Feb 06, 2018 6:28 am
Contact:

Re: Showing certain dialogues depending if the flag is triggered

#3 Post by Fabio101 »

Oh my God! It works! Hahaha!

Thank you very much kivik! The tutorial you wrote opened a lot of for me regarding the variable side of things! I'm still learning little by little but this is definitely an eye opener for me regarding its use! I'll stick around the forums!

Kudos!

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

Re: [Solved] Showing certain dialogues depending if the flag is triggered

#4 Post by kivik »

No problems, I hope it made sense above! I realised I didn't actually explain the final code, so if you have questions do let me know. Variables and conditions can be confusing to start with, but once you've got the basic grasp of it, you can simplify a lot of your code easily. Sometimes it's helpful to just read out loud what you're trying to evaluate and suddenly a simple solution emerges.

When you move to evaluating None, lists, objects, functions, that's when it gets a whole lot more interesting!

Post Reply

Who is online

Users browsing this forum: geoWaffle