[SOLVED] Drinking Potions How To Improve The Code

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
Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

[SOLVED] Drinking Potions How To Improve The Code

#1 Post by Nero »

Here I have a functional code that I use inside my battle engine.

My question is how could I improve it (make it smaller and more efficient).

Here I have a code that when item is selected this function will become active I explained every step in the code. Basically what I'm aiming for is to have better cooldown system that I have right now.
What if i had like 30+ items do I really need to make "self.COOLDOWN_ITEM_1, 2, 3 ,4, 5...etc." for every new item I make?

Code: Select all

        def USE_POTION(self, item):
            item_name = item.NAME # Used to display the name that was consumed
            player_name = self.NAME # Displays the name of player that used pot
            if self.CURRENT_MANA >= 30: # If we don't have enough mana we can't use this potion
                self.CURRENT_MANA -= 30
                if item.NAME == "Potion Of Fire Resistance":
                    self.COOLDOWN_ITEM_1 = 4 # As long as this is > 0 the potion will be active on character       
                elif item.NAME == "Apple":
                    self.COOLDOWN_ITEM_2 = 3
                    
                item.AMOUNT -= 1 # Decrease the amount that is in inventory
                self.FIRE_RESISTANCE += item.FIRE_RESISTANCE # Player will get additional fire resistance based on item resistance
                self.HP_REGENERATION += item.HP_REGENERATION 
                renpy.say(battle_speaker,"{} consumed {}!".format(current_player.NAME, item_name)) # Displays a message of current action
                renpy.jump("END_TURN_{}".format(player_name)) # Jump to specific label that will end players turn and go into the loop of POTION_COOLDOWNS
                
            else:
                renpy.say(battle_speaker,"{} don't have enough energy!".format(current_player.NAME))

Here is my cooldown system inside the battle engine loop:
Same question is for this also as these two are connected witch each other.
So for every item I have to make new cooldown number which is not very efficient and I would like to get better at making my code so if anyone have any suggestions on how to do it please tell me :D

Code: Select all

        def POTION_COOLDOWNS(self):
            if self.COOLDOWN_ITEM_1 > 0:  ### Potion Of Fire Resistance
                self.COOLDOWN_ITEM_1 -= 1 # Each turn item cooldown gets dropped by 1
                if self.COOLDOWN_ITEM_1 == 0: # After the potion time hits 0 the item value will be removed from a player
                    self.FIRE_RESISTANCE -= P1.FIRE_RESISTANCE  

            if self.COOLDOWN_ITEM_2 > 0:  ### Apple Hp Regeneration +10%
                self.COOLDOWN_ITEM_2 -= 1
                if self.COOLDOWN_ITEM_2 == 0:
                    self.HP_REGENERATION -= P2.HP_REGENERATION
Last edited by Nero on Sat Mar 03, 2018 9:56 pm, edited 3 times in total.

irredeemable
Regular
Posts: 78
Joined: Thu Feb 08, 2018 7:57 am
Contact:

Re: Drinking Potions How To Improve The Code

#2 Post by irredeemable »

Well, I think there are some things that fundamentally look off about the code but to keep it close to what you already have, you could add additional fields to your item class: effect (what field on the player object it is affecting--in your example this value would be equal to "FIRE_RESISTANCE" for a fire resist potion), magnitude (how much you're adding, subtracting from that field), cooldown, and mana_cost (self-explanatory). Then ceate a dictionary in the player class, let's call it cooldowns, that maps items currently in use to their remaining cooldowns.

So now USE_POTION and POTION_COOLDOWNS would look something like:

Code: Select all

def USE_POTION(self, item):
    if all([self.CURRENT_MANA >= item.mana_cost, not item in self.cooldowns, item.AMOUNT > 0]):
        self.CURRENT_MANA -= item.mana_cost
        item.AMOUNT -= 1
        self.cooldowns[item] = item.cooldown
        setattr(self, item.effect, getattr(self, item.effect) + item.magnitude)
        renpy.say(battle_speaker,"{} consumed {}!".format(current_player.NAME, item.name))
        renpy.jump("END_TURN_{}".format(self.NAME))
    else:
        # failure code here
        "Failed for one of three reasons, I dunno."

def POTION_COOLDOWNS(self):
    for item, cooldown in self.cooldowns.items(): 
        if cooldown == 0:
            setattr(self, item.effect, getattr(self, item.effect) - item.magnitude)
            del self.cooldowns[item]
        else:
            self.cooldowns[item] -= 1

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Drinking Potions How To Improve The Code

#3 Post by Nero »

Wow thank you so much! This is so nicely explained I followed everything you said and it works better than expected. This is so differently written from what I'm used to I will use this example to fix many other mistakes I made before.

Yeah my code is pretty noobish I used only 2 classes entire time item and player. I dumped 80% of variables into Player class like skills, potions etc. Not sure if that was a good idea.

Now that everything works fine 1 more question since now is my display messed up little bit I had an icon that shows the cooldown remaining. I would like to know how can I get cooldown duration check for it so it can show up again when item is active?

Before I used code like this:

Code: Select all

if each_party_member.COOLDOWN_ITEM_1 > 0:
So how would this line be right now with your code?
My idea was when the cooldown is greater than 0 then the icon would pop-up and when cooldown drops to 0 icon would be removed. So my next item would be COOLDOWN_ITEM_2 etc...

Full code below. (this is written inside the screen)

Code: Select all

                if each_party_member.COOLDOWN_ITEM_1 > 0: # If this is > 0 the small potion icon will appear with remaning cooldown number
                    text "[each_party_member.COOLDOWN_ITEM_1]" 
                    imagebutton:
                        hovered Show("TOOLTIP_DISPLAY")
                        idle "HoverScreens/hoverimagesmall/FR1_idle.png"
                        hover "HoverScreens/hoverimagesmall/FR1_hover.png"
                        unhovered Hide("TOOLTIP_DISPLAY")
                        action NullAction()

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: Drinking Potions How To Improve The Code

#4 Post by Nero »

Ok I got it:

Code: Select all

                for item, cooldown in each_party_member.cooldowns.items():
                    text "[cooldown]"
                    imagebutton:
                        hovered Show("TOOLTIP_DISPLAY")
                        idle "HoverScreens/hoverimagesmall/FR1_idle.png"
                        hover "HoverScreens/hoverimagesmall/FR1_hover.png"
                        unhovered Hide("TOOLTIP_DISPLAY")
                        action NullAction()
Thank you once more!

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: [SOLVED] Drinking Potions How To Improve The Code

#5 Post by Nero »

Ugh sorry I had to reopen this.
New problem appeared that screwed me over.
This will only work for 1 item that will display the tooltip. For example if I had the 10 items and I used them all the LAST one that I consumed will keep the tooltip so when I hover over the any icon the tooltip display will be from the last item consumed. How do I repair that I'm stuck now.

small icon screen: when I hover over the small icon tooltip below will appear

Code: Select all

                for item, cooldown in each_party_member.cooldowns.items():
                    if cooldown > 0:
                        text "[cooldown]" bold True  xalign 0.017 size 17 ypos 17 outlines [ (absolute(2), "#000", absolute(0), absolute(0)) ]
                        imagebutton:
                            hovered Show("potion_tooltip")
                            idle item.small_icon_effect
                            hover item.small_icon_effect
                            unhovered Hide("potion_tooltip")
                            action NullAction()
potion_tooltip screen

Code: Select all

screen potion_tooltip:
    zorder 106
    for i, each_party_member in enumerate(party_members_list):
            for item, cooldown in each_party_member.cooldowns.items():
                add "gui/text_box4.png" xalign 0.500 yalign 1.000
                add item.tooltip_icon yalign 0.988 xalign 0.075
                text "[item.e_desc_1]" xsize 1200 ypos 758 xpos 240 size 25 bold True
                text "[item.e_desc_2]" xsize 1200 ypos 790 xpos 240 size 22

EDIT:
Here with image so you can have better understanding what I mean.
Image

irredeemable
Regular
Posts: 78
Joined: Thu Feb 08, 2018 7:57 am
Contact:

Re: [RE-OPENED] Drinking Potions How To Improve The Code

#6 Post by irredeemable »

If I get what you're trying to do, try:

Code: Select all

hovered Show("potion_tooltip", item=item)
In the imagebutton and change the tooltip screen to:

Code: Select all

screen potion_tooltip(item):
    zorder 106
    add "gui/text_box4.png" xalign 0.500 yalign 1.000
    add item.tooltip_icon yalign 0.988 xalign 0.075
    text "[item.e_desc_1]" xsize 1200 ypos 758 xpos 240 size 25 bold True
    text "[item.e_desc_2]" xsize 1200 ypos 790 xpos 240 size 22

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: [SOLVED] Drinking Potions How To Improve The Code

#7 Post by Nero »

You are a God! That's it thank you sooo much!! I have so much to learn. :)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]