Inventory: define items each chapter

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
Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Inventory: define items each chapter

#1 Post by Fatimah »

Hello,

I'm using leon's code as a base for my inventory.
However, I'm facing some issues.
My game is divided in chapters and I would like to initiate each chapter's items alone.
The way the code works now is that the items are initialized when the game first load, and so I have to define all the items at that time.

Code: Select all

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

    #Tooltips-inventory:
    image tooltip_inventory_Silverweed=LiveComposite((300, 300), (0,0), ImageReference("silverweedView"),(5,300), Text("عشبة ذات زهرة صفراء جميلة.", style="tips_bottom"))
    image tooltip_inventory_Tetterwort=LiveComposite((300, 300), (0,0), ImageReference("tetterwortView"),(5,300), Text("رائحة هذه العشبة تشبه رائحة السمك.", style="tips_bottom"))
    image tooltip_inventory_Chasteberry=LiveComposite((300, 300), (0,0), ImageReference("chasteberryView"),(5,300), Text("عشبة يابسة ذات شكل غريب.", style="tips_bottom"))
    image tooltip_inventory_Jequirity=LiveComposite((300, 300), (0,0), ImageReference("jequirityView"), (50,300), Text("بذرةٌ حمراء اللون.", style="tips_bottom"))
    image tooltip_inventory_LadysMantles=LiveComposite((300, 300), (0,0), ImageReference("ladysmantlesView"), (5,300), Text("عشبةٌ بأوراقٍ مسننة و أزهارٍ صغيرة.", style="tips_bottom"))
    image tooltip_inventory_RedRobin=LiveComposite((300, 300), (0,0), ImageReference("redrobinView"), (5,300), Text("عشبةٌ بساقٍ حمراء و أوراقٍ مثلثة و زهرةٍ بنفسجية.", style="tips_bottom"))
    image tooltip_inventory_Medicine=LiveComposite((300, 300), (0,0), ImageReference("medicineView"), (50,300), Text("الدواء الذي أعطتني إياه الساحرة.رائحته كريهة و لونه لا يشجع على شربه.", style="tips_bottom"))
    image tooltip_inventory_note=LiveComposite((300, 300), (0,0), ImageReference("crumbledNoteView"),(5,300), Text("القصاصة التي أعطتنا إياها الطبيبة سمر.", style="tips_bottom"))


Is there a way for the items to be initialized at the start of a chapter?
Hoping I explained my problem clearly.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Inventory: define items each chapter

#2 Post by Fatimah »

I tried putting this statement inside an init block at the start of the second chapter file, but this doesn't work.
I then tried putting with the other image statements that I have in this chapter, but still doesn't work.

Code: Select all

image tooltip_inventory_note=LiveComposite((300, 300), (0,0), ImageReference("crumbledNoteView"),(5,300), Text("القصاصة التي أعطتنا إياها الطبيبة سمر.", style="tips_bottom"))
The description still doesn't show up in the inventory screen.
inventory.PNG

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inventory: define items each chapter

#3 Post by Alex »

Loks like you trying to re-create the tooltip_inventory_note... This might be, that init block for your next chapter was run first and then was run the code you have for all your items (is this code placed in script.rpy?).
Anyway, try to create completely different items in chapters (not re-create already existed items).

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Inventory: define items each chapter

#4 Post by Fatimah »

Alex wrote: Tue Dec 25, 2018 9:41 am Loks like you trying to re-create the tooltip_inventory_note... This might be, that init block for your next chapter was run first and then was run the code you have for all your items (is this code placed in script.rpy?).
Anyway, try to create completely different items in chapters (not re-create already existed items).
I'm not recreating anything. The note is a new item in the second chapter, but somehow I can't get it to work.
Does init block run when the game load from a saving point?

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inventory: define items each chapter

#5 Post by Alex »

Yes, all init blocks are run before loading data from save.
It's not clear that note in second chapter is another item, 'cause in both of your posts you show code for the tooltip_inventory_note. If this is two different named items it doesn't matter where you put code for them in the same script file or in different ones.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Inventory: define items each chapter

#6 Post by Fatimah »

Alex wrote: Thu Dec 27, 2018 7:03 am Yes, all init blocks are run before loading data from save.
It's not clear that note in second chapter is another item, 'cause in both of your posts you show code for the tooltip_inventory_note. If this is two different named items it doesn't matter where you put code for them in the same script file or in different ones.
I'm really sorry about the confusion, my bad.

So this is how my code is divided:

Code: Select all

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 the inventory screen
    item = None

    class Item(store.object):
        def __init__(self, name, usable, used, image=""):
            self.name = name
            self.image=image # image file to use for this item
            self.usable=usable
            self.used=used

        def use(self): #here we define what should happen when we use the item
            if self.usable == True:
                self.used = True
                inventory.drop(self)
                renpy.jump(self.name)

    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.tips_top.size=36
    style.tips_top.color="302725"

    style.tips_bottom = Style(style.tips_top)
    style.tips_top.size=36

    showitems = True #turn True to debug the inventory


screen inventory_button:
    imagebutton idle "backpack.png" hover "backpack.png" action [ Show("inventory_screen"), Hide("inventory_button")] align (.95,.04)

screen inventory_screen:
    zorder 3
    imagemap:
        ground "inventory_screen_idle.jpg"
        idle "inventory_screen_idle.jpg"
        hover "inventory_screen_hover.jpg"

        hotspot (239,578,132,62) action ShowMenu("save") activate_sound "click.mp3" hover_sound "click.mp3"
        hotspot (206,652,164,54) action ShowMenu("load") activate_sound "click.mp3" hover_sound "click.mp3"
        hotspot (219,722,152,55) action MainMenu() activate_sound "click.mp3" hover_sound "click.mp3"

        hotspot (944,255,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1117,255,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1290,255,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (944,412,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1117,412,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1290,412,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (944,569,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1117,569,162,145) action NullAction() hover_sound "click.mp3"
        hotspot (1290,569,162,145) action NullAction() hover_sound "click.mp3"

    modal True #prevent clicking on other stuff when inventory is shown
    hbox align (.95,.04) spacing 20:
        imagebutton idle "backpack.png" hover "backpack.png" action [ Hide("inventory_screen"), Show("inventory_button")]
    $ x = 1020 # coordinates of the top left item position
    $ y = 165
    $ i = 0
    $ sorted_items = sorted(inventory.items, key=attrgetter('name'), reverse=True) # we sort the items, so non-consumable items that change elemental damage (guns) are listed first
    $ next_inv_page = inv_page + 1
    $ previous_inv_page = inv_page - 1

    if next_inv_page > int(len(inventory.items)/9):
        $ next_inv_page = 0
    if previous_inv_page > int(len(inventory.items)/9):
        $ previous_inv_page = 0

    for item in sorted_items:
        if i+1 <= (inv_page+1)*9 and i+1>inv_page*9:
            $ x += 170
            if i%3==0:
                $ y += 160
                $ x = 1020
            $ pic = item.image
            $ my_tooltip = "tooltip_inventory_" + pic.replace("Icon.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", "click.mp3"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=250)] unhovered [Hide("gui_tooltip")] at inv_eff
#            if player.element and (player.element==item.element): #indicate the selected gun
#                add "selected.png" xpos x ypos y anchor(.5,.5)
        $ i += 1
        if len(inventory.items)>9:
            imagebutton idle "next_arrow.png" hover "next_arrow.png" xpos 1412 ypos 735 action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")]
            imagebutton idle "previous_arrow.png" hover "previous_arrow.png" xpos 965 ypos 735 action [SetVariable('inv_page', next_inv_page), Show("inventory_screen")]

screen gui_tooltip (my_picture="", my_tt_xpos=530, my_tt_ypos=250):
    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

    #Tooltips-inventory:
    image tooltip_inventory_Silverweed=LiveComposite((300, 300), (0,0), ImageReference("silverweedView"),(5,300), Text("عشبة ذات زهرة صفراء جميلة.", style="tips_bottom"))
    image tooltip_inventory_Tetterwort=LiveComposite((300, 300), (0,0), ImageReference("tetterwortView"),(5,300), Text("رائحة هذه العشبة تشبه رائحة السمك.", style="tips_bottom"))
    image tooltip_inventory_Chasteberry=LiveComposite((300, 300), (0,0), ImageReference("chasteberryView"),(5,300), Text("عشبة يابسة ذات شكل غريب.", style="tips_bottom"))
    image tooltip_inventory_Jequirity=LiveComposite((300, 300), (0,0), ImageReference("jequirityView"), (50,300), Text("بذرةٌ حمراء اللون.", style="tips_bottom"))
    image tooltip_inventory_LadysMantles=LiveComposite((300, 300), (0,0), ImageReference("ladysmantlesView"), (5,300), Text("عشبةٌ بأوراقٍ مسننة و أزهارٍ صغيرة.", style="tips_bottom"))
    image tooltip_inventory_RedRobin=LiveComposite((300, 300), (0,0), ImageReference("redrobinView"), (5,300), Text("عشبةٌ بساقٍ حمراء و أوراقٍ مثلثة و زهرةٍ بنفسجية.", style="tips_bottom"))
    image tooltip_inventory_Medicine=LiveComposite((300, 300), (0,0), ImageReference("medicineView"), (50,300), Text("الدواء الذي أعطتني إياه الساحرة.رائحته كريهة و لونه لا يشجع على شربه.", style="tips_bottom"))
This is my whole inventory code. It is placed before the start label.
I have another script file for the second chapter and I want to define new items.
However, the tool tips are not working & I have no idea why.

All of the items shown in the above code are defined in the first chapter and the code works fine with them.
It is when I define new items in the second chapter that I get the problem.

Code: Select all

image tooltip_inventory_crumbledNote=LiveComposite((300, 300), (0,0), ImageReference("crumbledNoteView"),(5,300), Text("القصاصة التي أعطتنا إياها الطبيبة سمر.", style="tips_bottom"))
Hopefully things are clearer now.
Thank you for all your help.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inventory: define items each chapter

#7 Post by Alex »

Hm, this should work as long as the player has this note-item in inventory. Did he get it somehow in second chapter?

For test purposes you can move the code for one of working items (like image tooltip_inventory_Silverweed=LiveComposite((300, 300), (0,0), ImageReference("silverweedView"),(5,300), Text("عشبة ذات زهرة صفراء جميلة.", style="tips_bottom"))) from script.rpy to second chapter script - this item should still work right.

Fatimah
Regular
Posts: 94
Joined: Tue Mar 01, 2016 2:53 pm
Contact:

Re: Inventory: define items each chapter

#8 Post by Fatimah »

Alex wrote: Sun Dec 30, 2018 6:09 am Hm, this should work as long as the player has this note-item in inventory. Did he get it somehow in second chapter?

For test purposes you can move the code for one of working items (like image tooltip_inventory_Silverweed=LiveComposite((300, 300), (0,0), ImageReference("silverweedView"),(5,300), Text("عشبة ذات زهرة صفراء جميلة.", style="tips_bottom"))) from script.rpy to second chapter script - this item should still work right.
I did as you suggested and moved the tool tips of the previous items to the script of the second chapter and it didn't work, which confuses me even further.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inventory: define items each chapter

#9 Post by Alex »

Could you send an archive of your project to let me run it and test?

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], simple_human