Page 1 of 1

[SOLVED] create and delete imagebuttons from python block

Posted: Mon Oct 31, 2022 8:19 am
by The Historian
I'm trying to delete imagebuttons by id from a python function, as well as add imagebuttons to specific 'fixed' blocks, by id, from a python function.
But I've been searching for days and haven't been able to find docs on python functions that do this stuff.

Code: Select all

init python:
	def add_button(img1, img2):
		ui.imagebutton(img1, img2, clicked=click_function() , hovered=hover_function()) # using clicked=None, action = click_function() gives same problem described below
This part (found from very outdated docs, renpy 5.6) almost works for adding imagebuttons. Except hover image (img2) doesn't work (doesn't show on hover), and the clicked (or action) function is triggered on hovers as well as clicks. The hovered function works fine.
Still dunno how to add it to a 'fixed' block though. I think get_displayable(screen, id) can grab a 'fixed' block correctly, but idk how to add an imagebutton as its child.

Are there no built in functions for this? Would I be able to do this with creator-defined displayables?
I'm familiar with using classes, but i'm fairly new to renpy, and what I can't find is docs of python functions, methods, properties, etc. for renpy. (except the python equivalents https://www.renpy.org/doc/html/statemen ... lents.html which just gives an overview.)

What i basically want is:

Code: Select all

init python:
	example_function(fixed_id, old_imgbutton_id, new_imgbutton_id):
		#none of the below are actual functions, they're pseudo code for what I want to do.
		
		#renpy.delete(old_imgbutton_id)
		#fixed_to_add_img_to = renpy.get_by_id(fixed_id)
		#fixed_to_add_img_to.children.append(renpy.imagebutton("idle_img", "hover_img", action=another_example_function(), hover=foo_bar(), id=new_imgbutton_id))

		#I completely understand these changes aren't permanent to the script/screen. They're not meant to be. That's kind of the point of why i need to do them from a function.
for some context, this is for a chess-like board game, except the board is bigger than screen so it's in a vpgrid. And the tiles have unique status effect that show on hover, so each tile is an imagebutton. Other imagebuttons - like chess pieces - are placed on top of certain tiles (so tiles + "chess pieces" are in 'fixed' blocks) and can be moved.
The only part I'm having trouble with is "moving" the "chess pieces", by removing and adding imagebuttons from inside python functions.

Basically, this. But everything here is placeholder, i'm just getting the "move" functionality to work. I wanna move the placeholder sword icons, which are imagebuttons. The tiles are also imagebuttons, the odd one out is being hovered over.
Image

even if not direct answers, just links to relevant docs, example codes, etc. would be extremely appreciated

Re: create and delete imagebuttons from python block

Posted: Mon Oct 31, 2022 9:13 am
by Ocelot
ui functions should not be used. There is a reason why they were deleted from current documentation.

What you need is a screen which places imagebuttons depending on some external data. Something like:

Code: Select all

default pieces = {
    (0, 5): "img1_%s",
    (1, 3): "img2_%s"
}

screen board():
    vpgrid:
        cols 20
        rows 16

        for row in range(16):
            for col in range(20):
                fixed:
                    imagebutton ... # grass pieces, 
                    if (row, col) in pieces:
                        imagebutton auto pieces[(row, col)] ... # add imagebutton for piece if it exist on curent row.
Moving pieces would consist of modifying pieces variable and restarting interactiion if nessesary.

Re: create and delete imagebuttons from python block

Posted: Mon Oct 31, 2022 1:24 pm
by The Historian
^ trying this, but it seems using the conditionals with a multidimensional arrays is causing issues when i add the action button calling a function that changes the array. ("action" is activating hundreds of times soon as the screen is shown)
I'll mark the thread solved when I post working code that addresses the original post.

Also, I'll try with dictionaries like the answer suggested, I just wanna see if it's possible with multidimensional arrays first

Re: create and delete imagebuttons from python block

Posted: Mon Oct 31, 2022 1:58 pm
by Ocelot
Which action do you use? This is usually symptom of having code with side effects in screens.

And I forgot to wrap bacgtound imagebutton and potential piece in fixed in the code I provided.

Re: create and delete imagebuttons from python block

Posted: Mon Oct 31, 2022 2:31 pm
by The Historian
Oh, answer (post #2) works perfectly.

Issue I mentioned in post #3 was cause I have to use:

Code: Select all

action Function(function_name, *args)
and not:

Code: Select all

action funcion_name(*args)
which apparently breaks everything cause I'm not supposed to run unwrapped python in renpy. Didn't know that...