Buttons activating actions from different buttons

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
User avatar
minyan
Miko-Class Veteran
Posts: 630
Joined: Tue Feb 10, 2015 3:59 pm
Completed: Trial By Fire, Heartbaked, Ellaria, Plain, This My Soul, The Pretenders Guild
Projects: Arena Circus
Tumblr: minyanstudios
itch: harlevin
Contact:

Buttons activating actions from different buttons

#1 Post by minyan »

Hello all,

So this is a weird issue I haven't seen before. I was playing around with some simple code for a potion creator game, and I run into this issue where when I make a new text button and assign it an action to activate a function, the function will instead activate automatically, or it will be activated by other buttons.

For example, I have this button:

textbutton "Create" action Function(make_potion(li))

But every other textbutton is now calling the "make_potion" function when I click them. I'm not a code expert, but usually when I do buttons like this the function is only called when I click that actual button. Is there something fundamental I'm missing here? I've attached my code below.

Code: Select all

init python:
    import renpy.store as store


    class potion(store.object):
        def __init__(self,name,attribute_1,attribute_2,desc,strength,cost,recipe):
            self.name = name
            self.attribute_1 = attribute_1
            self.attribute_2 = attribute_2
            self.desc = desc
            self.strength = strength
            self.cost = cost
            self.recipe = []

    def make_potion(used):
        global potion_list
        strength = 0

        effects = ""
        for ingredient in used:

            strength += ingredient.strength
            effects += ingredient.attribute_1

        if "Heal" and "Poison" in effects:
            renpy.show_screen("tooltip",message = "Potion failed!")

        else:
            recipe = used
            #new_potion = potion(n,att1,att2,desc,strength,cost,recipe)
            #potion_list.append(new_potion)




    class ing(store.object):
        def __init__(self,name,attribute_1,desc,images,hovers,strength,quantity,input_quantity):
            self.name = name
            self.attribute_1 = attribute_1
            self.strength = strength
            self.desc = desc
            self.images = images
            self.hovers=hovers
            self.quantity = quantity
            self.input_quantity = input_quantity


        def create_ing(self,x):
            x.append(self)



        def select(self):
            global selected_ing
            if self.quantity > 0:

                if self not in selected_ing:
                    selected_ing.append(self)
                    self.quantity -= 1
                    self.input_quantity += 1
                else:
                    self.quantity -= 1
                    self.input_quantity += 1



        def decrease(self):
            global selected_ing
            if self.input_quantity == 1:
                selected_ing.remove(self)
                self.quantity += self.input_quantity
                self.input_quantity = 0
            else:
                self.quantity += 1
                self.input_quantity -= 1

        def increase(self):
            if self.quantity > 0:
                self.quantity -= 1
                self.input_quantity += 1




default ingredients = []
default selected_ing = []
default potion_list = []
default recipes = []
default starblast = ing("Starblast","Heal","A beautiful flower with an earthy smell.", "images/starblast.png","images/pumpkinseed.png",1,4,0)
default pumpkinseeds = ing("Pumpkin Seeds","Poison","Seeds from a pumpkin.", "images/pumpkinseed.png", "images/starblast.png",1,4,0)


label start:
$ starblast.create_ing(ingredients)
$ pumpkinseeds.create_ing(ingredients)


scene white
call screen ingredient_list(ingredients,selected_ing)


"..."

return
screen tooltip(message):
    zorder 5
    frame:
        background "tooltip_box"
        xmaximum 276
        ymaximum 77
        xfill True
        yfill True
        # xalign 0.5
        # yalign 0.1
        # left_padding 20
        # top_padding 20
        # right_padding 20
        # bottom_padding 20
        xalign 0.5
        ypos 800
        text message style "tooltip_style" xalign 0.5 yalign 0.5
    timer 3 action Hide("tooltip")

screen ingredient_list(ingredients,li,scroll="vpgrid"):
        tag menu
        frame:
            xalign 0.5
            yalign 0.5
            xsize 500
            ysize 200
            vpgrid:

                cols 1
                xsize 500
                ysize 200
                yfill True
                xfill True
                side_yfill True
                scrollbars "vertical"
                mousewheel True
                draggable True
                pagekeys True
                transclude

                for item in li:
                    $ name = item.name
                    $ pic = item.images
                    $ quantity = item.quantity
                    $ count = item.input_quantity
                    hbox:
                        textbutton "<" action Function(item.decrease)
                        text "[name] [count]"
                        textbutton ">" action Function(item.increase)
                textbutton "Create" action Function(make_potion(li))

        frame:

            xsize 500
            ysize 500
            yfill True
            xfill True
            vpgrid:
                cols 4
                xsize 500
                ysize 500
                yfill True
                xfill True
                side_yfill True
                spacing 20

                scrollbars "vertical"
                mousewheel True
                draggable True
                pagekeys True
                transclude

                transclude
                for item in ingredients:
                    $ name = item.name
                    $ pic = item.images
                    $ h = item.hovers
                    $ quantity = item.quantity
                    $ li = selected_ing

                    vbox:
                            imagebutton:
                                idle pic
                                hover h
                                action Function(item.select)
                                #action item.select(li)
                            hbox:
                                spacing 10
                                text "[name]" style "ing_style" xsize 100
                                text "([quantity])" style "ing_style" xsize 100



style ing_style:
    size 15
ImageImage

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

Re: Buttons activating actions from different buttons

#2 Post by Ocelot »

This is a common issue people often encounter before they learn how screens work.
Most important point: screens execute all of their code when shown or predicted (which might happen at any time). So, if you write make_potion(li) it will execute that.
Screen actions are classes or functions, returning classes, which save their parameters and do some stuff when they are called. Let's see what happens when your screen code is executed:

Code: Select all

Function(
    make_potion(li)
)
This will execute make_potion(li), take its return argument (probably None) and store within Function object, so it can be called later. Compare this to correct code:

Code: Select all

Function(
    make_potion,
    li
)
This code will take make_potion function (no call operator here, so it is not called) and will store it to call later. It will also store li parameter and will pass it to the make_potion function when it will be called.

Line breaks in preceding code are only for demonstration and are not nessesary in actual code.
Last edited by Ocelot on Thu Dec 15, 2022 5:23 pm, edited 1 time in total.
< < insert Rick Cook quote here > >

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Buttons activating actions from different buttons

#3 Post by _ticlock_ »

EDIT: Nevermind, Ocelot already point that out
minyan wrote: Thu Dec 15, 2022 4:57 pm

Code: Select all

                textbutton "Create" action Function(make_potion(li))
I haven't check the whole code, but likely because you pass arguments incorrectly to the action Function:

Code: Select all

                textbutton "Create" action Function(make_potion, li)

User avatar
minyan
Miko-Class Veteran
Posts: 630
Joined: Tue Feb 10, 2015 3:59 pm
Completed: Trial By Fire, Heartbaked, Ellaria, Plain, This My Soul, The Pretenders Guild
Projects: Arena Circus
Tumblr: minyanstudios
itch: harlevin
Contact:

Re: Buttons activating actions from different buttons

#4 Post by minyan »

Ocelot wrote: Thu Dec 15, 2022 5:22 pm This is a common issue people often encounter before they learn how screens work.
Most important point: screens execute all of their code when shown or predicted (which might happen at any time). So, if you write make_potion(li) it will execute that.
Screen actions are classes or functions, returning classes, which save their parameters and do some stuff when they are called. Let's see what happens when your screen code is executed:

Code: Select all

Function(
    make_potion(li)
)
This will execute make_potion(li), take its return argument (probably None) and store within Function object, so it can be called later. Compare this to correct code:

Code: Select all

Function(
    make_potion,
    li
)
This code will take make_potion function (no call operator here, so it is not called) and will store it to call later. It will also store li parameter and will pass it to the make_potion function when it will be called.

Line breaks in preceding code are only for demonstration and are not nessesary in actual code.
This did it! Thank you both ^^
ImageImage

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot]