If/Else Statements Question

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
User avatar
AvalonMelody
Regular
Posts: 33
Joined: Thu May 29, 2014 2:14 am
Contact:

If/Else Statements Question

#1 Post by AvalonMelody » Mon May 23, 2016 4:24 pm

Hello! I've got a question on If/Else statements that I hope someone could clear up for me.
To start off, I do have a general understanding of if/else statements, along the lines of "if ___ is true, ___ will happen" and I've recently found out you can have just an "if" without an "else" or "else if".

My question is concerning adding dialogue to main dialogue based on true/false variables or by number of ___ (example, if charApoints = 4).
However, it's not so much asking how to do this rather than why some do the following to accomplish this. (Though, if you have any alternative ways of doing this, I'm all ears! I love learning new things. ^^ )

I've seen a few scripts where dialogue (for example) goes a bit like the following:
NOTE: In these examples, only one of the "custom dialogues" are to show up in the script. So either varA, varB, or varC will be true when this loads.

e "Main dialogue!"

if varA == True:
e "custom dialogue A"

if varB == True:
e "custom dialogue B"

if varC == True:
e "custom dialogue C"

e "Continuation of main dialogue"


I understand this would work just fine to accomplish the above question, but I'm curious as to why they don't do something like this:

e "Main dialogue!"

if varA == True:
e "custom dialogue A"

elif varB == True:
e "custom dialogue B"

elif varC == True:
e "custom dialogue C"

else:
pass

e "Continuation of main dialogue"


Which, I'd assume would have the same result of adding an extra line of dialogue? (Since again, only one of the variables would be true at the time this section was run.)
So, is it all about personal preference or using fewer lines or is it more efficient programming-wise somehow to use one over the other? (Though, perhaps I just fail at understanding this and I'm overthinking everything. @_@")

I hope the question makes sense!
Thanks for your time,
AvalonMelody

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: If/Else Statements Question

#2 Post by PyTom » Mon May 23, 2016 10:28 pm

The if/elif version is preferred. It's not for efficiency, which doesn't matter much for this sort of code. But the nice thing about if-elif is that I can be 100% sure that only one of the three choices will run. With only if statements, we don't know that - we have to read the rest of your code to figure out what might happen.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: If/Else Statements Question

#3 Post by Onishion » Tue May 24, 2016 7:06 am

Yup. Btw, "else: pass" is completely unnecessary. It literally does nothing here, you can just leave that bit out. You only need an "else" at the end if you want something to happen when "checks A, B, and C all failed, but you still want something to happen IF all of them fail, that doesn't happen if any of them succeed."

The reason you want to use elif is like if you do:

if Var > 10:
"do this"
if Var > 5:
"do this instead"

then if you're using ifs there, then both will happen if the number is over 10, while if you use elif on the second one then it will only ever hit that one if the number is over 5 but also under 10. It's all about setting up the sequence of conditions that do the thing you want done and nothing unexpected.

User avatar
AvalonMelody
Regular
Posts: 33
Joined: Thu May 29, 2014 2:14 am
Contact:

Re: If/Else Statements Question

#4 Post by AvalonMelody » Tue May 24, 2016 7:43 am

Ahh, I see! That makes sense~
I suppose if the custome lines were just one or two, something lie making a comment on "You have a very large orange in your bag" as opposed to "you have a big grapefruit in your bag" between the main dialogue (unless you have grapes, in which case nothing out of the ordinary is said.) Such dialogue isn't 100% necessary so it's no loss if it were skipped over by some error, then in which case it might be easier to use just if statements? I suppose if you can afford some leniency. Altough anything missed is still missed.

Thanks also for pointing out the redundancy of the pass! That makes sense, I can't believe I thought it was necessary here. Oops.

I'll be sticking to using if/elif as it stands. A few extra lines doesn't hurt.

Thanks so much for your replies!

Post Reply

Who is online

Users browsing this forum: Alex, nyeowmi