Long lines of condition evaluation. Any trick to shorten those?

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
DrGonzo
Regular
Posts: 46
Joined: Fri Feb 23, 2018 9:12 pm
Location: Los Angeles, California
Contact:

Long lines of condition evaluation. Any trick to shorten those?

#1 Post by DrGonzo »

Hi,
I'm coding away on my VN and some of the IF / ELIF lines of code becoming longer and longer. Depending on the complexity of an event or interaction.

Code: Select all

    if ......
    elif janitor_door_now_unlocked == False and class_b_tried_to_open == True and day >= class_b_eventcontinues_day and dtime >= class_b_eventcontinues_dtime and  class_b_event_progress == 1:
        jump janitor_unlocking_the_classroom_door
    else:
        ......    
Is there an elegant way to shorten this somehow, or to move the variable comparison somewhere else, so that the main code doesn't become so cluttered?
Maybe using a function call that returns a True or False to the IF statement?
Just using shorter variable names and labels isn't really an option, since the code needs to remain readable.

Thanks,
Kay
Google search: "renpy thisismyquestion -org"
This gives you everything - except renpy.org results. Which I find useless 90% of the time.

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

Re: Long lines of condition evaluation. Any trick to shorten those?

#2 Post by Alex »

At least you can change

Code: Select all

if some var == True
to

Code: Select all

if some_var
and

Code: Select all

if some_var == False
to

Code: Select all

if not some_var

User avatar
DrGonzo
Regular
Posts: 46
Joined: Fri Feb 23, 2018 9:12 pm
Location: Los Angeles, California
Contact:

Re: Long lines of condition evaluation. Any trick to shorten those?

#3 Post by DrGonzo »

Thanks Alex!
That will cut down on the length of a line.
Google search: "renpy thisismyquestion -org"
This gives you everything - except renpy.org results. Which I find useless 90% of the time.

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

Re: Long lines of condition evaluation. Any trick to shorten those?

#4 Post by philat »

Depends on what your conditions are, I guess. If you have non-boolean variables that need to be checked for equivalency, you can use a list. (if [var1, var2] == [1, 2] instead of if var1==1 and if var2==2, which is slightly shorter.) If you have a bunch of variables that are always going to be present, you could write a function, but it's unclear how much is generalizable based on what you've given.

User avatar
DrGonzo
Regular
Posts: 46
Joined: Fri Feb 23, 2018 9:12 pm
Location: Los Angeles, California
Contact:

Re: Long lines of condition evaluation. Any trick to shorten those?

#5 Post by DrGonzo »

Thanks for the tip philat, I'll try it out.
I am working on a sandbox game rather than a VN, so there is a lot of checking if a PC or NPC is at a certain location at a certain time, meeting a certain condition.
The variables are all True, False and numerical. As to be expected. Different vars for different NPCs.

I guess, at the end of the day there really is no elegant way to code complex game flows. Except having very short vars and a decoding table near by to know what they stand for.
But at least I figured out that I can use a single variable to keep track of the progress in a particular story branch. By increasing that var at every story node, there is a lot less comparing of individual vars needed.
Google search: "renpy thisismyquestion -org"
This gives you everything - except renpy.org results. Which I find useless 90% of the time.

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

Re: Long lines of condition evaluation. Any trick to shorten those?

#6 Post by philat »

Well, what I meant was like this part: day >= class_b_eventcontinues_day and dtime >= class_b_eventcontinues_dtime and class_b_event_progress == 1

I don't know, since I don't know what the rest of your game looks like, whether this is something that would be collapsible into something like daytimeprogress("class_b") or daytimeprogress("class_a"), but it seems possible. Can't give concrete pointers without more information, but it may be something to explore.

Post Reply

Who is online

Users browsing this forum: No registered users