How can I add into my code selectable items from inventory?

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: 237
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

How can I add into my code selectable items from inventory?

#1 Post by Nero » Wed Nov 16, 2016 2:41 pm

Hi it's me again "long time no see" juuust another "tiny" question :mrgreen:.

After long and painful way I am almost finished with my UI. So I created items, item values, inventory that contains those items, skills connected to those items etc.

Now last thing I have to do is make equippable item and the way I imagined it to be is the most simple like this: (Make button that says "Equip Item" and after you click this button current item in inventory gets selected as primary weapon.) Let me start with the code so you can understand better:

class for items:

Code: Select all

init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damagemin, damagemax, amount=0, desc="No Desc", picture = "No Picture", **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damagemin = damagemin
            self.damagemax = damagemax
            self.amount = amount
            self.desc = desc
            self.picture = picture
defined item example:

Code: Select all

default gun = Item("Gun", 450, 1, 40, 80, 1, "Guns and code are fun combination!", "gun.png")
And the screen that opens when I look at item description here is where I placed "Equip Item" button. Check the last line below...

Code: Select all

screen item_desc(item):
    add "gui/inventory_blank.png"
    modal True
    frame align (0.4, 0.5):
        style_group "item_desc_grp"
        at Position(xanchor = 0.0,xpos = 0.001, yanchor = 0.0, ypos = 0.80, xminimum = 10)
        xminimum 1439
        xmaximum 1439
        yminimum 200
        ymaximum 200
          
        vbox:
           text "[item.desc]"
           $ renpy.block_rollback()  
    frame:
        xpos 10
        style_group "nice_looking"
        xalign 0
        yalign 0.015     
        textbutton "CLOSE" action Hide("item_desc")
               
    frame align (0.5, 0.5):       
           image "[item.picture]"
      
    frame align (0.4, 0.5):
        style_group "item_desc_grp"
        at Position(xanchor = 0.0,xpos = 0.001, yanchor = 0.0, ypos = 0.74, xminimum = 10)
        vbox:
            frame:                
                text "[item.name]"
                
    frame align (0.2, 0.5):
        textbutton "EQUIP THIS ITEM" action Jump("equip_gun") ####Here is the line where I got stuck...
I was trying to do it this way but yeeeah...not really how this is supposed to work I guess. Could anyone give me a tip how to do it actually?

Code: Select all

    frame align (0.2, 0.5):
        if "[item.name]" == [gun.name]:
            textbutton "EQUIP THIS ITEM" action Jump("equip_gun")
        elif "[item.name]" == [sword.name]:
            textbutton "EQUIP THIS ITEM" action Jump("equip_sword")
            ##### ETC. for each item.
        
label equip_gun: # if gun is equipped
    $ sword_is_now_equipped = False
    $ gun_is_now_equipped = True
    
label equip_sword: # if sword is equipped
    $  gun_is_now_equipped = False
    $  sword_is_now_equipped = True
I know this is the wrong way but I hope you get the idea of how was I imagining this to work
Last edited by Nero on Thu Nov 17, 2016 1:37 pm, edited 2 times in total.

User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Re: Nero needs your help one more time (last time I promise!

#2 Post by Kaen » Thu Nov 17, 2016 12:14 pm

Hiya,

I think that if you use a proper title on your thread - like "How to Equip Items" or something - you'll raise the chances of getting a reply.

Take a look at screen data actions and pick the one that suits your case, like SetVariable() for example.

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

Re: Nero needs your help one more time (last time I promise!

#3 Post by Nero » Thu Nov 17, 2016 1:26 pm

Yeah good advice I will change the title. I asked so many question in short period time that I am feeling ashamed of spamming questions on this forum :oops: . Maybe if you would post example of how to implement it into the code its pretty hard to figure out what to do from just one line of code?

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

Re: How can I add into my code selectable items from invento

#4 Post by Pomeranian » Thu Nov 17, 2016 5:12 pm

I'd use python functions to do what you're trying to accomplish, personally. Using Leon's Inventory Screens as a base:

Code: Select all


init -1 python:

    class Player(renpy.store.object):
        def __init__(self, name, equip=None):
            self.name=name
            self.equip=equip
    
    class Item(store.object):
        def __init__(self, name, cost=0, wt=0, damagemin=0, damagemax=0, amount=0, desc="No Desc", picture = "", equip=""):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damagemin = damagemin
            self.damagemax = damagemax
            self.amount = amount
            self.desc = desc
            self.picture = picture
            self.equip = equip
            
    class Inventory(store.object):
        def __init__(self):
            self.items = []
        def equip(self, item):
            player.equip=item.equip #item is equipped
and then for the screen

Code: Select all

screen item_desc(item):
    add "gui/inventory_blank.png"
    modal True
    frame align (0.4, 0.5):
        style_group "item_desc_grp"
        at Position(xanchor = 0.0,xpos = 0.001, yanchor = 0.0, ypos = 0.80, xminimum = 10)
        xminimum 1439
        xmaximum 1439
        yminimum 200
        ymaximum 200
         
        vbox:
           text "[item.desc]"
           $ renpy.block_rollback() 
    frame:
        xpos 10
        style_group "nice_looking"
        xalign 0
        yalign 0.015     
        textbutton "CLOSE" action Hide("item_desc")
               
    frame align (0.5, 0.5):       
           image "[item.picture]"
     
    frame align (0.4, 0.5):
        style_group "item_desc_grp"
        at Position(xanchor = 0.0,xpos = 0.001, yanchor = 0.0, ypos = 0.74, xminimum = 10)
        vbox:
            frame:               
                text "[item.name]"
               
    frame align (0.2, 0.5):
        textbutton "EQUIP THIS ITEM" action Function(inventory.equip, item) 

be sure to add an equip name for the gun like:

Code: Select all

default gun = Item("Gun", 450, 1, 40, 80, 1, "Guns and code are fun combination!", "gun.png", equip = "Gun")
Your equip type of your selected item should be saved in the player.equip class. If you want something fancier, like with tooltips, I would suggest taking a look at Leon's Inventory Screens.

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

Re: How can I add into my code selectable items from invento

#5 Post by Nero » Thu Nov 17, 2016 6:55 pm

I got few errors in the code but I managed to fix them. I just needed the basic idea it works like a charm now thanks!

chrysagon
Newbie
Posts: 3
Joined: Tue Jan 31, 2017 1:38 pm
Location: France
Contact:

Re: How can I add into my code selectable items from invento

#6 Post by chrysagon » Tue Jan 31, 2017 2:03 pm

Thanks for the great job ! I am also using Leon's great code. I am pretty new to python, but I think I understood most of it. But I still have a little problem.
I am trying to add another type of item into the inventory : different types of armor that the player will find during the game. It is also a not a consumable item. so I added code like this :

Code: Select all

    inv_page = 0 # initial page of the inventory screen
    item = None
    class Player(renpy.store.object):
        def __init__(self, name, max_hp=0, max_mp=0, element=None, armor=None):
            self.name=name
            self.max_hp=max_hp
            self.hp=max_hp
            self.max_mp=max_mp
            self.mp=max_mp
            self.element=element
            self.armor=armor
I also dupplicated code everywhere, matching the settings of the 'element' parameter. And it almost works.
But I have a few problems : it seems I can't have at the same time some information into 'element' and into 'armor'. Each time the player chooses one into the inventory, it seems to empty the other. Plus, I can't make the word 'selected' appear on a weapon (element) and an armor (armor), probably for the same reasons.
Could you please help me ?

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]