pygame-web / platform_wasm

support and helpers for various packages on wasm platform
https://pypi.org/project/platform-wasm/
MIT License
1 stars 1 forks source link

`patch_set_timer` breaks if you pass an `pygame.event.Event` #2

Open queueseven opened 7 months ago

queueseven commented 7 months ago

In this line the constructor to pygame.event.Event is called with event, but event itself could already be a pygame.event.Event .

If that's the case, then the constructor of pygame.event.Event will throw a TypeError:

future: <Task finished name='main' coro=<main() done, defined at <console>:4> exception=TypeError("'pygame.event.Event' object cannot be interpreted as an integer")>
Traceback (most recent call last):
  File "<console>", line 19, in main
  File "/data/data/org.python/assets/site-packages/platform_wasm/pygame/__init__.py", line 40, in patch_set_timer
    cevent = pygame.event.Event(cust_event_no)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'pygame.event.Event' object cannot be interpreted as an integer
pmp-p commented 7 months ago

Thanks for the report, please test the patch and report here if it is ok https://github.com/pygame-web/platform_wasm/pull/1

queueseven commented 7 months ago

I'm afraid it's not that easy, because event is used as a key for the THREADS dict, and if it's a pygame.event.Event instance, this will fail, as this object is not hashable.

The documentation for set_timer says:

It is also worth mentioning that a particular event type can only be put on a timer once. In other words, there cannot be two timers for the same event type. Setting an event timer for a particular event discards the old one for that event type.

so maybe a good solution would be to use something like that:

    if isinstance(event, pygame.event.Event):
        cevent = event
        tevent = event.type
    else:
        cevent = pygame.event.Event(int(event))
        tevent = int(event)

and then use tevent as key for THREADS.

e-dong commented 7 months ago

Thanks I will update my PR