Wesley de Groot's Blog
Simplifying Game Controller Integration with GameControllerKit

Back

In the ever-evolving world of game development, integrating game controllers across multiple platforms can be a difficult task.
Enter GameControllerKit, a Swift package designed to streamline this process for developers working on iOS, macOS, and tvOS.
Let's dive into what makes this toolkit a game-changer.

What is GameControllerKit?

GameControllerKit is an open-source Swift package that simplifies the process of connecting and interacting with game controllers.
Whether you're developing for iOS, macOS, or tvOS, this toolkit provides a unified API to handle game controller inputs, control lights, and manage haptic feedback.

Key Features

  1. Easy Connection Management: GameControllerKit makes it straightforward to interact with game controllers. The package handles the (dis)connections of game controllers, allowing developers to focus on gameplay rather than handling game controller conntections and setups.

  2. Unified Input Handling: With a simple and intuitive API, developers can easily read inputs from various game controllers. This includes buttons, joysticks, and other control mechanisms, ensuring a seamless gaming experience across different devices.

  3. Advanced Control Options: Beyond basic input handling, GameControllerKit allows developers to control the lights and haptics of connected controllers. This feature can be used to enhance the gaming experience by providing visual and tactile feedback to players.

  4. Cross-Platform Support: One of the standout features of GameControllerKit is its support for multiple Apple platforms. Whether you're developing a game for iOS, macOS, or tvOS, this toolkit ensures consistent controller behavior across all devices.

Getting Started

To get started with GameControllerKit, you can add it to your project using Swift Package Manager. Here’s a quick guide:

  1. Add the Package: In Xcode, go to File > Swift Packages > Add Package Dependency and enter the repository URL: https://github.com/0xWDG/GameControllerKit.

  2. Import the Package: In your Swift files, import GameControllerKit to access its features:

    import GameControllerKit
  3. React to Controllers: Use the provided API to connect to game controllers and handle inputs:

    let controller = GameControllerKit()
    gameController.set(handler: { button, pressed, controller in
        // Handle controller input
    })

Demo Application

import SwiftUI
import GameControllerKit

struct ContentView: View {
    /// The game controller kit
    @State
    var gameController = GameControllerKit()

    /// Log
    @State
    var log: [String] = []

    var body: some View {
        VStack {
            Button {
                gameController.set(color: .GCKRandom)
            } label: {
                Text("Random Color")
            }

            Text("Controller: \(gameController.controller?.productCategory ?? "None"), " +
                 "\((gameController.controllerType ?? .generic).description)")
            Text("Last action:\n\(String(describing: gameController.lastAction)).")

            GCKControllerView()
                .environmentObject(gameController)

            List {
                ForEach(log.reversed(), id: \.self) { text in
                    Text(text)
                }
            }
        }
        .padding()
        .onAppear {
            gameController.set(handler: handler)
        }
    }

    /// Handler
    ///
    /// - Parameters:
    ///   - action: action
    ///   - pressed: is the button pressed?
    ///   - controller: which controller?
    public func handler(
        action: GCKAction,
        pressed: Bool,
        controller: GCKController
    ) {
        log.append(
            "\(String(describing: action))(\(action.position.arrowRepresentation)) \(pressed ? "Pressed" : "Unpressed"), " +
            "Controller #id \(String(describing: controller.playerIndex.rawValue))"
        )

        if action == .buttonA && pressed {
            // set to a random color
            gameController.set(color: .GCKRandom)
        }
    }
}

Screenshots:

iOS

C65552DF-04CC-493E-AD73-C385A7CEC53C

MacOS

AA801C52-88A1-4326-A5DC-3A04DF491077

tvOS

Screenshot 2024-08-29 at 14 43 51

Conclusion

GameControllerKit is a powerful tool for game developers looking to integrate game controllers into their projects with ease. Its simple API, cross-platform support, and advanced control options make it an invaluable resource for creating immersive gaming experiences. Whether you're a seasoned developer or just starting out, GameControllerKit can help you take your game to the next level.

Check out the GameControllerKit repository to learn more and start integrating it into your projects today!

Read more

Share


Share Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap