[RESOLVED] how to display the sum of 2 variables

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
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

[RESOLVED] how to display the sum of 2 variables

#1 Post by Pomeranian » Mon Nov 14, 2016 11:35 pm

EDIT: Please see the latest post for more information

I'm trying to make equipable items with stat changes, but I want the stats to be affected by whatever clothes the player is wearing, so for instance HP+20 while wearing winter clothes, or MP+50 while wearing a magician's robe, but those stats are only relevent while wearing the clothing item itself.(so when the player changes clothes, they won't have the previous item's stats)

I copied and pasted the relevent bits of code here. (this is in the items class)

Code: Select all

def use(self): 
            if self.battleitem == False and in_battle == True:
                renpy.show_screen("inventory_popup", message="This item cannot be used in battle.")
            elif self.clothes == True:
                if player.dress == self.clothestype:
                    renpy.show_screen("inventory_popup", message="You are already wearing this dress.")
                else:
                    player.dress = self.clothestype
                    hptotal = player.hp+self.hp
                    renpy.show_screen("inventory_popup", message=self.usdesc)
                    renpy.jump("home")
            elif self.battleitem == True and in_battle == False:
                renpy.show_screen("inventory_popup", message="This item can only be used in battle")
            elif self.name=="Book":
                player.kno += 8
                renpy.show_screen("inventory_popup", message=self.usdesc)
                inventory.drop(self)
            elif self.hp>0 and in_battle == True: #healing item
                player.hp = player.hp+self.hp
                if player.hp > player.max_hp: # can't heal beyond max HP
                    player.hp = player.max_hp
                renpy.show_screen("inventory_popup", message=self.usdesc)
                inventory.drop(self) # consumable item - drop after use
            else:
                renpy.show_screen("inventory_popup", message=self.usdesc)
and in the init python before the game starts:

Code: Select all


init python:
    player = Player("Derp", 50, 10)
    player.hp = 50
    player.mp = 10
    player.kno = 0
    player.dress = "Normal"
    hptotal = player.hp
    
    
    book = Item("Book", image="gui/inv_book1.png", usdesc="You read the book and already feel smarter!", cost=120)
    poetrybook = Item("Poetry Book", image="gui/inv_book2.png", usdesc="Read the book", cost=400)
    doll = Item("Doll", image="gui/inv_doll.png", usdesc="Played with the doll", cost=120)
    grenade = Item("Grenade", image="gui/inv_grenade.png", battleitem=True, usdesc="Used the Grenade", cost=100)
    healingpill = Item("Healing Pill", hp=40, image="gui/inv_healing.png", battleitem=True, usdesc="Used the Healing pill", cost=30)
    teacup = Item("Teacup", image="gui/inv_teacup.png", usdesc="Used the teacup", cost=500)
    clothes_normal = Item("Normal Dress", image="gui/inv_dressnormal.png", hp=0, clothes=True, clothestype="Normal", usdesc="Put on the Normal Dress", cost=10)
    clothes_summer = Item("Summer Dress", image="gui/inv_dresssummer.png", hp=10, clothes=True, clothestype="Summer", usdesc="Put on the Summer Dress", cost=70)
    clothes_winter = Item("Winter Dress", image="gui/inv_dresswinter.png", hp=20, clothes=True, clothestype="Winter", usdesc="Put on the Winter Dress", cost=120)

    inventory = Inventory()
    
    #add items to the initial inventory:
    inventory.add(clothes_normal)
Initially hptotal is set to just player.hp, but when the player changes their clothes, I set hptotal to player.hp+self.hp (of the item), however on the stat screen it still only displays the base player.hp and doesn't add the hp attributed to the clothing item despite me putting the hptotal variable there. There aren't any errors displayed when I start the game, but the changes are just not showing up.
Last edited by Pomeranian on Sun Nov 20, 2016 9:26 pm, edited 2 times in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Issues with variable not updating in equip with stat cha

#2 Post by Alex » Tue Nov 15, 2016 2:38 pm

Try to add
renpy.restart_interaction()
as the last line of "use" function.

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Issues with variable not updating in equip with stat cha

#3 Post by Pomeranian » Tue Nov 15, 2016 9:48 pm

Alex wrote:Try to add
renpy.restart_interaction()
as the last line of "use" function.
Tried this, but no changes. hptotal is still showing 50hp whether equipped with the summer dress or winter dress.

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#4 Post by Pomeranian » Sat Nov 19, 2016 10:49 am

I believe I've managed to narrow down the issue. What I want to do, essentially, is to display the sum of 2 variables as a single variable.

so something like:

Code: Select all

hptotal = player.hp + player.extra_hp
hptotal would be the sum of two stored variables in the player class. What is the correct way to go about this? The above does not display properly.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#5 Post by trooper6 » Sat Nov 19, 2016 12:29 pm

First off, you should show us your stat screen so we can see what that code looks like.

Nest, small things I see in your code that can make a big difference.
1) You should initialize your variables using default rather than in an init python block. It makes those things participate in saving/loading/etc better.

Code: Select all

default player = Player("Derp", 50, 10)
default player.hp = 50
default player.mp = 10
default player.kno = 0
default player.dress = "Normal"
default hptotal = player.hp
      
default book = Item("Book", image="gui/inv_book1.png", usdesc="You read the book and already feel smarter!", cost=120)
default poetrybook = Item("Poetry Book", image="gui/inv_book2.png", usdesc="Read the book", cost=400)
default doll = Item("Doll", image="gui/inv_doll.png", usdesc="Played with the doll", cost=120)
default grenade = Item("Grenade", image="gui/inv_grenade.png", battleitem=True, usdesc="Used the Grenade", cost=100)
default healingpill = Item("Healing Pill", hp=40, image="gui/inv_healing.png", battleitem=True, usdesc="Used the Healing pill", cost=30)
default teacup = Item("Teacup", image="gui/inv_teacup.png", usdesc="Used the teacup", cost=500)
default clothes_normal = Item("Normal Dress", image="gui/inv_dressnormal.png", hp=0, clothes=True, clothestype="Normal", usdesc="Put on the Normal Dress", cost=10)
default clothes_summer = Item("Summer Dress", image="gui/inv_dresssummer.png", hp=10, clothes=True, clothestype="Summer", usdesc="Put on the Summer Dress", cost=70)
default clothes_winter = Item("Winter Dress", image="gui/inv_dresswinter.png", hp=20, clothes=True, clothestype="Winter", usdesc="Put on the Winter Dress", cost=120)

default inventory = Inventory()

label start:    
    #add items to the initial inventory:
    $inventory.add(clothes_normal)
2) Your use method...is this a method that is inside of a class or is it a freestanding method? I assume it is inside of the Item class because of the "self"--Can we see that whole class? I think one of your problems is in there. Namely, hptotal and inventory are not variables that are inside of the Item class and you expect the individual item to be able to change them. I'd pass in both of those variables into the use function and see if that helps.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#6 Post by Pomeranian » Sat Nov 19, 2016 2:00 pm

1. Thanks. I was using the code that I did due to it being from the quickstart renpy framework but it's probably quite out of date.

2. Yes, it's inside the Item class. Inventory is its own class, and hptotal would be a variable related to the Player class I believe? I'm not entirely sure how I would display it because trying to set hptotal like this:

hptotal = player.hp + player.extra_hp

didn't work.

This is the code I've been using. Some of it is clearly out of date, though. I'm still rather inexperienced so I can't say I really know how to optimize it.

Code: Select all

init -1 python:
    import renpy.store as store
    import renpy.exports as renpy # we need this so Ren'Py properly handles rollback with classes
    from operator import attrgetter # we need this for sorting items

    inv_page = 0 # initial page of teh inventory screen
    in_battle = False
    item = None
    

    class Player(renpy.store.object):
        def __init__(self, name, max_hp=0, max_mp=0, kno=0, extra_hp=0, dress=None):
            self.name=name
            self.max_hp=max_hp
            self.hp=max_hp
            self.max_mp=max_mp
            self.mp=max_mp
            self.kno=kno
            self.extra_hp=extra_hp
            self.dress=dress
    
    class Item(store.object):
        def __init__(self, name, player=None, kno=0, hp=0, mp=0, image="", battleitem=False, clothes=False, clothestype=None, usdesc="Nothing Happened!", desc="No Information", cost=0):
            self.name = name
            self.player=player # which character can use this item?
            self.kno = kno # item raises knowledge
            self.hp = hp # does this item restore hp?
            self.mp = mp # does this item restore mp?
            self.battleitem=battleitem # is this a battle item?
            self.clothes=clothes
            self.clothestype=clothestype
            self.image=image # image file to use for this item
            self.usdesc = usdesc
            self.desc = desc
            self.cost=cost # how much does it cost in shops?
            
        def use(self): #here we define what should happen when we use the item
            if self.battleitem == False and in_battle == True:
                renpy.show_screen("inventory_popup", message="This item cannot be used in battle.")
            elif self.battleitem == True and in_battle == False:
                renpy.show_screen("inventory_popup", message="This item can only be used in battle.")
            elif self.name=="Book":
                player.kno += 8
                renpy.show_screen("inventory_popup", message=self.usdesc)
                inventory.drop(self)
            elif self.hp>0 and in_battle == True: #healing item
                player.hp = player.hp+self.hp
                if player.hp > player.max_hp: # can't heal beyond max HP
                    player.hp = player.max_hp
                renpy.show_screen("inventory_popup", message=self.usdesc)
                inventory.drop(self) # consumable item - drop after use
            else:
                renpy.show_screen("inventory_popup", message=self.usdesc)
        
        def equip(self):
            if player.dress == self.clothestype:
                renpy.show_screen("inventory_popup", message="You are already wearing this dress.")
            elif player.dress != "Normal":
                renpy.show_screen("inventory_popup", message="Unequip your current dress first.")
            else:
                player.dress = self.clothestype
                player.extra_hp = self.hp
                renpy.hide_screen("invscreen_use")
                renpy.show_screen("inventory_popup", message=self.usdesc)
                renpy.jump("home")
               
        def unequip(self):
            if player.dress == "Normal":
                renpy.hide_screen("invscreen_use") 
                renpy.show_screen("inventory_popup", message="No need to unequip the default dress.")
                renpy.jump("home")               
            elif player.dress == self.clothestype:
                player.dress = "Normal"
                player.extra_hp = 0
                renpy.hide_screen("invscreen_use")
                renpy.show_screen("inventory_popup", message="Item Unequipped")
                renpy.jump("home")
            else:
                renpy.show_screen("inventory_popup", message="You are not wearing this dress.")


            
    class Inventory(store.object):
        def __init__(self, money=500):
            self.money = money
            self.items = []
        def add(self, item): # a simple method that adds an item; we could also add conditions here (like check if there is space in the inventory)
            self.items.append(item)
        def drop(self, item):
            self.items.remove(item)
        def sell(self, item):
            if player.dress == item.clothestype:
                renpy.show_screen("inventory_popup", message="You cannot sell an item you are wearing.")
            elif item.cost>0:
                self.money += item.cost
                self.items.remove(item)
                renpy.show_screen("inventory_popup", message="Item sold.")
            else:
                renpy.show_screen("inventory_popup", message="This item cannot be sold.")
        def buy(self, item):
            if self.money >= item.cost:
                self.items.append(item)
                self.money -= item.cost
                renpy.show_screen("inventory_popup", message="Thank you for your purchase")
            else:
                renpy.show_screen("inventory_popup", message="You don't have enough money to buy this item")
        def purchase(self, item):
            s = item.name
            d = item.desc
            i = item.cost
            renpy.show_screen("invscreen_use", title = item.name, pic = item.image, message="%s\n\nAre you sure you wish to buy this %s for %d gold?" %(d, s, i), yes_action=[Function(inventory.buy, item), Hide("invscreen_use")], no_action=[Show("inventory_popup", message="Feel free to keep browsing"), Hide("invscreen_use")], sell_action=None, equip_action=None, unequip_action=None)

        def use(self, item):
            s = item.name
            d = item.desc
            if item.clothes == True:
                renpy.show_screen("invscreen_use", title = item.name, pic = item.image, message="%s\n\nAre you sure you wish to use this %s ?" %(d, s), yes_action=None, no_action=None, sell_action=[Function(inventory.sell, item), Hide("invscreen_use")], equip_action=[Function(item.equip), Hide("invscreen_use")], unequip_action=[Function(item.unequip), Hide("invscreen_use")])
            else:
                renpy.show_screen("invscreen_use", title = item.name, pic = item.image, message="%s\n\nAre you sure you wish to use this %s ?" %(d, s), yes_action=[Function(item.use), Hide("invscreen_use")], no_action=Hide("invscreen_use"), sell_action=[Function(inventory.sell, item), Hide("invscreen_use")], equip_action=None, unequip_action=None)

    def item_use():
        item.use()
    
    def inv_use():
        inventory.use(item)
This is the relevent part of the stats screen, indicating the variable I'd like to show. It's pretty simple - just a bar and text where I've been trying to display the variable.

Code: Select all

screen stats:
    
    tag menu
            
    frame:
        style_group "statsframe"
        xalign 0.02 
        yalign 0.3
        xmaximum 450
        ymaximum 380
        
        grid 3 13:
    
                text "HP : "
                text "[hptotal]"
                bar value StaticValue(hptotal, 999):
                    style "bar_stats"
I want it to be showing [hptotal] as player.hp + player.extra_hp so that I can have the stats change with the equipment automatically, rather than having to manually unequip an item first. I want player.extra_hp to always reference whatever item.hp was, with the default clothes' hp value being 0.

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#7 Post by Pomeranian » Sun Nov 20, 2016 12:55 pm

It's still not working. This is my current code, where I've put hptotal and the equip and unequip methods under the Player class

Code: Select all

    class Player(renpy.store.object):
        def __init__(self, name, max_hp=0, max_mp=0, kno=0, extra_hp=0, dress=None):
            self.name=name
            self.max_hp=max_hp
            self.hp=max_hp
            self.max_mp=max_mp
            self.mp=max_mp
            self.kno=kno
            self.extra_hp=extra_hp
            self.dress=dress
            self.hptotal = self.max_hp + self.extra_hp
            
        def equip(self, item):
            if self.dress == item.clothestype:
                renpy.show_screen("inventory_popup", message="You are already wearing this dress.")
            elif self.dress != "Normal":
                renpy.show_screen("inventory_popup", message="Unequip your current dress first.")
            else:
                self.dress = item.clothestype
                self.extra_hp = item.hp
                renpy.hide_screen("invscreen_use")
                renpy.show_screen("inventory_popup", message=item.usdesc)
                renpy.jump("home")
               
        def unequip(self, item):
            if self.dress == "Normal":
                renpy.hide_screen("invscreen_use") 
                renpy.show_screen("inventory_popup", message="No need to unequip the default dress.")
                renpy.jump("home")               
            elif self.dress == item.clothestype:
                self.dress = "Normal"
                self.extra_hp = 0
                renpy.hide_screen("invscreen_use")
                renpy.show_screen("inventory_popup", message="Item Unequipped")
                renpy.jump("home")
            else:
                renpy.show_screen("inventory_popup", message="You are not wearing this dress.")

Code: Select all

default player = Player("Derp", 50, 10)
default player.hp = 50
default player.mp = 10
default player.extra_hp = 0
default player.kno = 0
default player.dress = "Normal"
default player.hptotal = player.max_hp + player.extra_hp
I put [player.hptotal] on the stats page, but it's still only showing the regular player hp stat and not with the added extra hp from the clothes :? If anyone can help me figure this out, I'd really appreciate it.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#8 Post by xela » Sun Nov 20, 2016 4:03 pm

Code: Select all

init python:
    class Item(object):
        def __init__(self, hp, type):
            self.hp = hp
            self.clothestype = type
    
    class Player(object):
        def __init__(self, name, max_hp=0, max_mp=0, kno=0, extra_hp=0, dress=None):
            self.name = name
            self.max_hp = max_hp
            self.hp = max_hp
            self.max_mp = max_mp
            self.mp = max_mp
            self.kno = kno
            self.extra_hp = extra_hp
            self.dress = dress
            
        @property
        def hptotal(self):
            return self.max_hp + self.extra_hp
           
        def equip(self, item):
            self.dress = item.clothestype
            self.extra_hp = item.hp
                
default player = Player("Derp", 50, 10)
default someitem = Item(10000, "someclothingtype")

label start:
    "Base HP: [player.hptotal]"
    $ player.equip(someitem)
    "HP After we equipped some time: [player.hptotal]"
Like what we're doing? Support us at:
Image

User avatar
Pomeranian
Regular
Posts: 33
Joined: Wed Oct 12, 2016 6:52 pm
Contact:

Re: Trying to figure out how to display the sum of 2 variabl

#9 Post by Pomeranian » Sun Nov 20, 2016 9:26 pm

xela wrote:

Code: Select all

init python:
    class Item(object):
        def __init__(self, hp, type):
            self.hp = hp
            self.clothestype = type
    
    class Player(object):
        def __init__(self, name, max_hp=0, max_mp=0, kno=0, extra_hp=0, dress=None):
            self.name = name
            self.max_hp = max_hp
            self.hp = max_hp
            self.max_mp = max_mp
            self.mp = max_mp
            self.kno = kno
            self.extra_hp = extra_hp
            self.dress = dress
            
        @property
        def hptotal(self):
            return self.max_hp + self.extra_hp
           
        def equip(self, item):
            self.dress = item.clothestype
            self.extra_hp = item.hp
                
default player = Player("Derp", 50, 10)
default someitem = Item(10000, "someclothingtype")

label start:
    "Base HP: [player.hptotal]"
    $ player.equip(someitem)
    "HP After we equipped some time: [player.hptotal]"
It works perfectly! Thank you so much! Topic is now resolved :)

Post Reply

Who is online

Users browsing this forum: No registered users