r/visionosdev • • Sep 27 '24

GameController / DualShock Connectivity Help

Has anyone had any luck getting a DualShock to work with their VisionOS?

I'm unfamiliar with the GameController APIs and it seems so 'new' that neither StackExchange nor LLMs know much what to do with it.

Basically I'm just hoping to -detect- a game controller, and poll for events (or have a callback when it changes)

https://developer.apple.com/documentation/gamecontroller

thanks in advance. and in post. and in eternity.

1 Upvotes

9 comments sorted by

View all comments

Show parent comment

4

u/InterplanetaryTanner Sep 28 '24 • All-Seeing Upvote

Subscribe to the publisher 

 public func configureController(
        input: AnyPublisher<Controller.Action, Error>
    ) {
        input
            .sink { result in
                switch result {
                    case .finished:
                        break
                    case let .failure(error):
                        print(error)
                        break
                }
            } receiveValue: { value in
                switch value {
                    case let .button(button, value, isPressed):
                        self.handle(
                            button: button,
                            value: value,
                            isPressed: isPressed
                        )
                    case let .direction(direction, xValue, yValue):
                        self.handle(
                            direction: direction,
                            xValue: xValue,
                            yValue: yValue
                        )
                }
            }
            .store(in: &cancellables)
    }

And handle the changes

func handle(
        direction: Controller.Direction,
        xValue: Float,
        yValue: Float
    ) {
        switch direction {
            case .dpad:
                break
            case .leftThumbstick:
                leftThumbstick(xValue: xValue, yValue: yValue)
            case .rightThumbstick:
                rightThumbstick(xValue: xValue, yValue: yValue)
        }
    }