Coding Efficiency (if / elif / else)

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,249
7,625
For the VN I am working on, I am changing the scene image dependent upon which sector of space they are in. Each sector has a different light setup / HDRI background. Whilst I can get this to work using the following code, it is on the verbose side. Hence is there are more efficient way of achieving the same result?
Code:
label room_lt_l01:
    if sector == "Pleiades":
        scene pcrtl1bb
    elif sector == "Beehive":
        scene pcrtl1gb
    else:
        scene pcrtl1rb
    lia "What's up Captain?"
    
    if sector == "Pleiades":
        scene pcrtl1bc
    elif sector == "Beehive":
        scene pcrtl1gc
    else:
        scene pcrtl1rc
    cap "Just setting course for star base 42."
    
    jump corridor
 

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,249
7,625
You could place the scenes into a map(dictionary).
Don't know what you are getting at here, however I have found a solution by using python:
$ renpy.show("pcrtl1bb_%s" % (sector))

I will have to rename a lot of images, however believe the final result will be worth it.

If you have a better method, please let me know.
 

Evilko

Member
Apr 11, 2017
120
232
Basically the same:
Code:
scene expression "pcrtl1bb_" + sector
You still need to rename images to this pattern.
 
  • Like
Reactions: Xavster

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,964
16,210
Don't know what you are getting at here, however I have found a solution by using python:
$ renpy.show("pcrtl1bb_%s" % (sector))
You don't need to use Python for this. show expression "pcrtl1bb_%s" % (sector) and scene expression "pcrtl1bb_%s" % (sector) while do the same, under the same condition than your own approach.

Now, for the "renaming" part itself, it depend of the constancy of your naming convention. According to your example, the letter is always "b" when sector is "Pleiades", always "g" when the sector is "Beehive", and always "r" else. Therefore you can use something like this :
Python:
init python:
    def bySector():
        if   sector == "Pleiades": return "b"
        elif sector == "Beehive": return "g"
        else:                     return "r"

label whatever:
    {...]
    scene expression "pcrt11{}b".format( bySector() )
    {...]
    scene expression "pcrt11{}c".format( bySector() )
    [...]
Depending of how your images are named and stored on the disk, you can also simply avoid to use a displayable, and use directly the path+name of the image.
Lets say that you split them by sector, having something like :
  • game
    • images
      • Pleiades
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Beehive
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Other
        • map.jpg
        • town.jpg
        • town gate.jpg

You can use :
Code:
label whatever:
    [...]
    scene expression "{}/town.jpg".format( sector )
    [...]
    scene expression "{}/town gate.jpg".format( sector )
    [...]
Note that both approach offer you the possibility to extend your game by adding a new sector, without the need to change the code of the game.
Python:
init python:
    def bySector():
        if   sector == "Pleiades": return "b"
        elif sector == "Beehive": return "g"
        elif sector == "Added sector": return "x"      # <- here
        else:                     return "r"
or
  • game
    • images
      • Pleiades
        [...]
      • Beehive
        [...]
      • Added sector
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Other
        [...]
 

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,249
7,625
You don't need to use Python for this. show expression "pcrtl1bb_%s" % (sector) and scene expression "pcrtl1bb_%s" % (sector) while do the same, under the same condition than your own approach.

Now, for the "renaming" part itself, it depend of the constancy of your naming convention. According to your example, the letter is always "b" when sector is "Pleiades", always "g" when the sector is "Beehive", and always "r" else. Therefore you can use something like this :
Python:
init python:
    def bySector():
        if   sector == "Pleiades": return "b"
        elif sector == "Beehive": return "g"
        else:                     return "r"

label whatever:
    {...]
    scene expression "pcrt11{}b".format( bySector() )
    {...]
    scene expression "pcrt11{}c".format( bySector() )
    [...]
Depending of how your images are named and stored on the disk, you can also simply avoid to use a displayable, and use directly the path+name of the image.
Lets say that you split them by sector, having something like :
  • game
    • images
      • Pleiades
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Beehive
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Other
        • map.jpg
        • town.jpg
        • town gate.jpg

You can use :
Code:
label whatever:
    [...]
    scene expression "{}/town.jpg".format( sector )
    [...]
    scene expression "{}/town gate.jpg".format( sector )
    [...]
Note that both approach offer you the possibility to extend your game by adding a new sector, without the need to change the code of the game.
Python:
init python:
    def bySector():
        if   sector == "Pleiades": return "b"
        elif sector == "Beehive": return "g"
        elif sector == "Added sector": return "x"      # <- here
        else:                     return "r"
or
  • game
    • images
      • Pleiades
        [...]
      • Beehive
        [...]
      • Added sector
        • map.jpg
        • town.jpg
        • town gate.jpg
      • Other
        [...]
That's one comprehensive and impressive explanation. (y)

I have elected to go with the method as below:
scene expression "pcrtl1bb_" + sector

There are not that many images at present, hence I have already relabelled to the image_b (g or r).

Things have been going well with the coding so far, thanks to help from this forum. All I really need to do now is include more content. I may have some more questions later related to music / sound, however have all of the navigation / conditional coding down-pat for the present I believe.

I am trying to make the environment as dynamic as possible, hence the changes of lighting / hold inventory etc. To give you some idea of the artwork / sector thingy, please see spoiler below.

You don't have permission to view the spoiler content. Log in or register now.
 
Last edited:
  • Like
Reactions: anne O'nymous

deepandsilent3dx

Active Member
Game Developer
Dec 13, 2018
571
3,683
Hence is there are more efficient way of achieving the same result?
I am of the opinion that one does not need to make big thoughts about efficienty with creation of usual VN.

You don't have thousands of queries per second in your game, where coding efficiency really plays a role.

More important is that the coding is clean and error free.
 

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,249
7,625
I am of the opinion that one does not need to make big thoughts about efficienty with creation of usual VN.

You don't have thousands of queries per second in your game, where coding efficiency really plays a role.

More important is that the coding is clean and error free.
"More important is that the coding is clean and error free."
I understand that modern PC's can process millions of operations per second. However with the optimisation of code as suggested by fellow F95 members, I have turned 8 lines of code into 1 line of code. This makes it easier for myself, and any error checkers of my code to review.

There is beauty in simplicity and efficiency.:coffee:
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,964
16,210
More important is that the coding is clean and error free.
And that's exactly what happen here.

When you have to write something like :
Code:
    if sector == "Pleiades":
        scene pcrtl1bb
    elif sector == "Beehive":
        scene pcrtl1gb
    else:
        scene pcrtl1rb
each time the scene feature the outer space, you've 7 risk of errors. 2 when writing "sector", 2 when writing the name of the sector, and 3 when writing the name of the displayable. All those errors can be seen only when playing the game, and have to be multiplied by the number of times you need to write this kind of code. Then, to ensure that everything is right, you need to test the label with each value of "sector".

Now, when you write :
Code:
    scene expression "pcrtl1{}b".format( bySector() )
You fall back to 2 possibilities of errors. The name of the displayable, and the name of the function.
Same when you write :
Code:
    scene expression "{}/pcrtl1b.jpg".format( sector )
Plus, in both case, if it works with one sector, you know for sure that it will works for all the sectors, which also reduce the time needed to test the code.

And yet, here Xavster goes for something that is already more efficient that what is generally used in Ren'py games. Face to this kind of situation, too many authors write something like this :
Code:
label whatever:
    [...]
    if sector == "Pleiades":
        jump meetingPleiades
    elif sector == "Beehives":
        jump meetingBeehives
    else:
        jump meetingOther

label meetingPleiades:
    [...]
    scene pcrt11bb
    [...]
    scene pcrt11bc
    [...]
    jump somewhereElse

label meetingBeehives:
    [...]
    scene pcrt11gb
    [...]
    scene pcrt11gc
    [...]
    jump somewhereElse

label meetingOther:
    [...]
    scene pcrt11gb
    [...]
    scene pcrt11gc
    [...]
    jump somewhereElse
which this time not only increase the risk of error ; going for a copy/paste approach increase it even more since you'll always forgot to change a value... always. It also complicate the bug correction and change in the scene.
In top of that, it need more time to be wrote, which limit the content available in each updates.
 
  • Like
Reactions: RHQuinn