Page 3 of 7

Re: Inventory Screen

Posted: Sun Feb 15, 2015 6:32 pm
by Roxie
Hi Leon,

So I couldn't get it to work with my code, so to test it I tried it using yours purely. Still not working right as it's not selecting anything. Here's the code, maybe I'm missing something?

Code: Select all

init -1 python:
    import copy
    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
    item = None
    class Player(renpy.store.object):
        def __init__(self, name, max_hp=0, max_mp=0, element=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
    player = Player("Derp", 100, 50)
    
    class Item(store.object):
        def __init__(self, name, player=None, hp=0, mp=0, element="", image="", recipeItem=False):
            self.name = name
            self.selected = False
            self.recipeItem = recipeItem
            self.player=player # which character can use this item?
            self.hp = hp # does this item restore hp?
            self.mp = mp # does this item restore mp?
            self.element=element # does this item change elemental damage?
            self.image=image # image file to use for this item
        def use(self): #here we define what should happen when we use the item
            if self.recipeItem:
                if self in inventory.selected_items:
                    inventory.selected_items.remove(self)
            else:
                inventory.selected_items.append(self)
            
    class Inventory(store.object):
        def __init__(self):
            self.items = []
            self.selected_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)
    
    class Recipe(store.object):
        def __init__(self, items=[], item_created=None):
            self.items = items
            self.item_created = item_created
        def combine_check(self):
            too_many=False # too many items selected?
            correct_recipe_items = copy.copy(self.items) # make a copy of the list of recipe items
            for item in inventory.selected_items:
                if item.recipeItem: # and item in self.items:
                    if item in correct_recipe_items:
                        correct_recipe_items.remove(item) # remove the current item from the list of correct_recipe_items; if correct_recipe_items is empty at the end, we have created the recipe
                    if not (item in self.items): # current item is not in this recipe
                        too_many=True
            if len(correct_recipe_items) == 0 and not too_many: # correct_recipe_items is empty means we have selecte just the right items
                inventory.add(self.item_created) # add the newly created item
                for drop_item in self.items: # remove all the items in this recipe from inventory
                    inventory.drop(drop_item)

    def item_use():
        item.use()

    #Tooltips:
    style.tips_top = Style(style.default)
    #style.title.font="gui/arial.ttf"
    style.tips_top.size=14
    style.tips_top.color="fff"
    style.tips_top.outlines=[(3, "6b7eef", 0,0)]
    style.tips_top.kerning = 5

    style.tips_bottom = Style(style.tips_top)
    style.tips_top.size=20
    style.tips_bottom.outlines=[(0, "6b7eef", 1, 1), (0, "6b7eef", 2, 2)]
    style.tips_bottom.kerning = 2
    
    style.button.background=Frame("gui/frame.png",25,25)
    style.button.yminimum=52
    style.button.xminimum=52
    style.button_text.color="000"


    showitems = True #turn True to debug the inventory
    def display_items_overlay():
        if showitems:
            inventory_show = "HP: " + str(player.hp) + " bullets: " + str(player.mp) + " element: " + str(player.element) + "\nInventory: "
            for i in range(0, len(inventory.items)):
                item_name = inventory.items[i].name
                if i > 0:
                    inventory_show += ", "
                inventory_show += item_name
            
            ui.frame()
            ui.text(inventory_show, color="#000")
    config.overlay_functions.append(display_items_overlay)
    
        
screen inventory_button:
    textbutton "Show Inventory" action [ Show("inventory_screen"), Hide("inventory_button")] align (.95,.04)
            
screen inventory_screen:    
    add "gui/inventory.png" # the background
    modal True #prevent clicking on other stuff when inventory is shown
    hbox align (.95,.04) spacing 20:
        textbutton "Close Inventory" action [ Hide("inventory_screen"), Show("inventory_button"), Return(None)]
    $ x = 515 # coordinates of the top left item position
    $ y = 25
    $ i = 0
    $ sorted_items = sorted(inventory.items, key=attrgetter('name')) #sort by name
    $ sorted_items = sorted(inventory.items, key=attrgetter('element'), reverse=True) # we sort the items, so non-consumable items that change elemental damage (guns) are listed first
    $ next_inv_page = inv_page + 1            
    if next_inv_page > int(len(inventory.items)/9):
        $ next_inv_page = 0
    $ tmp_inventory = []
    for item in sorted_items:
        $ tmp_inventory.append(item)
        if tmp_inventory.count(item) == 1:
            if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
                $ x += 190
                if i%3==0:
                    $ y += 170
                    $ x = 515
                $ pic = item.image
                $ my_tooltip = "tooltip_inventory_" + pic.replace("gui/inv_", "").replace(".png", "") # we use tooltips to describe what the item does.
                if item.recipeItem:
                    imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), SetVariable("item", item), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
                else:
                    imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), Show("inventory_button"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
                if item in inventory.selected_items:
                    add "gui/selected.png" xpos x ypos y anchor(.5,.5)                        
                $ i += 1
            if sorted_items.count(item) > 1:
                text str(sorted_items.count(item)) xpos x+50 ypos y-50 anchor(.5,.5) color "#000" #align(.9,.1)
        if len(inventory.items)>9:
            textbutton _("Next Page") action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")] xpos .475 ypos .83
    textbutton "Combine" action [Function(combine_items), Hide("inventory_screen"), Show("inventory_screen")] align (.80,.80)
    
    
    
screen gui_tooltip (my_picture="", my_tt_xpos=58, my_tt_ypos=687):
    add my_picture xpos my_tt_xpos ypos my_tt_ypos

init -1:
    transform inv_eff: # too lazy to make another version of each item, we just use ATL to make hovered items super bright
        zoom 0.5 xanchor 0.5 yanchor 0.5
        on idle:
            linear 0.2 alpha 1.0
        on hover:
            linear 0.2 alpha 2.5
        on selected_idle:
            linear 0.2 alpha 1.0
        on selected_hover:
            linear 0.2 alpha 2.5

    image information = Text("INFORMATION", style="tips_top")
    #Tooltips-inventory:
    image tooltip_inventory_chocolate=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("Generic chocolate to heal\n40 points of health.", style="tips_bottom"))
    image tooltip_inventory_banana=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A healthy banana full of potassium! You can also use it as ammo for your guns! O.O Recharges 20 bullets.", style="tips_bottom"))
    image tooltip_inventory_gun=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("An gun that looks like something a cop would\ncarry around. Most effective on humans.", style="tips_bottom"))
    image tooltip_inventory_laser=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("An energy gun that shoots photon beams.\nMost effective on aliens.", style="tips_bottom"))
    image tooltip_inventory_puffy=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("Mr. Puffy from the bakery.", style="tips_bottom"))
    image tooltip_inventory_box=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("An unused box.", style="tips_bottom"))
    image tooltip_inventory_puffyset=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("Can't go wrong with a box of Mr. Puffy.", style="tips_bottom"))
    image sidewalk = "Sidewalk.jpg"

init python:    
    def combine_items():
        for recipe in recipes:
            recipe.combine_check()
        inventory.selected_items = []
        return

init python:    
    def combine_items():
        for item in inventory.items:
            for recipe in recipes:
                recipe_tmp = recipe
                if item in recipe and item.selected:
                    recipe_tmp.remove(item)
                    if len(recipe_tmp)==1:
                        for tmp_item in recipe:
                            pass
                            inventory.drop(item)
                        inventory.add(recipe[0])
        return

# The game starts here.
label start:
    python:
        
        player = Player("Derp", 100, 50)
        player.hp = 50
        player.mp = 10
        chocolate = Item("Chocolate", image="gui/inv_chocolate.png",recipeItem=True)
        banana = Item("Banana", image="gui/inv_banana.png",recipeItem=True)    
        gun = Item("Gun", image="gui/inv_gun.png",recipeItem=True)
        laser = Item("Laser Gun", image="gui/inv_laser.png",recipeItem=True)
        puffy = Item("Mr. Puffy", image="gui/inv_puffy.png",recipeItem=True)
        box = Item("Box", image="gui/inv_box.png",recipeItem=True)
        puffyset = Item("Cream puffs", image="gui/inv_puffyset.png")
        inventory = Inventory()
        #add items to the initial inventory:
        inventory.add(chocolate)
        inventory.add(banana)
        inventory.add(puffy)
        inventory.add(box)
        #recipes = [] 
        #recipes.append([puffy, box, puffyset]) 
        #recipes.append([chocolate, banana, laser])
        recipes = [Recipe([chocolate, banana], laser), Recipe([puffy, box], puffyset)]

Re: Inventory Screen

Posted: Tue Feb 17, 2015 12:40 am
by TheGameGirl
Hi so I have literally copy and pasted you code into my game, and I haven't edited it to fit my specific game yet but I am alredy getting an error with one section of the code. Here is what I'm getting:

File "game/script.rpy", line 208, in script
python:
File "game/script.rpy", line 212, in <module>
chocolate = Item("Chocolate", hp=40, image="gui/inv_chocolate.png")
TypeError: __init__() got an unexpected keyword argument 'image'

Full traceback:
File "game/script.rpy", line 208, in script
python:
File "C:\Users\Madison\Documents\My Games\renpy-6.18.3-sdk\renpy\ast.py", line 785, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\Users\Madison\Documents\My Games\renpy-6.18.3-sdk\renpy\python.py", line 1382, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 212, in <module>
chocolate = Item("Chocolate", hp=40, image="gui/inv_chocolate.png")
TypeError: __init__() got an unexpected keyword argument 'image'

again I haven't edited anything, just placed the code in the appropriate area, so maybe it's something in the code itself or I'm just an idiot who is missing something (very very possible) any help with this I could get would be great!

Re: Inventory Screen

Posted: Tue Feb 17, 2015 1:16 am
by Roxie
@TheGameGirl - While I don't know what caused your specific error, I found that you're less likely to have errors if you modify the code and game file directly to make it specific to your own game. Then later copy and paste everything into the main game. Just make sure all the folder directories and images are in the right place and format, and replace it accordingly.

Re: Inventory Screen

Posted: Fri Mar 13, 2015 5:37 am
by Luxliev
I just want to ask why script/text keeps advancing when I press close inventory button? Is this bug?

Re: Inventory Screen

Posted: Fri Mar 13, 2015 2:38 pm
by philat
Luxliev wrote:I just want to ask why script/text keeps advancing when I press close inventory button? Is this bug?
Delete Return(None) from the actions of the close button in the inventory screen and it won't advance.

Re: Inventory Screen

Posted: Thu May 14, 2015 11:08 pm
by Xaiyeon
Sorry to necro(?), but when you select an item and it adds the select image, what code can keep it in the inventory screen and not exit out?

Re: Inventory Screen

Posted: Fri May 15, 2015 3:17 am
by Swawa3D
Xaiyeon wrote:Sorry to necro(?), but when you select an item and it adds the select image, what code can keep it in the inventory screen and not exit out?
Are you just trying to have the inventory screen stay open until you press close? Editing the imagebutton from the inventory_screen as follows seems to do the trick...

Code: Select all

imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), SetVariable("item", item), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff

Re: Inventory Screen

Posted: Fri May 15, 2015 2:09 pm
by Xaiyeon
Swawa3D wrote:
Are you just trying to have the inventory screen stay open until you press close? Editing the imagebutton from the inventory_screen as follows seems to do the trick...

Code: Select all

imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), SetVariable("item", item), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
Oh, I did not see that there, thank you! xD

Re: Inventory Screen

Posted: Sat Jun 27, 2015 9:22 am
by burnt_offering
I'm getting that Store doesn't work.

Specifically, my error message is:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/gameplay code.rpy", line 234, in script
  File "renpy/common/000statements.rpy", line 447, in python
  File "game/gameplay code.rpy", line 92, in python
  File "renpy/common/00action_data.rpy", line 46, in python
AttributeError: 'StoreModule' object has no attribute 'item'
For the code:

Code: Select all

screen inventory_screen:
    tag game
    $ inv_page=0
    add "gui/inventory.png" # the background
    modal True #prevent clicking on other stuff when inventory is shown
    hbox align (.95,.04) spacing 20:
        textbutton "Close Inventory" action [Show(CurrentScreen)]
    $ sorted_items = sorted(inventory.items, key=attrgetter('name'), reverse=True)
    $ x = 515 # coordinates of the top left item position
    $ y = 25
    $ i = 0
    $ next_inv_page = inv_page + 1            
    if next_inv_page > int(len(inventory.items)/9):
        $ next_inv_page = 0
    for item in sorted_items:
        if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
            $ x += 190
            if i%3==0:
                $ y += 170
                $ x = 515
            $ pic = item.image
            $ my_tooltip = "tooltip_inventory_" + pic.replace("gui/inv_", "").replace(".png", "") # we use tooltips to describe what the item does.
            imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), Show("inventory_button"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
            if player.element and (player.element==item.element): #indicate the selected gun
                add "gui/selected.png" xpos x ypos y anchor(.5,.5)
        $ i += 1
        if len(inventory.items)>9:
            textbutton _("Next Page") action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")] xpos .475 ypos .83

Re: Inventory Screen

Posted: Wed Jul 08, 2015 1:58 am
by Xerofit51
hello leon! for some reason I keep getting global name inventory is not defined despite it is under the start label inventory = Inventory() can you tell me whats wrong I didn't edit anything else

Re: Inventory Screen

Posted: Wed Jul 15, 2015 3:24 pm
by Discostar
Roxie wrote:Hi Leon,

So I couldn't get it to work with my code, so to test it I tried it using yours purely. Still not working right as it's not selecting anything. Here's the code, maybe I'm missing something?
Roxie- If you look over your code and compare it with Leon's, you'll notice that some things are missing. Specifically with the classes you're defining in the script.rpy-

Double check your classes Item(store.object), Inventory(store.object) ...etc. you're not defining things like "cost" and "money".

Secondly, the download I got was missing a couple of item pngs. 3 items. The box, Mr. Puffy and Puffy Set. You'll have to replace those images on your own.

Re: Inventory Screen

Posted: Wed Jul 15, 2015 6:15 pm
by Roxie
@Discostar - Thanks for the image removal.

Re: Inventory Screen

Posted: Thu Jul 16, 2015 4:18 pm
by Discostar
edit: I got the inventory to work.

@Roxie - np :)

Re: Inventory Screen

Posted: Sun Jul 26, 2015 4:51 am
by AsHLeX
Thanks for this!!

I have this wierd error that pops up. Basically, I copy pasted everything into a new game and when I run it I get this error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 162, in script
    with None
  File "game/script.rpy", line 79, in display_items_overlay
    inventory_show = "Money:" + str(inventory.money) + " HP: " + str(player.hp) + " bullets: " + str(player.mp) + " element: " + str(player.element) + "\nInventory: "
NameError: global name 'inventory' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00start.rpy", line 162, in script
    with None
  File "C:\Users\Yip\Downloads\renpy-6.99.4-sdk\renpy\ast.py", line 1236, in execute
    renpy.exports.with_statement(trans, paired)
  File "C:\Users\Yip\Downloads\renpy-6.99.4-sdk\renpy\exports.py", line 1251, in with_statement
    return renpy.game.interface.do_with(trans, paired, clear=clear)
  File "C:\Users\Yip\Downloads\renpy-6.99.4-sdk\renpy\display\core.py", line 1850, in do_with
    self.with_none()
  File "C:\Users\Yip\Downloads\renpy-6.99.4-sdk\renpy\display\core.py", line 1871, in with_none
    self.compute_overlay()
  File "C:\Users\Yip\Downloads\renpy-6.99.4-sdk\renpy\display\core.py", line 1966, in compute_overlay
    i()
  File "game/script.rpy", line 79, in display_items_overlay
    inventory_show = "Money:" + str(inventory.money) + " HP: " + str(player.hp) + " bullets: " + str(player.mp) + " element: " + str(player.element) + "\nInventory: "
NameError: global name 'inventory' is not defined

Windows-8-6.2.9200
Ren'Py 6.99.4.467
A Ren'Py Game 0.0
Funny thing is, the inventory object is already created.

Code: Select all

label start:
    python:
        player = Player("Derp", 100, 50)
        player.hp = 50
        player.mp = 10
        chocolate = Item("Chocolate", hp=40, image="gui/inv_chocolate.png")
        banana = Item("Banana", mp=20, image="gui/inv_banana.png")    
        gun = Item("Gun", element="bullets", image="gui/inv_gun.png", cost=7)
        laser = Item("Laser Gun", element="laser", image="gui/inv_laser.png")
        inventory = Inventory()
        #add items to the initial inventory:
        inventory.add(chocolate)
        inventory.add(chocolate)
        inventory.add(banana)
    scene sidewalk
    "Me" "Lalala~"
    show screen inventory_button
    "Look! An inventory button!"
    "Strange guy" "Hey! You wanna buy a gun?"
    "Me" "Okay"
    $inventory.buy(gun)
    "Me" "Nice"
    $inventory.add(laser)
    "You found a laser gun!"
    
    "The end."
    return
It appears when I launch the game, but if I ignore it, everything runs fine. Looks like I'm the only one with this error though :/

Re: Inventory Screen

Posted: Sun Jul 26, 2015 3:57 pm
by leon
The error with undefined global name 'inventory' was due to some obsolete code and changes to the recent version of Ren'Py. I re-uploaded the file with a fix. Download it again and it should work.