I'm testing out an Inventory system to implement in future projects.
But I couldn't get the information screen to work properly.
Problem:
I have the inventory screen setup but when I click one of the items,...
...the screen shows the description of all items.
Objective:
I need it to only show the description of the selected item.
Here's my script.rpy code. It containts the Python code of the Inventory system.
Code: Select all
# The script of the game goes in this file.
# Declare characters used by this game. The color argument colorizes the
# name of the character.
init python: #This means everything here is in python coding. I dont know how to do it yet...
class Inventory(): #This defines the functions for Inventory.
def __init__(self, items, no_of_items):
self.items = items
self.no_of_items = no_of_items
def add_item(self, item): #Adding function for item.
self.items.append(item)
self.no_of_items += 1
def remove_item(self, item): #Removing item.
self.items.remove(item)
self.no_of_items -= 1
def list_items(self): #Command that causes the character to list the items they carry.
if len(self.items) < 1:
e("I'm not carrying anything.")
else:
e("Here's what I have...")
for item in self.items:
e(f"{item.name}.")
e(f"{item.description}")
class InventoryItem(): #The object class of the item. It has a name and a description.
def __init__(self, name, description):
self.name = name
self.description = description
define e = Character("Eileen")
# The game starts here.
label start:
default inventory = Inventory([], 0) #The inventory variable as a list to store items.
#The items as variables. Each item has a name and a description.
define banana = InventoryItem("Banana", "Nature's energybar. Monkeys love it.")
define car_key = InventoryItem("Car Keys", "Keys to a Toyoto Accord.")
scene bg room
show eileen happy
$ inventory.list_items()
show screen bag_button
e "Oh Whats this?"
$ inventory.add_item(banana)
$ inventory.add_item(car_key)
e "Who left this here?"
e "Oh a monkey!"
e "Hey let go!!"
$ inventory.remove_item(banana)
$ inventory.remove_item(car_key)
e "NO!!"
$ inventory.list_items()
# This ends the game.
return
Here's the myscreens.rpy code.
Code: Select all
screen bag_button(): #Bag button. Let's the player access the inventory screen.
modal False
frame:
xalign 0.05
yalign 0.14
background "#242424"
xsize 125
ysize 70
textbutton "Bag":
xalign 0.05
yalign 0.14
xsize 100
ysize 60
action ToggleScreen("inventory"), Hide("bag_button")
screen inventory(): #The inventory screen.
modal True
label "Inventory":
xalign 0.2
yalign 0.0
frame:
xalign 0.16
yalign 0.2
background "#242424"
xsize 630
ysize 650
vbox:
pos 0.12, 0.20
for item in inventory.items:
textbutton "[item.name]":
action Show("item_info")
textbutton "Close": # Close command for inventory.
xalign 0.39
yalign 0.08
action ToggleScreen("inventory"), Hide("item_info"), Show("bag_button")
default item_description = ""
screen item_info(): #Screen for the item's description.
window:
xalign 0.7
yalign 0.2
background "#ff8000"
xsize 630
ysize 650
text item_description:
xfill
yfill
vbox:
xalign 0.7
yalign 0.2
for item in inventory.items:
textbutton item.description:
yalign 0.3
action SetVariable(item_description, item.description)
selected False
Have a nice day!
Sources of the code:
The codes were based on the inventory tutorial by Coding with B and E from Youtube while the screens are based on insipid's inventory code from reddit.
Links:
Coding with B and E: https://www.youtube.com/watch?v=237jY-KtbVA
Insipid's code: https://www.reddit.com/r/RenPy/comments ... ry_system/