Problem with displaying images in imagebutton

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
Oduvan
Newbie
Posts: 8
Joined: Sun Jul 23, 2017 2:24 am
Contact:

Problem with displaying images in imagebutton

#1 Post by Oduvan »

Hello. Im new to coding and struggling at remake Elaine's inventory system with dictionaries (is it even possible?)
Have a dictionary with all items. For example:

Code: Select all

default itemz = {'sword': {"name": "sword",'img': img_path + "steel_sword_idle.png", "atk":10,"val":20},'helm': {"name": "helm",'img': img_path + "steel_helm_idle.png","atk": 0,"def": 10,"val": 20}}
Then i create screen for displaying items images in inventory:

Code: Select all

screen test_buttons():
        for item in itemz:
            imagebutton idle [item][img] action SetVariable("selected_item", item) xalign 0.5 yalign 0.5
But when i try to show this screen it gives me an error:

Code: Select all

Full traceback:
  File "game/script.rpy", line 39, in script
    
  File "F:\Renpy\renpy\ast.py", line 706, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "F:\Renpy\renpy\exports.py", line 1336, in say
    who(what, *args, **kwargs)
  File "F:\Renpy\renpy\character.py", line 1139, in __call__
    self.do_display(who, what, cb_args=self.cb_args, **display_args)
  File "F:\Renpy\renpy\character.py", line 842, in do_display
    **display_args)
  File "F:\Renpy\renpy\character.py", line 591, in display_say
    rv = renpy.ui.interact(mouse='say', type=type, roll_forward=roll_forward)
  File "F:\Renpy\renpy\ui.py", line 295, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "F:\Renpy\renpy\display\core.py", line 2699, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "F:\Renpy\renpy\display\core.py", line 3091, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "F:\Renpy\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "F:\Renpy\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "F:\Renpy\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "F:\Renpy\renpy\display\screen.py", line 430, in visit_all
    callback(self)
  File "F:\Renpy\renpy\display\core.py", line 3091, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "F:\Renpy\renpy\display\screen.py", line 440, in per_interact
    self.update()
  File "F:\Renpy\renpy\display\screen.py", line 625, in update
    self.screen.function(**self.scope)
  File "game/script.rpy", line 35, in execute
    imagebutton idle item.img hover item.img_hover selected_idle item.img_hover action SetVariable("selected_item", item) xalign 0.5 yalign 0.5
  File "game/script.rpy", line 35, in execute
    imagebutton idle item.img hover item.img_hover selected_idle item.img_hover action SetVariable("selected_item", item) xalign 0.5 yalign 0.5
  File "game/script.rpy", line 36, in execute
    #imagebutton idle [itemz[sword][img]] action SetVariable("selected_item", item) xalign 0.5 yalign 0.5
  File "game/script.rpy", line 37, in execute
    show screen test_buttons
  File "game/script.rpy", line 37, in keywords
    show screen test_buttons
  File "<screen language>", line 37, in <module>
NameError: name 'img' is not defined
So my question is how do i display an image from items in nested dictionary in imagebutton?

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Problem with displaying images in imagebutton

#2 Post by rayminator »

well you can try it this way
it gave you that error because of this [item][img]
you have to set the SetVariable to the actual item

Code: Select all

imagebutton idle im.Scale("your_location_image_is/steel_sword_idle.png", 129, 250) action SetVariable("selected_item", item) xalign 0.5 yalign 0.5

Oduvan
Newbie
Posts: 8
Joined: Sun Jul 23, 2017 2:24 am
Contact:

Re: Problem with displaying images in imagebutton

#3 Post by Oduvan »

Thanks for reply, but this not what i want to achieve.
I need items in inventory dictionary to automatically appear as buttons after i add them to inventory.
Then after click on them they become "selected_item" and can be equipped.
It was working with objects of class but there was a problem when i added few same objects to the inventory, they all become "selected" after click on one. So i try to remake it with dictionaries because complex objects like lists, tuples, dictionaries etc. id() would give a unique integer even if the content of those containers is same.

eezergoode
Regular
Posts: 58
Joined: Tue Aug 29, 2006 10:55 am
Contact:

Re: Problem with displaying images in imagebutton

#4 Post by eezergoode »

Well, without knowing a little more about what your end-goal is, I can only suggest a workaround.

First thing that comes to mind is use Elaine's inventory, but make a slight modification to make items stack-able... add an item count value to the items, and have the identical items all just use one inventory slot, but add a way to display how many of them you have. If you have 12 rusty swords, show one rusty sword with a small 12 in the corner of the imagebutton, OR show the count on the side when you hover over or select the item. If it is a consumable item, just add a small piece of code that instead of removing it from your inventory when used, if you have more than 1, subtract one from the count when used.If it the only one, then remove it from inventory.
Eezergoode - AKA Rabid Penguin
Team Lead

It does not matter how slowly you go so long as you do not stop.
Confucius
Chinese philosopher & reformer (551 BC - 479 BC)

Post Reply

Who is online

Users browsing this forum: No registered users