Skip to content

Constants in Pygame are not grouped (2430) #1243

Open
@GalacticEmperor1

Description

@GalacticEmperor1

Issue №2430 opened by jtiai at 2020-12-31 07:34:52

Description

Constant groups like keys, events etc, are not logically grouped. This makes impossible to create for example event patcher based on group value names/values.

For example event patcher

for event in pygame.event.get():
    get_dispatcher(event.type)()

So there is no way to transform event.type to for example textual representation so it could be used to fetch/call some function or method.

Same applies for keys though they are prefixed with K_ which makes possible to extract constants from definitions much easier.


Comments

# # MyreMylar commented at 2020-12-31 07:53:58

For keys you can use pygame.key.name(K_KEYCODE_HERE), and invert this with pygame.key.key_code("key_name_here") in pygame 2. It should be possible (though tedious) to make a similar convert to string-and-back-again pair for event functions. For the raw integers we are often just passing through what SDL has picked, so likely easier not to mess with that in general.


# # jtiai commented at 2021-01-02 19:15:02

According to code event id's are grouped as a enum in C-code https://github.com/pygame/pygame/blob/main/src_c/_pygame.h# L85-L188

Those are not just exposed by any sensible means, for example as an Python IntEnum


# # ankith26 commented at 2021-01-03 05:25:09

I like the idea of grouping constants too, as long as it does not break any compatibility with old code (IIRC the enum thing does not work with python 2, which is gonna stick around here for a few more years, see # 2326)

PS: the C struct you are referring to there, is related to internal proxy events, not the real pygame events


# # jtiai commented at 2021-01-03 12:12:53

Note that in https://github.com/pygame/pygame/blob/main/test/event_test.py someone already did great deal of trouble setting up events and names for example.


# # ankith26 commented at 2021-01-04 12:50:47

If you want to convert event variables to their string representation. event_name function is what you need
https://www.pygame.org/docs/ref/event.html# pygame.event.event_name

The event_test just redefine those values for unit testing purposes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions