Button Action triggering without being clicked

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
Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Button Action triggering without being clicked

#1 Post by Draite » Wed Mar 25, 2020 12:31 am

I'm working on some consumable items that I'd like to have trigger some text after being consumed in the inventory. I've hit a weird road block which I've experienced before and can't remember if I ever figured out how to fix it. Essentially when the screen is updated part of the button action is triggered without clicking the button itself.

Code: Select all

if selected_item != None and selected_item < len(inventory):
        if isinstance(inventory[selected_item][0], Consumable):
            imagebutton:
                pos (669,204)
                anchor (0.5,0.5)
                idle "gui/frames/generic_button_idle.png"
                hover "gui/frames/generic_button_hover.png"
                action [SetVariable("consumable_text_point",0),SetVariable("consumable_text",inventory[selected_item][0].useItem(pc, False)),Function(removeFromInventory(inventory,selected_item,1)),Show("consumable_screen")]
            text "Consume" size 16 color "#000" anchor (0.5,0.5) pos (669,204)
Essentially once you select an item in the inventory it'll add a new set of buttons to the screen which are action you can take with that particular item. Equipment can be equipped or unequipped and consumables should be consumed. However whenever I select a consumable item the removeFromInventory function is immediately triggered, but nothing else in that list seems to happen which has only made me more confused by it.

What I'd like to happen is you select the item in the inventory, then you get the new buttons. When you click one of those you use the item. This adds a some dialogue describing the effects of the item to a list. Then applies the item effects. Then removes one copy of that item from the inventory. Then displays the 'consumable_screen' which is just a box that displays the dialogue that was grabbed at the beginning. You then click through the dialogue and the consumable screen is closed, returning you to the inventory.

Thank you for any insight you can give me and thanks for reading.

adunato
Newbie
Posts: 3
Joined: Sun Sep 01, 2019 2:28 am
Contact:

Re: Button Action triggering without being clicked

#2 Post by adunato » Wed Mar 25, 2020 3:12 am

Hello,

I'm going out on a limb here, I had a similar issue when including a python statement in action rather than a function / callable object, the button would trigger at screen creation. In your case each python instruction is wrapped in either Function or SetVariable so it's less obvious what the issue may be.

I would try to isolate the issue by using one action at the time and see if one or more is responsible for triggering the button (sorry if this is obvious).

If you do find that one (or more) action is responsible for that, I would try to remove python statements from the action script and streamline any python instruction into a dedicated callable. For example you have that inventory[selected_item][0].useItem(pc, False) which returns a value but also does something (e.g. update inventory object). I would replace it with a callable that sets consumable_text variable and updates the inventory.

As a side note I found action Function to not be of much use as any callable seems to be working just fine.

Let me know if you sort it out as I'm interested in understanding action behaviours better.

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Button Action triggering without being clicked

#3 Post by gas » Wed Mar 25, 2020 7:58 am

Post there the removeFromInventory function.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Button Action triggering without being clicked

#4 Post by Draite » Wed Mar 25, 2020 8:40 am

@adunato - I tried breaking it down completely using only one function at a time and it's only triggers when removeFromInventory is being called in the action list

@gas -

Code: Select all

def removeFromInventory(inven,p,amo):
        # First make sure the item is not equipped
        if isinstance(inven[p][0],Equipable):
            if inven[p][0].is_equipped:
                return None
               
        # Test code to check if item is removed correctly
        _r = []
        _r.append(copy.copy(inven[p][0]))
        _a = inven[p][1]
        if _a >= amo:
            _a = amo
        _r.append(_a)
        
        # Remove item amount. If item quantity is less then or equal to 0 remove item from inventory
        inven[p][1] -= amo
        if inven[p][1] <= 0:
            inven.pop(p)

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Button Action triggering without being clicked

#5 Post by Remix » Wed Mar 25, 2020 9:02 am

Function(removeFromInventory, inventory,selected_item, 1))

You do not add the Function arguments as the full call...
Function(function_name, ## <---- comma not parenthesis ##
Frameworks & Scriptlets:

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Button Action triggering without being clicked

#6 Post by Draite » Wed Mar 25, 2020 5:06 pm

Sorry I don't think I understand. How should it be written?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Button Action triggering without being clicked

#7 Post by Remix » Wed Mar 25, 2020 5:55 pm

The first line of my reply has it written exactly as needed
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Button Action triggering without being clicked

#8 Post by Remix » Wed Mar 25, 2020 6:02 pm

Note though that I have no idea if your SetVariable part will work. It will likely run the useItem during prediction to get the returned string. So that function/method might cause side effects.
You should also consider formatting your code to use shorter lines, e.g.

Code: Select all

    action [
        SetVariable("consumable_text_point",0),
        SetVariable("consumable_text",inventory[selected_item][0].useItem(pc, False)), # <-- this might cause issues
        Function(removeFromInventory, inventory, selected_item, 1), # <-- fixed action
        Show("consumable_screen")
        ]
Frameworks & Scriptlets:

Draite
Regular
Posts: 26
Joined: Tue Oct 08, 2019 9:10 pm
Contact:

Re: Button Action triggering without being clicked

#9 Post by Draite » Wed Mar 25, 2020 6:30 pm

Oh shoot, sorry I totally see what you meant now. Thank you so much. That seems to have fixed it.

Code: Select all

SetVariable("consumable_text",inventory[selected_item][0].useItem(pc, False))
This seems to work after some crude testing with a couple of different items. Seems to get the descriptions correctly and connect them properly. I'll have to do some more extensive stress testing before I'm sure it works but it seems stable at least for the moment.

Downside it seems like I can't nest modal screens which could be a little annoying to work around. Like the inventory screen is modal so that it doesn't interupt the rest of the game, but if I call consumable screen which is also modal it seems to push the game forward anyways.

Nvm I made a dumb mistake. It's all working now! Thanks for the help!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], span4ev