I am trying implement controller hotplug support with pygame 2.6.1. According to the docs, this can be achieved with JOYDEVICEADDED, JOYDEVICEREMOVED events.
Example:
import sys
import pygame
pygame.init()
from pygame.locals import JOYDEVICEADDED, JOYDEVICEREMOVED, QUIT
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == JOYDEVICEADDED:
print(event)
if event.type == JOYDEVICEREMOVED:
print(event)
When running this with 1 controller attached, I first get this:
This seems fine. But when plugging another controller, the device_index is the same:
Then when unplugging the 1st controller:
When unplugging the 2nd controller:
Replugging 1st controller:
Replugging 2nd controller:
Unplugging 2nd:
Unplugging 1st:
I don't see the logic here. instance_id seems to be randomly increasing at some point, and device_id is always 0, except when having multiple controllers plugged at the very start of the program.
I think what I would need is a unique identifier for each controller, even if it's of the same type (EG PS4). In my opinion this would be the only way I could achieve proper hotplug support.
How can this be achieved?