Inventory Screen
Forum rules
Do not post questions here!
This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Do not post questions here!
This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Re: Inventory Screen
Hello, for now I advanced. I had to use the 150x150 item pics. Not my upscaled 300x300. And made the bg for my test not transparent. Then zoom them from 0.0 to 0.66 change xanchor 0.5 to 0.0 in the lower part of the code. This is for align all three. On the upper 1st part I changed $ x = 515 to 717 and $y = 25 to 64 (Top left item pos). Then 2nd pic coordinates, one colum lower, are changed from x+=190 to x+= 268. Next time I fill up the inventory to see how the other rows look+the "Next page" button then. Also, I read on the forum that one item has to stay permamently (non consumable) in the inventory. Because when all items are used up, the screen will not close when I click again on my inventory open/close imagebutton. I also put the information+description text to the right place. For the big inventory bg it was necessary to scale up to game resolution 1920x1080. Anyone has some suggestions what else can be done later, like pick up items with mouse from screen to inventory?
Re: Inventory Screen
EDIT: Just noticed today, creating images, I use the 300x300 resolution for the item images. Not the standard 150x150. This means my code is optimized for 300x300.
Hello, I changed now two variables to made the horizontal spacing between the items fit. Changed: $ y += 170 to 240. in this 2nd part:
for item in sorted_items:
if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
$ x += 268
if i%3==0:
$ y += 240
$ x = 620
$ 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_screen"), SetVariable("item", item), Hide
("inventory_screen"), item_use] hovered [ Play ("sound", "sound/klick002.ogg"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=975) ]
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(.0,.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
And I changed bolow his, the "SELECTED" pic to align more to the right from: add "gui/selected.png" xpos x ypos y anchor(.5,.5)
to zero: add "gui/selected.png" xpos x ypos y anchor(.0,.5)
And I changed in: zoom 0.66 xanchor +0.0 yanchor 0.5 to: zoom 0.66 xanchor +0.0 yanchor 0.66:
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.66 xanchor +0.0 yanchor 0.66
on idle:
linear 0.2 alpha 1.0
on hover:
linear 0.2 alpha 2.5
I pre- add a "Non consumable" item (laser) that stay always in the inventory. So I get no more freeze when all items where consumed and I want to
close the screen.
Now next problem, I filled up the inventory to over 9 items to test the "NEXT PAGE" function and it is working.
But when all items of the "next page" are consumed, the inventory do not jump back to the first screen with the rest items.
The emptyied inventory is shown. I can close it. When open again, the remaining items are still missing.
What I have to change? Some variable containing the "9" (number of items showed for each screen). Or has some more coding to be add? Telling the
script to jump or hide the 2nd, now empty screen? Or reload. Then showing what the rest 9 items? Maybe let the textbutton "next page" always appear, so can browse back?
Here is the changed code for the 1920x1080 resolution:
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
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="", cost=0):
self.name = name
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
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.hp>0: #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
inventory.drop(self) # consumable item - drop after use
elif self.mp>0: #mp restore item
player.mp = player.mp+self.mp
if player.mp > player.max_mp: # can't increase MP beyond max MP
player.mp = player.max_mp
inventory.drop(self) # consumable item - drop after use
else:
player.element=self.element #item to change elemental damage; we don't drop it, since it's not a consumable item
class Inventory(store.object):
def __init__(self, money=10):
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 buy(self, item):
if self.money >= item.cost:
self.items.append(item)
self.money -= item.cost
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/frameInv.png",205,205)
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 = "Money:" + str(inventory.money) + " 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.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
#use battle_frame(char=player, position=(.97,.20)) # we show characters stats (mp, hp) on the inv. screen
#use battle_frame(char=dog, position=(.97,.50))
#hbox align (.95,.04) spacing 20:
#textbutton "Close Inventory"
#action [ Hide("inventory_screen")]
#Imagebutton for hide inventory:
imagebutton idle "HUDINV_idle001.png" hover "HUDINV_hover001.png" xpos 0.933 ypos 0.417 action [Hide("inventory_screen")]
$ x = 715 # coordinates of the top left item position
$ y = 64
$ i = 0
$ 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
for item in sorted_items:
if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
$ x += 268
if i%3==0:
$ y += 240
$ x = 620
$ 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_screen"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Play ("sound", "sound/klick002.ogg"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=975) ] 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(.0,.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
screen gui_tooltip (my_picture="", my_tt_xpos=58, my_tt_ypos=1000):
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.66 xanchor +0.0 yanchor 0.66
on idle:
linear 0.2 alpha 1.0
on 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"))
And here the after "label start" filled up inventory code:
label start:
init -1 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(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(laser)
inventory.add(banana)
EDIT: I put this under the similar one, seems to work fine (for now): $ i += 0
if len(inventory.items)<9:
textbutton _("Next Page") action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")] xpos .475 ypos .83 EDIT
Hello, I changed now two variables to made the horizontal spacing between the items fit. Changed: $ y += 170 to 240. in this 2nd part:
for item in sorted_items:
if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
$ x += 268
if i%3==0:
$ y += 240
$ x = 620
$ 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_screen"), SetVariable("item", item), Hide
("inventory_screen"), item_use] hovered [ Play ("sound", "sound/klick002.ogg"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=975) ]
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(.0,.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
And I changed bolow his, the "SELECTED" pic to align more to the right from: add "gui/selected.png" xpos x ypos y anchor(.5,.5)
to zero: add "gui/selected.png" xpos x ypos y anchor(.0,.5)
And I changed in: zoom 0.66 xanchor +0.0 yanchor 0.5 to: zoom 0.66 xanchor +0.0 yanchor 0.66:
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.66 xanchor +0.0 yanchor 0.66
on idle:
linear 0.2 alpha 1.0
on hover:
linear 0.2 alpha 2.5
I pre- add a "Non consumable" item (laser) that stay always in the inventory. So I get no more freeze when all items where consumed and I want to
close the screen.
Now next problem, I filled up the inventory to over 9 items to test the "NEXT PAGE" function and it is working.
But when all items of the "next page" are consumed, the inventory do not jump back to the first screen with the rest items.
The emptyied inventory is shown. I can close it. When open again, the remaining items are still missing.
What I have to change? Some variable containing the "9" (number of items showed for each screen). Or has some more coding to be add? Telling the
script to jump or hide the 2nd, now empty screen? Or reload. Then showing what the rest 9 items? Maybe let the textbutton "next page" always appear, so can browse back?
Here is the changed code for the 1920x1080 resolution:
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
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="", cost=0):
self.name = name
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
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.hp>0: #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
inventory.drop(self) # consumable item - drop after use
elif self.mp>0: #mp restore item
player.mp = player.mp+self.mp
if player.mp > player.max_mp: # can't increase MP beyond max MP
player.mp = player.max_mp
inventory.drop(self) # consumable item - drop after use
else:
player.element=self.element #item to change elemental damage; we don't drop it, since it's not a consumable item
class Inventory(store.object):
def __init__(self, money=10):
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 buy(self, item):
if self.money >= item.cost:
self.items.append(item)
self.money -= item.cost
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/frameInv.png",205,205)
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 = "Money:" + str(inventory.money) + " 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.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
#use battle_frame(char=player, position=(.97,.20)) # we show characters stats (mp, hp) on the inv. screen
#use battle_frame(char=dog, position=(.97,.50))
#hbox align (.95,.04) spacing 20:
#textbutton "Close Inventory"
#action [ Hide("inventory_screen")]
#Imagebutton for hide inventory:
imagebutton idle "HUDINV_idle001.png" hover "HUDINV_hover001.png" xpos 0.933 ypos 0.417 action [Hide("inventory_screen")]
$ x = 715 # coordinates of the top left item position
$ y = 64
$ i = 0
$ 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
for item in sorted_items:
if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
$ x += 268
if i%3==0:
$ y += 240
$ x = 620
$ 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_screen"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Play ("sound", "sound/klick002.ogg"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=975) ] 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(.0,.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
screen gui_tooltip (my_picture="", my_tt_xpos=58, my_tt_ypos=1000):
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.66 xanchor +0.0 yanchor 0.66
on idle:
linear 0.2 alpha 1.0
on 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"))
And here the after "label start" filled up inventory code:
label start:
init -1 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(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(chocolate)
inventory.add(laser)
inventory.add(banana)
EDIT: I put this under the similar one, seems to work fine (for now): $ i += 0
if len(inventory.items)<9:
textbutton _("Next Page") action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")] xpos .475 ypos .83 EDIT
- Isolongitudinal
- Newbie
- Posts: 1
- Joined: Wed Dec 26, 2018 8:47 pm
- Contact:
Re: Inventory Screen
Thank you for this. Just beginning to get into this & have a lot of ideas, including the inventory, I'm completely clueless how to do. Having a working example to learn from & build off of is extraordinarily valuable.
Re: Inventory Screen
Need help.
Using this code works well. But when i want to remove an item with "$ inventory.drop(specialoil)" this crash message comes:
Adding it and others is no problem. But why droping do not work?
Using this code works well. But when i want to remove an item with "$ inventory.drop(specialoil)" this crash message comes:
Code: Select all
I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/script.rpy", line 5026, in script
$ inventory.drop(specialoil)
File "game/script.rpy", line 5026, in <module>
$ inventory.drop(specialoil)
File "game/script.rpy", line 165, in drop
self.items.remove(item)
ValueError: list.remove(x): x not in list
-- Full Traceback ------------------------------------------------------------
Full traceback:
File "game/script.rpy", line 5026, in script
$ inventory.drop(specialoil)
File "C:\Users\keinschwein\Desktop\renpy-7.0.0-sdk\renpy\ast.py", line 862, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\Users\keinschwein\Desktop\renpy-7.0.0-sdk\renpy\python.py", line 1912, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 5026, in <module>
$ inventory.drop(specialoil)
File "game/script.rpy", line 165, in drop
self.items.remove(item)
File "C:\Users\keinschwein\Desktop\renpy-7.0.0-sdk\renpy\python.py", line 713, in do_mutation
return method(self, *args, **kwargs)
ValueError: list.remove(x): x not in list
Windows-7-6.1.7601-SP1
Ren'Py 7.0.0.196
Re: Inventory Screen
I found out why the above error shows up = because the "specialoil" and other items is gone after I load the save game. So there is nothing to remove anymore.
The starting item stays in the inventory, I also put the "Inventory = Inventory()" right before the item list and after the "label start:" here part of this code:
In game all working well, and show up in inventory when obtained (buy at the shop. add with "$ inventory.add(specialoil)" but when restart and reload a savegame items are gone.
This bug was mentioned by two people in this thread before me. And still no solution to this? I am new to Renpy, and want knew what is wrong here. Who can help please?
The starting item stays in the inventory, I also put the "Inventory = Inventory()" right before the item list and after the "label start:" here part of this code:
Etc.init -1 python:
inventory = Inventory()
startingitem = Item("Space suit", keyitem="Ssuit_Elie", image="gui/inv_Ssuit_Elie.png")
chocolate = Item("Chocolate", image="gui/inv_chocolate.png")
banana = Item("Banana", image="gui/inv_banana.png")
specialoil = Item("Special Oil", image="gui/inv_Special_Oil.png")
In game all working well, and show up in inventory when obtained (buy at the shop. add with "$ inventory.add(specialoil)" but when restart and reload a savegame items are gone.
This bug was mentioned by two people in this thread before me. And still no solution to this? I am new to Renpy, and want knew what is wrong here. Who can help please?
Re: Inventory Screen
To "ourdecemberdreams":
viewtopic.php?p=487926#p487926
I put after the "label start:" the "init -1 python:" and the bug mentioned by you "NameError: name 'inventory' is not defined" allthrough you defined it do no show up.
You have " python:" right after the "label start:" and in the inventory code before "label start:" you have "init -1 python:". So I think it has be the same, both "init -1 python:". This works then for me, but as I asked for help, the gained items in game will vanish after load a savegame to continue play. The demo by "Leon" who made this code has: "init -1 python:" and after the "label start:" the " python:" and no "inventory = Inventory()" and save/load is working. Also has starting items. Strange, what we have done wrong?
viewtopic.php?p=487926#p487926
I put after the "label start:" the "init -1 python:" and the bug mentioned by you "NameError: name 'inventory' is not defined" allthrough you defined it do no show up.
You have " python:" right after the "label start:" and in the inventory code before "label start:" you have "init -1 python:". So I think it has be the same, both "init -1 python:". This works then for me, but as I asked for help, the gained items in game will vanish after load a savegame to continue play. The demo by "Leon" who made this code has: "init -1 python:" and after the "label start:" the " python:" and no "inventory = Inventory()" and save/load is working. Also has starting items. Strange, what we have done wrong?
-
- Newbie
- Posts: 23
- Joined: Sun Feb 25, 2018 11:39 pm
- Contact:
Re: Inventory Screen
Hello guys. First of all thanks for the code, secondly I'm sorry to bother you with this but I wrote the code this way:
and
But I get this error:
Can you please help me with this?
Code: Select all
init 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
item = None
class Player(renpy.store.object):
def __init__(self, name, consumable=None, permanent=None):
self.name=name
self.consumable=consumable
self.permanent=permanent
player = Player("Ryan", 100, 50)
class Item(store.object):
def __init__(self, name, player=None, consumable="", permanent="", image="", cost=0):
self.name = name
self.player=player # which character can use this item?
self.image=image # image file to use for this item
self.cost=cost # how much does it cost in shops?
self.consumable=consumable # is this item consumable?
self.permanent=permanent # is this item permanent?
def use(self): #here we define what should happen when we use the item
if self.consumable>0:
inventory.drop(self) # consumable item - drop after use
else:
player.permanent=self.permanent #we don't drop it, since it's not a consumable item
class Inventory(store.object):
def __init__(self, money=0):
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 buy(self, item):
if self.money >= item.cost:
self.items.append(item)
self.money -= item.cost
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("New_inventory/frame.png",25,25)
style.button.yminimum=52
style.button.xminimum=52
style.button_text.color="000"
showitems = False #turn True to debug the inventory
init 1 screen inventory_screen:
add "New_inventory/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") ]#, Return(None)]
$ x = 515 # coordinates of the top left item position
$ y = 25
$ i = 0
$ sorted_items = sorted(inventory.items, key=attrgetter("permanent"), reverse=True) # we sort the items, so non-consumable items are listed first
$ 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("New_inventory/inv_", "").replace(".png", "") # we use tooltips to describe what the item does.
imagebutton idle pic hover pic xpos x ypos y action NullAction() hovered [ Play ("sound", "New_inventory/sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [ Hide("gui_tooltip") ] at inv_eff
if player.permanent and (player.permanent==item.permanent): #indicate the selected item
add "New_inventory/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
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:
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
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 bar.", style="tips_bottom"))
image tooltip_inventory_giftcard=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A hardnLong Gift Card.", style="tips_bottom"))
image tooltip_inventory_flowers=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("An bouquet of flowers.", style="tips_bottom"))
image tooltip_inventory_melatonin=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("Melatoninthe, the strongest over the counter sleep aid that I can buy.\nIt's all natural...non addictive...", style="tips_bottom"))
image tooltip_inventory_spycam=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A webcam.", style="tips_bottom"))
Code: Select all
label start:
# This defines the items.
python:
player = Player("Ryan", 100, 50)
chocolate = Item("Chocolate", consumable="chocolate", image="New_inventory/inv_chocolate.png", cost=9)
giftcard = Item("Gift Card", consumable="giftcard", image="New_inventory/inv_giftcard.png", cost=24)
flowers = Item("Flowers", consumable="flowers", image="New_inventory/inv_flowers.png", cost=49)
melatonin = Item("Melatonin", consumable="melatonin", image="New_inventory/inv_melatonin.png", cost=99)
spycam = Item("Spy-cam", consumable="spycam", image="New_inventory/inv_spycam.png", cost=1000)
inventory = Inventory()
#add items to the initial inventory:
inventory.add(chocolate)
inventory.add(chocolate)
Code: Select all
I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/script.rpy", line 512, in script
call screen ryanmapnight
File "renpy/common/000statements.rpy", line 519, in execute_call_screen
store._return = renpy.call_screen(name, *args, **kwargs)
File "game/New_inventory/New_inventory.rpy", line 66, in execute
init 1 screen inventory_screen:
File "game/New_inventory/New_inventory.rpy", line 66, in execute
init 1 screen inventory_screen:
File "game/New_inventory/New_inventory.rpy", line 74, in execute
$ sorted_items = sorted(inventory.items, key=attrgetter("permanent"), reverse=True) # we sort the items, so non-consumable items are listed first
File "game/New_inventory/New_inventory.rpy", line 74, in <module>
$ sorted_items = sorted(inventory.items, key=attrgetter("permanent"), reverse=True) # we sort the items, so non-consumable items are listed first
AttributeError: 'RevertableList' object has no attribute 'items'
-- Full Traceback ------------------------------------------------------------
Re: Inventory Screen
Hello, not sure, not remember if I have had this bug too.
Put: "inventory = Inventory()" just after "label start:" and your " python:" line, this will made Renpy recognize your following items list. But soon you maybe get the bug we have, mentioned above. Items will not persist when game is restarted and a savegame is load. What about to put all items as "starting.item" (because starting items seems to survive the save bug) and then remove them in game. And after this adding them again during gameplay as intented? I am a beginer in this. We wait for help from the autor "leon" or a professional coder. Who can help with this?
Put: "inventory = Inventory()" just after "label start:" and your " python:" line, this will made Renpy recognize your following items list. But soon you maybe get the bug we have, mentioned above. Items will not persist when game is restarted and a savegame is load. What about to put all items as "starting.item" (because starting items seems to survive the save bug) and then remove them in game. And after this adding them again during gameplay as intented? I am a beginer in this. We wait for help from the autor "leon" or a professional coder. Who can help with this?
-
- Newbie
- Posts: 23
- Joined: Sun Feb 25, 2018 11:39 pm
- Contact:
Re: Inventory Screen
Thank you @M-77 for your help, I figured it out. Now I'm trying to show how much quantity of that specific item you have because I don't want to populate the entire inventory with images of the same image
I tried using tooltip to show by using the flowing code:
but the var futchocolate always shows as 0 and not the right amount.
I even used persistent vars.
Does anyone know how tot do that?
I tried using tooltip to show by using the flowing code:
Code: Select all
image tooltip_inventory_chocolate=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("Generic chocolate bar.\nQuantity: [futchocolate]", style="tips_bottom"))
I even used persistent vars.
Does anyone know how tot do that?
-
- Regular
- Posts: 33
- Joined: Sat May 04, 2019 8:09 am
- Contact:
Re: Inventory Screen
Hi team,
I've been integrating this with my game and it seems to be working okay with the cosmetic tweaks I've given it, but there's a couple of bits I can't seem to get right.
I added in two money counters to the same screen so it covers everything, but I can't seem to change their appearance (font, color, size) or the appearance of the 'Next Page' textbutton. Is there some way I should be applying a style to them that I'm getting wrong here? I just want them to look pretty much the same as what I've set for the tooltip_inventory info.
I also have a problem in that the text of the tooltip_inventory section runs off the screen so I figure there's something I need to adjust to limit the horizontal space that information can take up, but I can't figure out which bit to adjust to make that difference.
Finally, is there a way to add a confirmation to using items? At the moment once clicked on they just are applied and I'd like to be able to do a 'Are your sure?' and 'It's been done now'-type text to the use of items.
Thanks for your help!
I've been integrating this with my game and it seems to be working okay with the cosmetic tweaks I've given it, but there's a couple of bits I can't seem to get right.
I added in two money counters to the same screen so it covers everything, but I can't seem to change their appearance (font, color, size) or the appearance of the 'Next Page' textbutton. Is there some way I should be applying a style to them that I'm getting wrong here? I just want them to look pretty much the same as what I've set for the tooltip_inventory info.
Code: Select all
screen inventory_screen:
text "Jackpot Funds: $[jackpotfunds]" xpos 0.06 ypos 0.21
text "Hellfire Club Funds: $[hfcfunds]" xpos 0.06 ypos 0.25
add "phoneivn" at truecenter # the background
modal True #prevent clicking on other stuff when inventory is shown
#use battle_frame(char=player, position=(.97,.20)) # we show characters stats (mp, hp) on the inv. screen
#use battle_frame(char=dog, position=(.97,.50))
hbox align (.95,.04) spacing 20:
textbutton "Close Phone" action [ Hide("inventory_screen"), Show("phone_button"), Return(None)]
$ x = 0 # coordinates of the top left item position
$ y = 300
$ i = 0
$ sorted_items = sorted(inventory.items, key=attrgetter('name')) # 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)/5):
$ next_inv_page = 0
for item in sorted_items:
if i+1 <= (inv_page+1)*5 and i+1>inv_page*5:
$ x += 330
if i%5==0:
$ y += 170
$ x = 275 #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("phone_button"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=690) ] 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)>5:
textbutton _("Next Page") action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")] xpos .07 ypos .80
screen gui_tooltip (my_picture="", my_tt_xpos=127, my_tt_ypos=682):
add my_picture xpos my_tt_xpos ypos my_tt_ypos
Code: Select all
style.tips_bottom = Style(style.tips_top)
style.tips_bottom.font="Future World.ttf"
style.tips_bottom.size=28
style.tips_bottom.outlines=[(0, "83a8fc", 1, 1)]
style.tips_bottom.kerning = 2
Finally, is there a way to add a confirmation to using items? At the moment once clicked on they just are applied and I'd like to be able to do a 'Are your sure?' and 'It's been done now'-type text to the use of items.
Thanks for your help!
Re: Inventory Screen
Hello "HammeredEnt" to make the description of the item line breake, you have to edit this part:
image information = Text("INFORMATION", style="tips_top")
#Tooltips-inventory:
image tooltip_inventory_gemblue=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A blue gem.", style="tips_bottom"))
image tooltip_inventory_gemred=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A red gem.", style="tips_bottom"))
image tooltip_inventory_gemgreen=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A green gem.", style="tips_bottom"))
image tooltip_inventory_book1=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A pretty looking book.", style="tips_bottom"))
image tooltip_inventory_book2=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A pretty looking book.", style="tips_bottom"))
You have to change the "(3,30)" coordinates, or one of the other 3 (3,0), (665, 73) - I forgot wich it was, but I have the same issue months ago and it was because of this part. So it is no big problem to find out. But can you please help me how to get the Items stay in the inventory after starting the game new/and load a save game? I still not could find out, it has something to do with "Rollback" function. Do I need to shut off Rollback during the scene when a item is supposed to be used? Did your items persist? I wrote above about this issue, but no one answered me yet.
image information = Text("INFORMATION", style="tips_top")
#Tooltips-inventory:
image tooltip_inventory_gemblue=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A blue gem.", style="tips_bottom"))
image tooltip_inventory_gemred=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A red gem.", style="tips_bottom"))
image tooltip_inventory_gemgreen=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A green gem.", style="tips_bottom"))
image tooltip_inventory_book1=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A pretty looking book.", style="tips_bottom"))
image tooltip_inventory_book2=LiveComposite((665, 73), (3,0), ImageReference("information"), (3,30), Text("A pretty looking book.", style="tips_bottom"))
You have to change the "(3,30)" coordinates, or one of the other 3 (3,0), (665, 73) - I forgot wich it was, but I have the same issue months ago and it was because of this part. So it is no big problem to find out. But can you please help me how to get the Items stay in the inventory after starting the game new/and load a save game? I still not could find out, it has something to do with "Rollback" function. Do I need to shut off Rollback during the scene when a item is supposed to be used? Did your items persist? I wrote above about this issue, but no one answered me yet.
Re: Inventory Screen
The bug about items disapearing from the inventory is solved, thank to help from the: https://f95zone.to/threads/doubt-with-r ... 518/page-2 Members. After the "laber start:" You have to remmove the "init -1" from above the inventory list. (the "init -1" in the whole inventory code before the "label start:" stays as it is.) Add "default" to all your positions of items in the list, and a "default inventory = inventory()" line too:
I also got a bug report after doing this because I used the word "Inventory" for the screen button (before "label start:") to open/close it. So I changed it to "Inventar" and no cross call now:
label start:
default inventory = Inventory()
default rosa = Item("Rosa", element="rosa", image="images/shop/items/Rosa_idle.png")
default camerareflex = Item("camerareflex", element="camerareflex", image="shop/items/CameraReflex_idle.png")
#etc...
This change makes the items stay. Also "Rollback" will work now.default inventory = Inventory()
default rosa = Item("Rosa", element="rosa", image="images/shop/items/Rosa_idle.png")
default camerareflex = Item("camerareflex", element="camerareflex", image="shop/items/CameraReflex_idle.png")
#etc...
I also got a bug report after doing this because I used the word "Inventory" for the screen button (before "label start:") to open/close it. So I changed it to "Inventar" and no cross call now:
#Inventory icon
init python:
showinv= False
Inventar = 0
max_inv= 1
Thanks to all!init python:
showinv= False
Inventar = 0
max_inv= 1
-
- Regular
- Posts: 33
- Joined: Sat May 04, 2019 8:09 am
- Contact:
Re: Inventory Screen
Ah-ha! Brilliant, fixed that now - thanks so much for your help with that! Afraid I don't know anything about the rest of your issue (I'm a total newbie at all this stuff) but it looks like you managed to figure it out yourself.
Re: Inventory Screen
Is there a possibility to add multiple same objects to this inventory and select only one of them?
Re: Inventory Screen
Hello Oduvan, I do not know. I not use the inventory that much in my project. Just to show what the player get or buy or lost again. I also use True/False switches in the script to made an event start if player got the corresponding item and.
Who is online
Users browsing this forum: No registered users