[Kind of solved] How to set imagebutton's Action from a Python function?

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
PasswordIsntHunter2
Newbie
Posts: 19
Joined: Sun Jan 07, 2018 6:23 pm
Contact:

[Kind of solved] How to set imagebutton's Action from a Python function?

#1 Post by PasswordIsntHunter2 »

I want to put an imagebutton on the screen and have it do something (say, just change a variable) when clicked. I know how to do this from a ren'py script, but I want to do it from a Python function using renpy.ui.imagebutton(). If I do this:

Code: Select all

        renpy.ui.imagebutton(auto="test_images/Item_%s.png", xcenter=640, ycenter=400)
The button renders, greyed-out, and clicking anywhere will advance the script. If I add a command like this:

Code: Select all

        renpy.ui.imagebutton(auto="test_images/Item_%s.png", xcenter=640, ycenter=400, action=SetVariable(chosen, 3))
then I get an error on the next call - in my case a renpy.say() - that an "attribute name must be string".

Here is the entirety of my script.rpy and traceback.txt (I pared it down from a much larger project but it still has the same error).

How do I use renpy.ui.imagebutton()? Everything I can find is on declaring the button from a ren'py script (not a Python function), which isn't what I'm trying to do here. I searched for usages of the imagebutton() function in the example projects and a few others but it's impossible to figure out what's going on in there due to Python's moronic typing.

(I'm also interested in how to block interaction with anything behind the button(s), which I could do if I had a Screen, but again I want to set all this up in a Python function and I can't find anything on using ren'py Screens from Python)

[EDIT] I think I can actually do what I want with a Screen, but I'm still curious about what the ImageButton is doing here.

[EDIT2] Pretty sure I solved it. The variable can't be defined in the function or the ren'py script - it has to have a more global scope than that. I put it in an "init python" block with some other vars and it works now. I'm still not sure what kind of scope SetVariable has exactly.

User avatar
jesusalva
Regular
Posts: 88
Joined: Mon Jul 22, 2013 5:05 pm
Organization: Software in Public Interest, Inc.
IRC Nick: jesusalva
Github: pazkero
itch: tmw2
Location: Brazil
Discord: Jesusalva#4449
Contact:

Re: [Kind of solved] How to set imagebutton's Action from a Python function?

#2 Post by jesusalva »

PasswordIsntHunter2 wrote: Sun Jan 07, 2018 8:14 pm

Code: Select all

        renpy.ui.imagebutton(auto="test_images/Item_%s.png", xcenter=640, ycenter=400, action=SetVariable(chosen, 3))
then I get an error on the next call - in my case a renpy.say() - that an "attribute name must be string".


[EDIT2] Pretty sure I solved it. The variable can't be defined in the function or the ren'py script - it has to have a more global scope than that. I put it in an "init python" block with some other vars and it works now. I'm still not sure what kind of scope SetVariable has exactly.
SetVariable expects the first argument to be a string. The correct syntax is:

Code: Select all

        renpy.ui.imagebutton(auto="test_images/Item_%s.png", xcenter=640, ycenter=400, action=SetVariable("chosen", 3))
I wish it was better explained at documentation...
I think what you described as “it works now” means “it presents undefined behavior now, but it is what I expected”.
Jesusaves/Jesusalva

Post Reply

Who is online

Users browsing this forum: No registered users