Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to stop a function from being called when assigning to a dictionary?

I’m making a game using pygame, and trying to add in items. I have a function for every item to call when you get the item (this part works well).

However, when I assign these functions to a dictionary, it calls and uses them, giving you every item in the game. I have tested every piece of code that uses the functions, so I know for a fact that is is the cause. Furthermore, I have had this problem multiple times before so I am certain that this is what’s occurring.

(Edit: To clarify, there is a separate list with acquired items, which is empty as it should be. The effects of each item gets added, but not the item itself, proving that this is the cause of the bug)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the item dictionary:

items = {
    "Damage Stone": damageStone(player),
    "Health Stone": healthStone(player),
    "Burn": burn(player),
    "Poison": poison(player),
    "Freeze": freeze(player)
}

This is assigned before the main game loop begins.

Is there any way to prevent the functions from being called when assigned to this list?

Notes:

Obviously I could write a section of code that undoes the effect of each item, however this is not optimal and as the amount of items grows would become very frustrating.

Lambda functions have the same problems and have the same issue.

ANSWER:

User John Gordon’s answer worked perfectly!
The problem was the brackets, which told python to call the function. Luckily, the function can be assigned without being called by using the function name without the brackets, as John demonstrated.
Thanks all!

New list:

items = {"Damage Stone": damageStone, "Health Stone": healthStone, "Burn": burn, "Poison": poison, "Freeze": freeze}

Where the functions are called:

def ItemUpdate(self, itemuselist, items):
        j = 0
        for i in itemuselist:
            func = items[i]
            func(player)
            itemuselist.pop(j)
            j += 1

>Solution :

If you want a dictionary to contain functions that are not called immediately, you would put the function object (just the function name, without parentheses) in the dictionary, like so:

items = {
    "Damage Stone": damageStone,
    "Health Stone": healthStone,
    ...
}

Then, later on in the program, you can fetch the function object and call it:

game_function = items["Damage Stone"]
game_function(player)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading