I Need Help with an Item Description Screen.[SOLVED]

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
eddywardster
Newbie
Posts: 2
Joined: Sat Dec 03, 2022 6:46 am
Deviantart: eddywardster
Contact:

I Need Help with an Item Description Screen.[SOLVED]

#1 Post by eddywardster »

Hello,
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,...
Image

Image
...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

I await for any help and solutions you guys can offer and thank you for giving this post a read.
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/
Last edited by eddywardster on Tue Dec 06, 2022 1:49 pm, edited 1 time in total.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: I Need Help with an Item Description Screen.

#2 Post by enaielei »

The item_info screen contains a for loop that goes through all the items in the inventory that's why it's showing all the descriptions.
You need to pass the item to the item_info screen to display only a certain item.

Code: Select all

# Add item parameter to the item_info screen, the passed item will be contained in this variable.
screen item_info(item): #Screen for the item's description.
    window:
        xalign 0.7
        yalign 0.2
        background "#ff8000"
        xsize 630
        ysize 650
        # Use the description property of the passed item to display it.
        text item.description:
            xfill True
            yfill True
Then when showing the item_info screen just pass the item.

Code: Select all

screen inventory(): #The inventory screen.
    ...
    vbox:
        pos 0.12, 0.20
        for item in inventory.items:
            textbutton "[item.name]":
                # Show action allows you to pass the item object into the item_info screen.
                action Show("item_info", item=item)
    ...

eddywardster
Newbie
Posts: 2
Joined: Sat Dec 03, 2022 6:46 am
Deviantart: eddywardster
Contact:

Re: I Need Help with an Item Description Screen.

#3 Post by eddywardster »

Image


Thank you so much, enaielei! It works like charm!

Post Reply

Who is online

Users browsing this forum: Google [Bot]