Optimize if-else statement

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.
Post Reply
Message
Author
User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Optimize if-else statement

#1 Post by bonnie_641 »

When I managed to get the drag and drop game to work (thanks to Alex and hell-oh-world) I was very happy. However, when I added more elements, the game became slow (from 3 to 52 cards).
How can I optimize the game?

I think the long "if-else" statement check is the problem :oops:

Code: Select all

if _return == "uno": # if the card is 1, displays card contents
    call screen vamos_1() with dissolve
if _return == "dos": # if the card is 2, displays card contents
    call screen vamos_2() with dissolve
#(... rest of code)
if _return == "cincuenta_y_dos": # if the card is 52, displays card contents
    call screen vamos_52() with dissolve
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Optimize if-else statement

#2 Post by hell_oh_world »

Suggestions:
1. Do you really need to have a screen for each card? If each card screen shares the same style etc. then you can use parameters in your screen instead...

Code: Select all

screen card(name, pic):
  vbox:
    text name
    add pic

label start:
  if _return == "uno":
    call screen card("uno", "your image name")
2. In relation to #1, you can just do...

Code: Select all

define card_names = ("uno", "dos", "tres")

label start:
  if _return in card_names:
    call screen card(_return, "card {}".format(_return)) # card uno, card dos, etc...
3. If you're going to pursue the multiple if statements, just make the other statements elifs for the matter of logical coding.

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Optimize if-else statement

#3 Post by bonnie_641 »

Thank you so much! :D
I followed your recommendation and it works perfectly.

But I have another problem. The minigame performance is very slow (yet).
I was thinking that instead of using all 52 cards in the desk, I would only have 3 cards appear (I would have to apply a 'random' to the 3 cards to randomly show the 52 cards but not in a row).

Code: Select all

# the list of cards will be of 3, but implicitly it must carry the 52 cards
default cards_list = [
    {"name":"uno", "img":"card_face_1", "x_pos":197, "y_pos":202}, 
    {"name":"dos", "img":"card_face_1", "x_pos":197, "y_pos":202}, 
    {"name":"tres", "img":"card_face_1", "x_pos":197, "y_pos":202},  
    ]
default cards = ("img/card1.png", "img/card2.png", (...), "img/card52.png") #<---- does not work
default card_face_1 = renpy.random.shuffle(cards) <---------------------------------------------|
I tried every way I know how to get the cards to appear randomly, but:
1.- It does not show the image associated with the variable assigned to it.
2.- It only appears in order (of the first 3 cards, which is not random).
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Optimize if-else statement

#4 Post by Imperf3kt »

I have a blackjack game that uses a (four) deck(s) of 52 cards which may give you some ideas about how to proceed.
I think I've got the code somewhere linked in my signature, but I cannot be sure. - the game itself (for Android) is linked. I haven't made the code available yet.

After work today I'll try to modify it to do what you want and post it here if I manage to get it working. I need to anyway as my game lacks a GUI at the moment.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Optimize if-else statement

#5 Post by bonnie_641 »

Imperf3kt wrote: Wed Sep 08, 2021 11:34 pm I have a blackjack game that uses a (four) deck(s) of 52 cards which may give you some ideas about how to proceed.
I think I've got the code somewhere linked in my signature, but I cannot be sure. - the game itself (for Android) is linked. I haven't made the code available yet.

After work today I'll try to modify it to do what you want and post it here if I manage to get it working. I need to anyway as my game lacks a GUI at the moment.
Thank you very much for reply :D
I hope you can make it (in my game it supports 1 deck with 10 cards).

If you want, I can upload my demo so you can guide you (let me know, ok?)
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

Post Reply

Who is online

Users browsing this forum: No registered users