GKMatch

Inherits: RefCounted < Object

Represents an active real-time Game Center match.

Description

GKMatch wraps Apple’s realtime networking object. After starting matchmaking with GKMatchmakerViewController (see the “Realtime Matchmaking” section of GameCenterGuide.md) you will receive a GKMatch instance and can send data, monitor players, and respond to disconnections via Godot signals. For platform specifics see Apple’s GKMatch reference.

Guide sample showing matchmaking and signal hookups:

var request := GKMatchRequest.new()
request.max_players = 2
request.min_players = 1
request.invite_message = "Join me in a quest to fun"

GKMatchmakerViewController.request_match(request, func(game_match: GKMatch, error: Variant) -> void:
    if error:
        print("Could not request a match %s" % error)
        return

    print("Got a match!")
    game_match.data_received.connect(func(data: PackedByteArray, from_player: GKPlayer) -> void:
        print("Received data from %s" % from_player.display_name)
    )
    game_match.data_received_for_recipient_from_player.connect(func(data: PackedByteArray, for_recipient: GKPlayer, from_remote_player: GKPlayer) -> void:
        print("Forwarded data for %s from %s" % [for_recipient.display_name, from_remote_player.display_name])
    )
    game_match.did_fail_with_error.connect(func(match_error: String) -> void:
        print("Match failed with %s" % match_error)
    )
    game_match.should_reinvite_disconnected_player = func(player: GKPlayer) -> bool:
        return true
    game_match.player_changed.connect(func(player: GKPlayer, connected: bool) -> void:
        print("Player %s changed to %s" % [player.display_name, connected])
    )
)

Broadcast helper from the guide (assuming you keep the reference as game_match):

var data := "How do you do fellow kids".to_utf8_buffer()
game_match.send_data_to_all_players(data, GKMatch.SendDataMode.reliable)

Send to a subset of players (still using the game_match variable):

var payload := PackedByteArray([1, 2, 3])
var recipients := [first_player, second_player]
game_match.send(payload, recipients, GKMatch.SendDataMode.reliable)

Properties

int

expected_player_count

0

Array

players

[]

Variant

should_reinvite_disconnected_player

Methods

void

disconnect()

int

send(data: PackedByteArray, toPlayers: Array, dataMode: int)

int

send_data_to_all_players(data: PackedByteArray, dataMode: int)


Signals

data_received(arg1: PackedByteArray, arg2: GKPlayer) 🔗

Emitted when another player sends data to the local device. Provides the raw bytes and the sender.


data_received_for_recipient_from_player(arg1: PackedByteArray, arg2: GKPlayer, arg3: GKPlayer) 🔗

Emitted when the local player receives data that was addressed to a specific recipient. Arguments provide the payload, the intended recipient, and the sender.


did_fail_with_error(arg1: String) 🔗

Fires when Apple reports a networking error. The String argument contains the localized description.


player_changed(arg1: GKPlayer, arg2: bool) 🔗

Notifies you when a player connects or disconnects. The boolean is true for connected players and false otherwise.


Enumerations

enum SendDataMode: 🔗

SendDataMode reliable = 0

Uses GameKit’s reliable data channel (ordered delivery with retries).

SendDataMode unreliable = 1

Uses GameKit’s unreliable channel, useful for latency-sensitive updates.


Property Descriptions

int expected_player_count = 0 🔗

  • int get_expected_player_count()

The number of additional players that GameKit is recruiting before the match can begin.


Array players = [] 🔗

Array of GKPlayer instances that are currently connected.


Variant should_reinvite_disconnected_player 🔗

  • void set_should_reinvite_disconnected_player(value: Variant)

  • Variant get_should_reinvite_disconnected_player()

Optional Callable that receives a GKPlayer and returns true if the player should be reinvited after disconnecting (see the sample in GameCenterGuide.md).


Method Descriptions

void disconnect() 🔗

Calls GKMatch.disconnect() to leave the match immediately.


int send(data: PackedByteArray, toPlayers: Array, dataMode: int) 🔗

Sends a payload to the specified Array[GKPlayer]. Returns a Error value (OK on success, FAILED when the payload could not be converted or Apple reported an error). Choose a SendDataMode constant for dataMode.


int send_data_to_all_players(data: PackedByteArray, dataMode: int) 🔗

Broadcasts the packed bytes to everyone in the match. Return value matches send(). Use the SendDataMode constants.