How to "hover "images/[items[i]].png""?

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
kafkaontheshore
Newbie
Posts: 22
Joined: Thu Apr 22, 2021 8:57 am
Contact:

How to "hover "images/[items[i]].png""?

#1 Post by kafkaontheshore »

Hey there!

Code: Select all

    ## Items ####################
    hbox xpos 40 ypos 1113 spacing 25:
      for i in range(len(items)):
        imagebutton: 
            action If(len(items)>=i, Jump('test'))
            idle "images/[items[i]].png"
            hover "images/[items[i]].png"
            insensitive "inventory insensitive.png"
            hovered Notify(items[i])
            unhovered Hide("notify")
results in:

Code: Select all

While running game code:
  File "game/script.rpy", line 25, in script
    call screen body
  File "renpy/common/000statements.rpy", line 609, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
Exception: In DynamicImage 'images/[items[[i]]].png': TypeError('list indices must be integers or slices, not str')
It works if do this instead: hover "images/[items[0]].png". But that would result in a lot of probably unnecessarally repetitive work. How to generalize the numbers?

Thanks for your time.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2446
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to "hover "images/[items[i]].png""?

#2 Post by Ocelot »

for i in range(len(container)): container is an antipattern. If you do that, it is almost certainly wrong. Iterate on elements instead. You can have python statements inside your loop to prebuild paths, extract data from containers, etc. and not rely on interpolation and such.

Code: Select all

for item in items:
    $ imagepath = "images/" + item
    imagebutton:
        action Jump('test') # your condition is always true anyway
        idle imagepath # no need to hover, since it is the same as idle
        insensitive "inventory insensitive.png"
        # do not use hovered/unhovered pair for something better done with tooltip.
If you do need index for something, you can use enumerate and tuple unpacking:

Code: Select all

for i, item in enumerate(items):
    text str(i) + "th element: " + item
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: No registered users