Function executes when it shouldn't

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
Rosiero
Newbie
Posts: 5
Joined: Thu Oct 19, 2017 2:42 pm
Contact:

Function executes when it shouldn't

#1 Post by Rosiero »

I'm creating a shop system for my game, with a function that handles altering the inventory and subtracting money as follows:

Code: Select all

init python:
    def item_purchase(item_id):
        global money
        if shop_items[item_id][5]:
            shop_items[item_id][6] += 1
        else:
            shop_items[item_id][5] = True
            shop_items[item_id][6] = 1
        money -= shop_items[item_id][1]
In my shop screen's code, buttons to purchase items are coded like this:

Code: Select all

                            button:
                                $ item_name = shop_items[shop_items_to_display[i]][0]
                                $ item_desc = shop_items[shop_items_to_display[i]][3]
                                if shop_items[shop_items_to_display[i]][4] == 2:
                                    text "{color=#999}[item_name]\n{size=16}[item_desc]{/size}{/color}"
                                    text "{color=#999}Sold Out{/color}" xalign 1.0
                                else:
                                    text "[item_name]\n{size=16}[item_desc]{/size}"
                                    text "$" + str(shop_items[shop_items_to_display[i]][1]) xalign 1.0
                                action [ If (money >= shop_items[shop_items_to_display[i]][1] and shop_items[shop_items_to_display[i]][4] == 1, 
                                    [ Function(item_purchase(0)) ],
                                    If(shop_items[shop_items_to_display[i]][4] == 1, Notify("Not enough money."))) ]
                                xpos 0
                                ypos (0 + 55 * i)
However, instead of only being called when this button is pressed, the item_purchase function seems to execute several times when -any- button is pressed, even on another screen! I'm very new to Python and Ren'Py so I'm sure I must have done something obvious wrong, but I've been trying to figure this one out for hours and I really have no idea why this is happening.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Function executes when it shouldn't

#2 Post by RicharDann »

This caused me a lot of headaches in the past... Anyway pretty sure the problem is in the Function action, you need to pass your function's arguments like this:

Code: Select all

Function(item_purchase, 0)
More on Function() here.
Last edited by RicharDann on Thu Oct 19, 2017 3:03 pm, edited 1 time in total.
The most important step is always the next one.

Rosiero
Newbie
Posts: 5
Joined: Thu Oct 19, 2017 2:42 pm
Contact:

Re: Function executes when it shouldn't

#3 Post by Rosiero »

Thank you so much! That fixed it! I still don't really understand why I got the crazy result I did, but now that it works properly I can finally rest easy.

Post Reply

Who is online

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