How to update a Sprite in pygame which takes in a function i.e. lambda?

Viewed 46

Say I have a Button sprite class like

class Button(pygame.sprite.Sprite): 
     def __init__(self, callback, ...):
         self.callback = callback

Now, callback is a function, but takes say an array and does something with it. Once I click the button, the array is changed, but now I want to apply the same button but on the new array?

Note that the button works the first iteration but now I want the function to be updated with the new array.

1 Answers

If you are passing a lambda (or function containing the array as a closure) to callback, then all you need to do to change that is reassign self.callback with a new one. I would suggest doing it with a method.

    def set_callback(self, callback):
        self.callback = callback

You can just set the callback wherever it is that you want it changed, including inside the previous callback function.

Related