How to do Comparisons Between Variables?

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.
Message
Author
User avatar
EusthEnoptEron
Newbie
Posts: 21
Joined: Thu Jul 10, 2008 6:27 am
Location: Switzerland
Contact:

Re: How to do Comparisons Between Variables?

#16 Post by EusthEnoptEron » Fri Jul 18, 2008 3:38 am

You could use the .reverse() method of the List object after you've sorted it, I guess.
Real programmers don't comment their code - it was hard to write, it should be hard to understand.

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: How to do Comparisons Between Variables?

#17 Post by PyTom » Fri Jul 18, 2008 10:15 am

You want to do:

jump expression scores[-1][1]

The -1 refers to the last item in the scores list. Sorry about forgetting this in my original post... about 90% of the time when I sort, I want the smallest rather than the largest number.
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

User avatar
cloverfirefly
Newbie
Posts: 22
Joined: Wed Jun 25, 2008 1:59 am
Completed: The Curious Alliance
Contact:

Re: How to do Comparisons Between Variables?

#18 Post by cloverfirefly » Sat Jul 19, 2008 3:45 am

Thanks, PyTom. Problem solved. :)

For anyone following along, here is the fixed version of the sample game I posted earlier.

Code: Select all


label start:
    
    $ mary_score = 2
    $ eileen_score = 1
    $ lucy_score = 3
    
    "Eileen's score is %(eileen_score)s."
    "Lucy's score is %(lucy_score)s."
    "Mary's score is %(mary_score)s."
    
    python:
        scores = [
            (eileen_score, "eileen_ending"),
            (lucy_score, "lucy_ending"),
            (mary_score, "mary_ending"),
            ]

        scores.sort()

    jump expression scores[-1][1]
    
label eileen_ending:
    "Eileen likes you the most!"
    return
    
label lucy_ending:
    "Lucy likes you the most!"
    return
    
label mary_ending:
    "Mary likes you the most!"
    return

future aspect my current work
my tumblr for art and visual novel development updates

Watercolorheart
Eileen-Class Veteran
Posts: 1314
Joined: Mon Sep 19, 2005 2:15 am
Completed: Controlled Chaos / Sum of the Parts / "that" Midna game with ZONEsama
Projects: Sparse Series/Oddments Shop original cartoon in Pevrea; Cybernetic Duels (fighting game); Good Vibin'
Organization: Watercolorheart Studios
IRC Nick: BCS
Tumblr: adminwatercolor
Deviantart: itsmywatercolorheart
Github: Watercolordevdev
Skype: heartnotes
Soundcloud: Watercollider
itch: watercolorheart
Location: Florida
Contact:

Re: How to do Comparisons Between Variables?

#19 Post by Watercolorheart » Sun Jul 20, 2008 6:41 pm

What does this part do :?:
scores = [
(eileen_score, "eileen_ending"),
(lucy_score, "lucy_ending"),
(mary_score, "mary_ending"),
]

Is it connecting the name of the variable to the jump?
I'm not even the same person anymore

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: How to do Comparisons Between Variables?

#20 Post by PyTom » Sun Jul 20, 2008 6:58 pm

It's actually connecting the value of the variable to the jump. Basically, what we do is:

1) Create a list of score-label pairs.
2) Sort that list.
3) Jump to the label of the last thing in the list, which is the one with the highest score.

Being able to do this sort of thing is why it's important to include a full-fledged programming language as part of your visual novel engine. While you could do this for a few labels with simple compare-jumps, that can become unwieldy fast, while this approach scales easily.
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

User avatar
Gau_Veldt
Regular
Posts: 86
Joined: Tue Jun 10, 2008 8:22 pm
Location: Prince George, BC
Contact:

Re: How to do Comparisons Between Variables?

#21 Post by Gau_Veldt » Sun Jul 20, 2008 11:23 pm

BCS wrote:What does this part do :?:
scores = [
(eileen_score, "eileen_ending"),
(lucy_score, "lucy_ending"),
(mary_score, "mary_ending"),
]

Is it connecting the name of the variable to the jump?
This list uses tuples to connect the scores to the ending to call if that score wins. The winning score is determined by sorting the list which orders it by score, with the highest score being at the end of the list. A list like this works well when there are a lot of characters.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], _ticlock_