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
Nafai
Veteran
Posts: 290
Joined: Thu Jun 29, 2006 2:10 pm
Projects: Elect: Ascendance
Location: Philippines
Contact:

How to do Comparisons Between Variables?

#1 Post by Nafai »

Hi guys,

Still working on the Elect script, and trying to integrate a bit of code as I go along. One of the things I want the game to be able to keep track of is which of the girls likes you best, which of the girls like you second best and so on. I'll be using the point system, and while I can use that to track the girl's individual affection ratings, I don't know of any way to compare and rank their affection ratings. I'd like to do that for certain jealousy-events (Catfights anyone?) so if anyone can clue me in, that'd be much appreciated.

Thanks in advance guys!
Image

User avatar
Deji
Cheer Idol; Not Great at Secret Identities
Posts: 1592
Joined: Sat Oct 20, 2007 7:38 pm
Projects: http://bit.ly/2lieZsA
Organization: Sakevisual, Apple Cider, Mystery Parfait
Tumblr: DejiNyucu
Deviantart: DejiNyucu
Location: Chile
Contact:

Re: How to do Comparisons Between Variables?

#2 Post by Deji »

To sort a number of variables you need to make an iteration inside another iteration.

My basic idea is to make an Array with all the variables.
I made a two dimensional Array, where each element of the main array has two components: the girl's name and a numeric value.

[a,3][b,5][c,6][d,2]

I compare the first element's numeric value with each of the other elements' in order. If I find a higher numeric value, I swap it with the current first element.

at the end of this cycle, my array should look like this:

[c,6][a,3][b,5][d,2]

Now that I know the highest value, I need to know the second, so I leave the first out and compare all the elements with the second value and swap them if I find a higher value, the same way I did with the first one.

Then I repeat with the third value and so on....

In the end, it looks like this:

[c,6][b,5][a,3][d,2]

Then you just need to access the first component of the first element to know the name of the girl with higher affection.

I have no idea how to code this in Ren'Py, thou =X
I tested it in Flash with ActionScript 2.0 and it works... I would post that code, but it'd be confusing ^^;

Hope this helps >_o;
Image
Tumblr | Twitter
Forever busy :')
When drawing something, anything, USE REFERENCES!! Use your Google-fu!
Don't trust your memory, and don't blindly trust what others teach you either.
Research, observation, analysis, experimentation and practice are the key! (:

chronoluminaire
Eileen-Class Veteran
Posts: 1153
Joined: Mon Jul 07, 2003 4:57 pm
Completed: Elven Relations, Cloud Fairy, When I Rule The World
Tumblr: alextfish
Skype: alextfish
Location: Cambridge, UK
Contact:

Re: How to do Comparisons Between Variables?

#3 Post by chronoluminaire »

While what Deji says is true, it may be overkill for your needs, depending on how many girls you've got.

ElvenRelations had several lines that went something like:

Code: Select all

if AsilanaOpinion >= TohkoOpinion and AsilanaOpinion >= YurikaOpinion:
  # Asilana section
elif YurikaOpinion >= AsilanaOpinion and YurikaOpinion >= TohkoOpinion
  # Yurika section
else
  # Tohko section
I released 3 VNs, many moons ago: Elven Relations (IntRenAiMo 2007), When I Rule The World (NaNoRenO 2005), and Cloud Fairy (the Cute Light & Fluffy Project, 2009).
More recently I designed the board game Steam Works (published in 2015), available from a local gaming store near you!

Sin
Veteran
Posts: 298
Joined: Thu Oct 18, 2007 3:43 am
Contact:

Re: How to do Comparisons Between Variables?

#4 Post by Sin »

You could also try "bubble sort". It's a sorting algorithm that's really easy to program.

1. Compare the first and second value in the list. If the latter is larger, swap them with eachother.
2. Compare the second and third value. If the latter is larger, swap them.
3. Continue doing this for all items in the list.
4. Repeat step 1-3 until you don't have to do any swapping.

It's not the fastest algorithm but it's better than the brute force approach, although I suspect it's still a bit overkill for what you're trying to do. :p (Maybe someone else could find it useful).

It's called bubble sort because the larger values will "bubble" up to the top of the list over each iteration.
Using Deji's example: [a,3][b,5][c,6][d,2]
Iteration 1: [b,5][c,6][a,3][d,2]
Iteration 2: [c,6][b,5][a,3][d,2]

User avatar
PyTom
Ren'Py Creator
Posts: 16088
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?

#5 Post by PyTom »

Hi. I'm from the CS police, and I'm here to revoke your CS license for suggesting the use of bubble sort. Heck, insertion sort is better than bubble sort, we'd just fine you for that. :-)

Probably the best thing to use would be python's built-in sort, which uses an algorithm called timsort. (It's a clever variant of mergesort that tries to keep sorted sub-lists together, allowing it to work in O(n) when the list is already sorted.)

I'd probably build a list of (score, label) tuples, sort it using the .sort() method, and then take the second component of the first tuple.

Code: Select all

python:
    scores = [ 
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

jump expression scores[0][1]
Note that this will use the label, in alphabetical order, as the tiebreaker. (So, Eileen would beat Lucy, leading to another Mugenjohncel game.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Deji
Cheer Idol; Not Great at Secret Identities
Posts: 1592
Joined: Sat Oct 20, 2007 7:38 pm
Projects: http://bit.ly/2lieZsA
Organization: Sakevisual, Apple Cider, Mystery Parfait
Tumblr: DejiNyucu
Deviantart: DejiNyucu
Location: Chile
Contact:

Re: How to do Comparisons Between Variables?

#6 Post by Deji »

PyTom wrote:Hi. I'm from the CS police, and I'm here to revoke your CS license for suggesting the use of bubble sort. Heck, insertion sort is better than bubble sort, we'd just fine you for that. :-)

Probably the best thing to use would be python's built-in sort, which uses an algorithm called timsort. (It's a clever variant of mergesort that tries to keep sorted sub-lists together, allowing it to work in O(n) when the list is already sorted.)

I'd probably build a list of (score, label) tuples, sort it using the .sort() method, and then take the second component of the first tuple.

Code: Select all

python:
    scores = [ 
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

jump expression scores[0][1]
Note that this will use the label, in alphabetical order, as the tiebreaker. (So, Eileen would beat Lucy, leading to another Mugenjohncel game.)

I figured there should be a method of sorting in Python, just like in AS!

Thanks for the enlightment!~
*bookmarks topic*
Image
Tumblr | Twitter
Forever busy :')
When drawing something, anything, USE REFERENCES!! Use your Google-fu!
Don't trust your memory, and don't blindly trust what others teach you either.
Research, observation, analysis, experimentation and practice are the key! (:

Nafai
Veteran
Posts: 290
Joined: Thu Jun 29, 2006 2:10 pm
Projects: Elect: Ascendance
Location: Philippines
Contact:

Re: How to do Comparisons Between Variables?

#7 Post by Nafai »

*watches programming language go flying over his head*

Hehe, thanks so much for the help Deji, chrono, Pytom!

Being totally non-versed in the language of programming though, I have a few questions:

* I could use the elif expression more than once right?

* What does "jump expression scores[0][1]" do exactly? How would I be able to integrate this into an "if Girl A is #1 and Girl B is #2 then...; if Girl B is #1 and Girl C is #2 then..." operation?
Image

Sin
Veteran
Posts: 298
Joined: Thu Oct 18, 2007 3:43 am
Contact:

Re: How to do Comparisons Between Variables?

#8 Post by Sin »

Crap! How will I finish Novelty now without my CS license? D:

User avatar
Deji
Cheer Idol; Not Great at Secret Identities
Posts: 1592
Joined: Sat Oct 20, 2007 7:38 pm
Projects: http://bit.ly/2lieZsA
Organization: Sakevisual, Apple Cider, Mystery Parfait
Tumblr: DejiNyucu
Deviantart: DejiNyucu
Location: Chile
Contact:

Re: How to do Comparisons Between Variables?

#9 Post by Deji »

Nafai wrote:
* I could use the elif expression more than once right?

* What does "jump expression scores[0][1]" do exactly? How would I be able to integrate this into an "if Girl A is #1 and Girl B is #2 then...; if Girl B is #1 and Girl C is #2 then..." operation?
1. Yes, you can use elif as much as you want, like in the last example here:
http://www.renpy.org/wiki/renpy/doc/coo ... ed_Endings

2. It basically translates to:
"jump to the second component of the first element in the array", being the second element a label name.
in PyTom's example, since Eileen is the first element (eileen_score, "eileen_ending"), it jumps to the label "eileen ending" (second element of the tuple)

To transform this into what you want for comparision, I think you can do this (taking PyTom's example):

Code: Select all

python:
    scores = [
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

if expression scores[0][0] == eileen_score:
    if expression scores[1][0] == lucy_score:
        jump eileen_vs_lucy
    elif expression scores[1][0] == mary_score:
        jump eileen_vs_mary
elif expression scores[0][0] == lucy_score:
    if expression scores[1][0] == elieen_score:
        jump lucy_vs_elieen
    elif expression scores[1][0] == mary_score:
        jump lucy_vs_mary
elif expression scores[0][0] == mary_score:
   if expression scores[1][0] == elieen_score:
        jump mary_vs_elieen
   elif expression scores[1][0] == lucy_score:
        jump mary_vs_lucy
(I'm not sure if you have to keep the "expression" part, thou... I'm just puttting it there to be safe!)

It tells the program to check if the first component of the first tuple is x and the frist component of the second tuple is y, and then jum to the right label accordingly.
Image
Tumblr | Twitter
Forever busy :')
When drawing something, anything, USE REFERENCES!! Use your Google-fu!
Don't trust your memory, and don't blindly trust what others teach you either.
Research, observation, analysis, experimentation and practice are the key! (:

mozillauser
Newbie
Posts: 16
Joined: Mon Jun 30, 2008 4:00 pm
Contact:

Re: How to do Comparisons Between Variables?

#10 Post by mozillauser »

Deji wrote:(I'm not sure if you have to keep the "expression" part, thou... I'm just puttting it there to be safe!)
Well, I'm not very experienced in phyton, but in my opinion the expression is not required for this case. Look at PyToms example:
PyTom wrote:jump expression scores[0][1]
I guess the syntax of the jump statement in phyton is: jump <label>. Therefore the script should contain a statement like: jump eileen_ending (or $ jump eileen_ending if it is located outside a phyton block).
But PyTom wants to jump to a label, that has the same name like the result of: scores[0][1]. Therefore the phyton processor first has to evaluate the result of scores[0][1] and then take this result as an argument for the jump statement.
But how to define that? The solution is the expression. The expression makes the phyton processor to evaluate the expression and embed the result in the jump statement. If the syntax is satisfied, the jump statement is executed as if the programmer coded the statement jump eileen_ending.

Deji, your code snippet needs no expression, because the if-else syntax already expects an expression.
Hmmm, sounds funny but that's just the way it is.

(Maybe there is someone out there, who wants to correct this posting, because he (or she) is either a phyton expert or speaks much better english than me :wink: )

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?

#11 Post by Gau_Veldt »

Python's got nice recursion how about quicksort?

User avatar
PyTom
Ren'Py Creator
Posts: 16088
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?

#12 Post by PyTom »

Generally, mergesorts (of which timsort is one) perform better than quicksorts assuming that you have at least n/2 scratch space. Quicksort is only expected O(n lg n) and can get as bad as O(n^2) if you pick the pivot poorly, while Mergesort is guaranteed O(n lg n).

Timsort is an improved mergesort that takes advantage of lists that are already partially sorted, so in the case where the list is already sorted (or you merge 2 sorted lists), it winds up as O(n).

Not to mention, python's built-in sort is written in C, which is much faster than python proper, so you'd win from that as well.

This is 2008. Outside of a CS class, there's no reason to implement your own sorting algorithms anymore.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

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?

#13 Post by Watercolorheart »

Wha ... what is this notation?

[letter, number][letter, number] and what does it mean? Should I look up Arrays in the cookbook?

I have never seen this before.
I'm not even the same person anymore

User avatar
PyTom
Ren'Py Creator
Posts: 16088
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?

#14 Post by PyTom »

Which notation are you talking about, precisely? Big-O notation is a CS concept, while arrays are discussed in the python tutorial.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

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

Re: How to do Comparisons Between Variables?

#15 Post by cloverfirefly »

(Hi, first post.)

I tried PyTom's sample code to make a simple "game" and the following is my code.

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[0][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
Unfortunately, when I run my game, Eileen (with a score of 1) likes me the most! I would imagine the reason for this is that sort is going by ascending order. Is there a way to make sort go by descending order so that the girl with the highest score (in this case Lucy and her score of 3) would be the one that "wins"?
future aspect my current work
my tumblr for art and visual novel development updates

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Kocker