ChatGPT4 tries using objc bindings with Game Center

@jfperusse

I asked ChatGPT 4 to use Codea’s objc to authenticate the player inside Codea. Here’s the code it gave me:

AuthenticationHandler = objc.class("AuthenticationHandler")
function AuthenticationHandler:authenticateLocalPlayer()
    -- Obtain GKLocalPlayer from GameKit
    local GKLocalPlayer = objc.GKLocalPlayer:localPlayer()
    
    -- Define a block to handle authentication response
    local authResponseBlock = objc.block(function(authViewController, error)
        if authViewController then
            -- Present the authentication view controller
            print("Present the authentication view controller")
        elseif GKLocalPlayer:isAuthenticated() then
            print("Player authenticated")
        else
            print("Player not authenticated:", error)
        end
    end, "v@?@") -- v@?@ is the block signature for this block
    
    -- Set the authenticate handler
    GKLocalPlayer:setAuthenticateHandler_(authResponseBlock)
end
local authHandler = AuthenticationHandler()
authHandler:authenticateLocalPlayer()

It doesn’t work because GKLocalPlayer isn’t available from objc. Would it work if it was?

Hi @UberGoober! ChatGPT doesn’t know much about Codea’s objc feature but it’s interesting to see what it came up with. I’m not sure we ever had an objc.block, but I would have to revisit this code to make it use objc properly.

That being said, it does seem to indicate we are currently missing classes such as GKLocalPlayer. I’ll investigate this with @sim. I believe if it was included you would be able to use it for authentication.

Thanks!

@UberGoober Here’s a sneak peek at a sample I’ve been working one. I’ve been developing it using two VSCode Air Code windows, each connected to a separate device (iPhone + iPad) which made it a lot easier to iterate on this project.

This sample uses GameKit to create a match, invite a friend (both devices must have a different Game Center account), and then send messages to the other player.

I had to make some small changes to Objective-C (support for methods returning “const” results, as well as passing raw string arguments to objective-c methods), so my code would not work with the current TestFlight version, but I’ll share it as soon as the updates are in!

This was a really fun proof-of-concept to work on, and could easily be used as a basis for creating turn-based multiplayer games.

3 Likes

Hi @UberGoober!

Here’s the sample GameKit project, as promised :slight_smile:. This works with the latest 3.8 beta version.

GameKit.zip (3.3 KB)

3 Likes

This looks awesome, I can’t wait to try it.

It looks focused on turn-based multiplayer, and all my GameCenter work has been on real-time multiplayer, including the implementations I’ve done in Codea. Do you know how different setting up turn-based games is from setting up real-time games?

Hi @UberGoober! Technically, this is using GameKit real-time and not turned based. Both players are connected over an active network connection, and messages are received as fast as they can. I used a chat feature as an example, but you could instead send, for example, the position of a touch every frame and display it to the other player “in real time”.

1 Like