Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule functions with arguments #246

Open
jasongrout opened this issue Mar 3, 2021 · 0 comments
Open

Schedule functions with arguments #246

jasongrout opened this issue Mar 3, 2021 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jasongrout
Copy link

First, I'm teaching kids how to program with pygame zero - it's awesome! They really picked it up quickly, and the quick wins they get encourage them to dive deeper into programming. Thanks very much for your work on this.

One thing that came up after doing the tutorial is wanting multiple aliens, which quickly led to storing the aliens in a list and iterating through them, and abstracting out how the alien pictures change to work across a list of aliens. This led to us wanting to schedule a function with arguments (in our case, we want to schedule setting an alien's picture back to normal, but we want to pass in the alien and a string to construct the alien's picture filename as arguments to the callback). We ended up with something like:

clock.schedule_unique(partial(set_alien_normal, alien, 'blue'))

where set_alien_normal is essentially:

def set_alien_normal(alien, color):
    alien.image = 'alien_' + color

Of course, this doesn't work, as is explained just under https://pygame-zero.readthedocs.io/en/stable/builtins.html#Clock.unschedule (and is clear from looking at the code, since scheduling is using weak refs). Scheduling with weakrefs prevents easy abstraction of a scheduled function call with arguments. Which brings us to:

Would you be amenable to adding a *args, **kwargs to the scheduling functions to supply arguments to call the callbacks with? Unscheduling could also take these arguments and only unschedule a function if it matched both the function and the args/kwargs. This would allow us to schedule a call without creating a temporary function that goes out of scope, i.e., we could do something like clock.schedule_unique(set_alien_normal, 1, alien, color).

@lordmauve lordmauve added enhancement New feature or request good first issue Good for newcomers labels Jan 2, 2022
oak-cassia added a commit to oak-cassia/OSSClass-pgzero that referenced this issue Jun 15, 2023
* Create Moon

* Create shin

* Create lim

* Create song

* Add folder and txt files

* add jung txt file

* test commit

* Update shin.txt

* first commit

* Update moon.txt

* Moon Seobin

* actor, music 설명추가

* [Revise Feature]

* [Refactoring]

* [Refactoring]

* [Refactoring]

* [Refactoring]

---------

Co-authored-by: Oak_Cassia <52772732+gnlckswjd@users.noreply.github.com>
Co-authored-by: tkdgjs98 <titania0325@gmail.com>
Co-authored-by: Song-JiHun <82068918+Song-JiHun@users.noreply.github.com>
Co-authored-by: seobin-moon <72489502+seobin-moon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants