How to use python functions with imagebuttons?

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
tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

How to use python functions with imagebuttons?

#1 Post by tmp0000 »

Hi all. Can't understand why my code is not working.

I have an array with items and function to lose it.

Code: Select all

$ items = [("coin", "Lucky coin")];

python:
    def lose(search_item):
        global items
        for item in items:
            if (item[0] == search_item):
                items.remove(item)
                return True
        return False
If I use it in console or in dialog like so: $ lose('coin'), all seem to be okay.

But. When I try to use this function as an imagebutton action, strange things happen. I've googled and tried many variants (see comments before action string), and there were different errors. Function doesn't work or coin is losed but after that gameplay jumps somethere I can't control etc.

Code: Select all

screen inventory:
    zorder 111
    default tt = Tooltip(" ")
    frame:
        xalign 1.0
        background Solid("#0000")
        xmaximum 130
        ymaximum 450
        xfill True
        vbox:
            imagebutton auto "inventory/bag_%s.png" action SetVariable("invent", not invent)
            if invent:
                text tt.value
                hbox:
                    viewport id "box":
                        yinitial 9999
                        xmaximum 0.9
                        mousewheel True
                        draggable True
                        vbox:
                            for i in range(0, len(items)):
                                imagebutton:
                                    idle Image(GetFN(i))
                                    hover Image(GetFN(i))
                                    hovered tt.Action(GetHint(i))
                                    # tried Lose
                                    # Lose = renpy.curry(lose)
                                    # tried renpy.restart_interaction()
                                    action lose(items[i][0])
I don't understand what is going on and why. Spent a lot of time with no success. Please help.

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

Re: How to use python functions with imagebuttons?

#2 Post by Ocelot »

< < insert Rick Cook quote here > >

tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

Re: How to use python functions with imagebuttons?

#3 Post by tmp0000 »

Ocelot wrote: Mon Apr 09, 2018 12:37 pm https://www.renpy.org/doc/html/screen_a ... l#Function

Code: Select all

action Function(lose, items[i][0])
The same thing. Items seems to be losed, but one of the current dialog variants is clicked, and inventory screen doesn't update.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: How to use python functions with imagebuttons?

#4 Post by kivik »

Try adding print(search_item) at the start of your function, then use Shift-O to bring up console after clicking the button - it'll either tell you whether the button is calling the function, or what is being passed into the function. We may be able to figure it out from there.

Also, I know I don't know how far down development you are, but you may want to change your items into objects instead of tuples, it may get more complicated if you want to associate other information such as price, long description, item type, even special properties with them later.

tmp0000
Newbie
Posts: 11
Joined: Tue Mar 06, 2018 5:46 am
Contact:

Re: How to use python functions with imagebuttons?

#5 Post by tmp0000 »

kivik wrote: Wed Apr 11, 2018 4:06 am Try adding print(search_item) at the start of your function, then use Shift-O to bring up console after clicking the button - it'll either tell you whether the button is calling the function, or what is being passed into the function. We may be able to figure it out from there.

Also, I know I don't know how far down development you are, but you may want to change your items into objects instead of tuples, it may get more complicated if you want to associate other information such as price, long description, item type, even special properties with them later.
Thank you a lot. I did it another way.

I am quite skilled programmer, but in renpy all my skills are zero. I wroted a lot of classes, but these objects didn't save and then I rewrited them.

Post Reply

Who is online

Users browsing this forum: No registered users