Ren'Py Coding question

Korentin

Member
Game Developer
May 20, 2019
152
319
Hullo!

I'm researching the "best" approach to coding a way to display dialogue and scenes depending on the top value of three different variables assigned to a character in my game.

Example:

The main character can have different values in A, B and C. Depending on what the player does during the game these can increase to different values.

I want to be able to display lines and scenes depending on which of these three variables are the highest (and if they are equal).

If A is 2, B is 1 and C is 3, anyone knows an efficient way to do this so that the character displays the text associated with C and not A or B? I'd also like to know a way to handle this if all three values are equal.

Hopefully, that made sense to somebody :)
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,581
2,219
I can see one way

Python:
default a = 0
default b = 0
default c = 0
default temp1 = 0

label start:

    "*** START ***"

    $ a = 2
    $ b = 1
    $ c = 3

    $ temp1 = max(a, b, c)

    if temp1 == a:
        "Displaying path A"
    elif  temp1 == b:
        "Displaying path B"
    else:
        "Displaying Path C"

    "***THE END***"

    return
I'm sure there might be neater/quicker ways of doing it... But I'm pretty sure it'll work how you want.

Edit: In the case they are were all equal, it would pick "a". If B and C were equal, but larger than A, it would pick "b". It would only ever pick 1 outcome. (It could be changed to do multiple outcomes, if you'd prefer that).
If you wanted to reverse the order to be C > B > A, just change if temp1 == a: to if temp1 == c: and change the outcome to match.

Edit:2 typo.
 
Last edited:

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,490
7,035
If A is 2, B is 1 and C is 3, anyone knows an efficient way to do this so that the character displays the text associated with C and not A or B? I'd also like to know a way to handle this if all three values are equal.

Hopefully, that made sense to somebody :)
What 79flavors showed you (aside from the typo on the second line where it should be "default b" instead of "default c" is the most basic approach. But only you can decide what to do if all three values are equal, or if the top two are equal, and that will affect your logic. 79flavors code, as he says, will take path A if A is the highest or tied for the highest, then take B if IT is the highest or tied for the highest, and will only take C if it is the highest all by itself.

There are other ways that this can be attacked using lookup tables and things like that which might save a few keystrokes, but none of them are as obvious and easy-to-understand as the above.
 

Korentin

Member
Game Developer
May 20, 2019
152
319
I can see one way

Python:
default a = 0
default b = 0
default c = 0
default temp1 = 0

label start:

    "*** START ***"

    $ a = 2
    $ b = 1
    $ c = 3

    $ temp1 = max(a, b, c)

    if temp1 == a:
        "Displaying path A"
    elif  temp1 == b:
        "Displaying path B"
    else:
        "Displaying Path C"

    "***THE END***"

    return
I'm sure there might be neater/quicker ways of doing it... But I'm pretty sure it'll work how you want.

Edit: In the case they are were all equal, it would pick "a". If B and C were equal, but larger than A, it would pick "b". It would only ever pick 1 outcome. (It could be changed to do multiple outcomes, if you'd prefer that).
If you wanted to reverse the order to be C > B > A, just change if temp1 == a: to if temp1 == c: and change the outcome to match.

Edit:2 typo.
Thank you! This should work splendidly.

I was entertaining the notion of having the result of all three being equal have a unique (a path D if you will) result.

But for various reasons, I think I much prefer having it defaulting to one of the existing attributes.

I'll experiment with this a bit and see how it works out in practice.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,581
2,219
I was entertaining the notion of having the result of all three being equal have a unique (a path D if you will) result.

As in all things... that's possible. I'd probably code something like:

Python:
default a = 0
default b = 0
default c = 0
default temp1 = 0

label start:

    "*** START ***"

    $ a = 2
    $ b = 1
    $ c = 3

    $ temp1 = max(a, b, c)

    if temp1 == min(a, b, c):
        "Displaying path ORGY (D)"
    elif temp1 == a:
        "Displaying path A"
    elif  temp1 == b:
        "Displaying path B"
    else:
        "Displaying Path C"

    "***THE END***"

    return

... but only because I've got this whole max(), min() thing going on in my head already.

This works too and honestly, is probably easier to read. (I just try not to use and statements if I can avoid them).

Python:
    if a == b and b == c:

But either would produce the "Path D" option you mentioned.
 
  • Like
Reactions: Korentin

Korentin

Member
Game Developer
May 20, 2019
152
319
As in all things... that's possible. I'd probably code something like:

Python:
default a = 0
default b = 0
default c = 0
default temp1 = 0

label start:

    "*** START ***"

    $ a = 2
    $ b = 1
    $ c = 3

    $ temp1 = max(a, b, c)

    if temp1 == min(a, b, c):
        "Displaying path ORGY (D)"
    elif temp1 == a:
        "Displaying path A"
    elif  temp1 == b:
        "Displaying path B"
    else:
        "Displaying Path C"

    "***THE END***"

    return

... but only because I've got this whole max(), min() thing going on in my head already.

This works too and honestly, is probably easier to read. (I just try not to use and statements if I can avoid them).

Python:
    if a == b and b == c:

But either would produce the "Path D" option you mentioned.
Great, might be useful down the line!