Ren'Py Structuring code in Ren'Py

__b

Member
Mar 4, 2020
181
2,322
Python:
label start:
    scene default_background
    with fade

    show livia_neutral

    $ loop = True
    while loop:
        menu:
            "What should I do?"

            "Make her happy":
                hide livia_neutral
                show livia_happy
                window hide
                pause
                hide livia_happy

            "Make her angry":
                hide livia_neutral
                show livia_angry
                window hide
                pause
                hide livia_angry

            "Exit":
                $ loop = False
            
        show livia_neutral

    return
How to remove the repeating code in a nice way? I wanted to create another label with the repeating code in it, and call it with the image livia_x, but that didn't work for some reason.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,969
16,222
How to remove the repeating code in a nice way?
By understanding how Ren'Py works ; what is only possible if you starts by reading the documentation.


" "

" "

Therefore, "livia_neutral" and "livia_happy" should be named "livia neutral" and "livia happy".
That way, show livia happy would automatically hide "livia neutral", replacing it by "livia happy".


" "

" "

Therefore the window hide and window show will follow soon or later are not needed.


Python:
label start:
    scene default_background
    with fade

    show livia_neutral

    $ loop = True
    while loop:
        menu:
            "What should I do?"

            "Make her happy":
                hide livia_neutral
                show livia_happy
                window hide
                pause
                hide livia_happy

            "Make her angry":
                hide livia_neutral
                show livia_angry
                window hide
                pause
                hide livia_angry

            "Exit":
                $ loop = False
           
        show livia_neutral

    return



I wanted to create another label with the repeating code in it, and call it with the image livia_x, but that didn't work for some reason.
" "

The last sentence is a question of convenience. hide also accept the "expression" keyword, therefore the tag is optional when you are in full control of the flow.
But if, by example, the second image should be hidden, and hidden from another part of the code, the tag become mandatory. It would be the only way to know what image to address.


[Note: This follow the corrected code, not the original one]
Python:
label whatever( mood ):
    show expression "livia {}".format( mood )
    pause
    show livia neutral
    return

label start:
    scene default_background
    with fade

    show livia neutral

    $ loop = True
    while loop:
        menu:
            "What should I do?"

            "Make her happy":
                call whatever ( "happy" )

            "Make her angry":
                call whatever ( "angry" )

            "Exit":
                $ loop = False

    "END"
 
  • Like
Reactions: __b

__b

Member
Mar 4, 2020
181
2,322
There appears to be a problem with this version.
Python:
show expression "livia {}".format(mood)
This does unfortunately not replace the previous livia, but if I change this code to
Python:
show livia happy
it does replace the previous livia with the new happy one.

But now I lose the functionality to make a function that takes the mood as a parameter. How can this be solved?
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,969
16,222
Python:
show expression "livia {}".format(mood)
This does unfortunately not replace the previous livia, but if I change this code to
Well, the answer was in my post... Use a tag:
Python:
label whatever( mood ):
    show expression "livia {}".format( mood ) as livia
    [...]

label start:
    [...]
    show livia happy

But now I lose the functionality to make a function that takes the mood as a parameter.
Incidentally, I don't really see the interest of such function.

I can only talk for me, but as you present it in your example, it wouldn't be long before I find it too annoying to want to continue playing.
Having to press a key for her to return to her neutral expression is relatively useless. An would be better:
Python:
image livia temporarily angry:
    "images/livia/livia_angry.png"
    pause 2.0
    "images/livia/livia_neutral.png"

image livia temporarily happy:
    "images/livia/livia_happy.png"
    pause 2.0
    "images/livia/livia_neutral.png"

label start:
    scene default_background
    with fade

    show livia neutral

    $ loop = True
    while loop:
        menu:
            "What should I do?"

            "Make her happy":
                show livia temporarily happy

            "Make her angry":
                show livia temporarily angry

            "Exit":
                $ loop = False

    "END"
As for other uses, it's relatively the same. There's already the dialog lines that are here to temporize the progression, offering to the player all the time he need to looks at the CGs.
There's exception of course, but if your function(-like) need only one or two lines (without counting the return) then it's totally useless. Especially if one of those lines it a superfluous pause.
 
  • Like
Reactions: __b