-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
pygame.event.get() returns incorrect order of events when using eventtype kwarg #3305
Comments
I ultimately moved away from using the eventtype kwarg. I had planned to first pull any pygame.QUIT events, followed by custom user events, followed by a regular event.get() for any leftovers. The performance impact of calling .get multiple times and using it to effectively order events the way I wanted was too great. Profiling reported about a half millisecond per call and doing three calls added a whole millisecond+ to each frame time. It might be prudent to give the .get function some love and attempt to improve it's performance / functionality in general. It is a critical function for pygame and should be well tuned. Anyways, back to one big loop of if else logic for me. |
The underlying SDL function ( As far as performance is concerned, the best thing to do is to simply call the regular |
The issue you're describing with Current BehaviorWhen you call Expected BehaviorThe events should be returned in the order they were added to the queue, regardless of their type. This is the standard behavior for event queues and is what most developers would expect. WorkaroundAs you mentioned, one workaround is to avoid using the import pygame
pygame.init()
# Define custom events
USEREVENT_1 = pygame.event.custom_type()
USEREVENT_2 = pygame.event.custom_type()
# Post custom events
pygame.event.post(pygame.event.Event(USEREVENT_1, {'message': 'First Event 1'}))
pygame.event.post(pygame.event.Event(USEREVENT_2, {'message': 'First Event 2'}))
pygame.event.post(pygame.event.Event(USEREVENT_1, {'message': 'Second Event 1'}))
# Main event loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type in (USEREVENT_1, USEREVENT_2):
print(f'Message: {event.message}')
# Handle other event types here
pygame.quit() Performance ConsiderationsYou mentioned that calling Future ImprovementsIt would be beneficial for the Pygame community to address this issue in future releases. The ConclusionFor now, the best approach is to manually filter events in your event loop to ensure they are processed in the correct order. This avoids the reordering issue and gives you more control over how events are handled in your game. |
Don't make multiple calls to |
I believe the comment you're replying to is AI generated. It's actually a good summary IMO, but still. |
When calling pygame.event.get() with a sequence of event types using the eventtype keyword the return values are sorted by event type rather than preserving their order in the queue. This works fine when only one event type is pass but it problematic when asking for multiple types. Events returned by this function should not be reordered. The docs do not mention reordering and it does not seem like wanted behavior when dealing with events as order inherently matters when dealing with a queue.
On a side not, the eventtype kwarg does not accept sets of events and only takes sequences. It would be nice to allow for sets to be used as well as lists.
I did not test if using the exclude functionality would result in events being sorted as well.
Environment:
pygame-ce 2.5.2 (SDL 2.30.8, Python 3.12.0)
Current behavior:
Events are returned sorted by event type.
Expected behavior:
Events to return in the same order as they exist in the queue regardless of type.
Test code
The text was updated successfully, but these errors were encountered: