The GameSparks SDK and the GameSparks Module

When using the Unreal SDK, it's important to be clear at the outset about how the SDK is designed to work with the GameSparks module:

The GameSparks module enables you to connect and disconnect to the backend and to monitor that connection.

The Unreal SDK offers you two ways of interacting with the GameSparks module - via the GameSparks Object or via the GameSparks Component:

The two different ways of connecting to the GameSparks module can be used together or separately.

How you choose to connect to the GameSparks module will depend on the type of game you are developing and the way in which the connection is made.

Connecting to the GameSparks Module via the GameSparks Object

The GameSparks Object:

Is declared and initialized in the Game Instance class.

Remains persistent throughout the game client's life.

Is a way of establishing a persistent connection to the GameSparks module.

Every client owns their own Game Instance, making this ideal for games with a dedicated server.

This way of connecting is more suited to games with a dedicated server, because all client instances and the server instance are separate and can establish and control their own connections.

Connecting to the GameSparks Module via the GameSparks Component

GameSparks Component:

Is declared and initialized in the Game Mode class.

Is a way of establishing a persistent connection to the GameSparks module.

When the level is changed, the component does not exist anymore, there needs to be a component for every level.

Only the server owns the single 'Game Mode' instance.

This way of connecting is quicker to set up, because you can package everything into a single Game Mode, without needing multiple classes that refer to the game Object from each game instance. However, this is not a recommended approach for games with a dedicated server, because players will not have access to the single Game Mode.

Includes

Add the following includes in your Game Mode or Game Instance header file:

Connection

After you've instantiated a GameSparks Object or a GameSparks Component, you can call the connection method. You'll need an availability call back to determine if your game is connected after invoking a connection.

Declare the OnGameSparksAvailable function handler, which will determine what happens when your module is connected or disconnected - we recommend a playerController.h class or the Gamemode.h:

//Function used to determine what happens if GameSparks connects or fails to (Needs to be UFUNCTION)
UFUNCTION()
void OnGameSparksAvailable(bool available);
//Add the response function to the Authentication requeststaticvoid AuthenticationRequest_Response(GameSparks::Core::GS&, const GameSparks::Api::Responses::AuthenticationResponse&);

In the body of the function, add some logic to handle your connection. In our example, we authenticate a player when the game is connected, which is only fit for testing:

GS Component

Retrieve the GS Component reference by getting it from the Game Mode.

//Get reference to Game Mode. If you want to implement this in the Game Mode, ignore this part
UGSGameMode* GSGM = (UGSGameMode*)GetWorld()->GetAuthGameMode();
//Add a function to the OnGameSparksAvailableDelegate invocation list
GSGM->GameSparksComp->OnGameSparksAvailableDelegate.AddDynamic(this, &AMyGameMode::OnGameSparksAvailable);
//Disconnected the module just incase it's connected (Refresh)
GSGM->GameSparksComp->Disconnect();
//Connect to GameSparks using Key and Secret
GSGM->GameSparksComp->Connect("key", "secret");

Setting up a Listener

To set up a listener, get a reference to the message listener object or to the message listener component and add a delegate and assign a function with the same parameters.