Different dialogues using "if" statements [SOLVED]

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
Green Skies
Regular
Posts: 53
Joined: Wed Jun 16, 2010 1:47 am
Projects: Shoot The Moon (GXB Otome)
Organization: Green Skies
Contact:

Different dialogues using "if" statements [SOLVED]

#1 Post by Green Skies » Tue Jul 20, 2010 12:52 am

Hello, we're making a dating sim-style game where you are given a menu screen each day with three areas you can go to. Depending on which area you choose to go to, you'll meet up with a guy who will say something new each day. I thought I could use "if" statements to control what the characters say, but this only works up to a point, for some reason.

Here is the code:

Code: Select all

label day2:
    $tajtalk_1 = False
    $tajtalk_2 = False
    $tajtalk_3 = False
    jump what_to_do
    
label what_to_do:
   
    "What should I do today?"
    menu: 
        "Go to the beach.":
            jump beach
        "Go to the café.":
            jump cafe
        "Stay at home.":
            jump home
            
label beach:   
    
    if tajtalk_1:
        jump ttalk2    
    if tajtalk_2:
        jump ttalk3
    if tajtalk_3:
        jump ttalk4
        
label ttalk1:
    
    "I see Taj standing on the beach next to his surfboard.{w=1.0} It looks like he just got out of the water."
    menu:
        "Call to him.":
            jump calltaj
        "Don't call to him.":
            jump nocalltaj
            
label calltaj:
    $surfer_love += 2
    pov "Taj!"
    su "'Sup, %(povname)s!"
    pov "You like to surf, huh?"
    su "Yeah, but today it's pretty soggy.{w=1.0} Hope it'll be better tomorrow."
    if tajtalk_1 == False:
        $tajtalk_1 = True
    jump dayend
    
label nocalltaj:
    "I'd better leave him alone."
    jump dayend

label ttalk2:
    
    su "Yo, come to check out the waves?"
    menu:
        "Pretend you've surfed before.":
            pov "Totally! I surf all the time."
            su "Yeah? What's your favorite wave?"
            pov "Well...uh..."
            su "You don't know...?{w=1.0} Huh, you sure you've surfed before?"
            "Well, that didn't go well."
            jump dayend
        "Say you haven't surfed.":
            $surfer_love +=2
            pov "I'm not much of a surfer, actually...{w=1.0} I've never tried."
            su "You kidding?"
            su "Aw, you gotta learn sometime.{w=1.0} It's righteous!"
            if tajtalk_2 ==False:
                $tajtalk_2 = True
            jump dayend
 
label ttalk3:
    
    pov "What's the matter?"
    su "I lent my surfwax to some dude and he hasn't brought it back yet."
    pov "He didn't...{w=0.5}take it for keeps, did he?"
    su "Nah...{w=1.0}Well, probably not."
    su "No biggie, though. Just bummed I can't surf right now."
    menu: 
        "Offer to chat instead.":
            jump goodalt
        "Offer to help look for the guy.":
            jump badsearch
            
label goodalt:
    $ surfer_love += 2
    pov "It's no fun worrying about it.{w=0.5} Want to talk instead?"
    su "Yeah, you're right. I got plenty left back at the hotel anyway."
    pov "Hotel?"
    su "Yeah, I don't live here. I'm visitin' for the summer like every other year."
    su "This beach is the best I've been."
    if tajtalk_3 ==False:
        $tajtalk_3 = True
    jump dayend
    
label badsearch:
    pov "I can help look for the guy."
    su "You don't have to do that!{w=0.5} No use sweatin' about it."
    pov "Oh..."
    jump dayend
    
label dayend:
    
    "What a day! Time to go to bed!"
    $ day_count +=1
    jump what_to_do
           
            
Basically, it works fine up till the label ttalk2. After choosing the right choice, it jumps to label dayend like it's supposed to. After going to the beach a third time, it repeats ttalk2.
There might be a ton of extra stuff because I was trying to fix the problem myself before revealing dialogues from the game...

If you can help me out, thank you very much! I'm still an extreme amateur at coding, so please keep it simple if you can.
Last edited by Green Skies on Wed Jul 21, 2010 2:45 am, edited 1 time in total.
Current Project: Shoot the Moon // Devblog

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Different dialogues using "if" statements

#2 Post by Jake » Tue Jul 20, 2010 4:31 am

Green Skies wrote:

Code: Select all

label beach:   
   
    if tajtalk_1:
        jump ttalk2   
    if tajtalk_2:
        jump ttalk3
    if tajtalk_3:
        jump ttalk4
Basically, it works fine up till the label ttalk2. After choosing the right choice, it jumps to label dayend like it's supposed to. After going to the beach a third time, it repeats ttalk2.
Your main problem is in the snippet of code quoted above; Ren'Py will execute all of those statements in order, starting from the top... so the first thing it does it check 'tajtalk_1', and if that's true, it'll jump to 'ttalk2' regardless of what all the other variable values are. So because you've finished the first day's dialogue successfully it'll skip straight to 'ttalk2' without even thinking to check any of the other days.

There's two ways to get around this. The more-awkward way is to just reverse the order of your checks - check for 'tajtalk_3' first, then 'tajtalk_2', then 'tajtalk_1' - like this, you'll always get the latest bit of dialogue, so long as you unlock those variables in that order.
The slightly-less-cumbersome way, that's easier to read and easier to add new items to, is to just start using a number instead of a boolean True/False value, and just have one variable:

Code: Select all

label start:
  $ tajtalk = 0

label beach:
  if tajtalk == 0:
    jump ttalk1
  if tajtalk == 1:
    jump ttalk2
  if tajtalk == 2:
    jump ttalk3

label ttalk1:
  # ... dialogue
  tajtalk = 1

  jump beach

label ttalk2:
  # ... dialogue
  tajtalk = 2

  jump beach
and so on. Like that you can add your checks in whatever order you like, so it's easier to read and you don't have to be careful when you're adding new ones.



If you feel ambitious programming-wise, you should also consider the Dating Sim Engine, which is specifically for this kind of game. If you need more-complex sets of events and conditions, it's probably a good idea to at least look at it.



(As another note: there's no need to do this:

Code: Select all

  if tajtalk == False:
    tajtalk = True
- if you want the value to be True, then just set it to True, there's no need to check for False beforehand... either way the end result is that the variable has a value of 'True', and it shouldn't even be the case that you get to that line of code and the var is True already anyway.)
Server error: user 'Jake' not found

Green Skies
Regular
Posts: 53
Joined: Wed Jun 16, 2010 1:47 am
Projects: Shoot The Moon (GXB Otome)
Organization: Green Skies
Contact:

Re: Different dialogues using "if" statements

#3 Post by Green Skies » Wed Jul 21, 2010 2:44 am

Oooh I see! You're so helpful, Jake! You've made it much easier for me. Thank you :)
Current Project: Shoot the Moon // Devblog

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]