Unreal CPP QuickStart

Project.build.cs

In your build file include the following:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameSparks", "OnlineSubsystem" });

Make sure GameSparks and OnlineSubsystem are both included.

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:

Connecting to the GameSparks Module via the GameSparks Object

The GameSparks Object:

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:

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:

 #include "GameSparks/Private/GameSparksComponent.h"
 #include "GameSparksModule.h"
 #include <GameSparks/GS.h>
 #include <GameSparks/generated/GSResponses.h>
 #include <GameSparks/generated/GSRequests.h>

Initialization

Whether you're using the GameSparks Object or the GameSparks Component, you'll need to initialize an instance of the class to be able to use it in your code.

GameSparks GameObject

The GameSparks Game Object lives in the GameInstance class.

GameInstance.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GSObjects")
 UGameSparksObject* GSObj;
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GSObjects")
 UGSMessageListenersObject* GSMessageListener;

GameInstance.CPP
//Instantiate both objects
    GSObj = NewObject<UGameSparksObject>(this,UGameSparksObject::StaticClass());
    GSMessageListener = NewObject<UGSMessageListenersObject>(this, UGSMessageListenersObject::StaticClass());

GameSparks Component

The GameSparks Component lives in the GameMode class.

GameMode.h

UGameSparksComponent* GameSparksComp;

GameMode.cpp

In your GameMode's constructor, initialize the GameSparks Component:


AMyGameMode::AMyGameMode() {

    //Initialize GameSparksComponent used to connect
    GameSparksComp = CreateDefaultSubobject<UGameSparksComponent>(TEXT("GSparkComp"));

}

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 request
static void 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:

void AGSPlayerController::OnGameSparksAvailable(bool available)
{
    if (available)
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("Connected"));

        GameSparks::Core::GS& gs = UGameSparksModule::GetModulePtr()->GetGSInstance();

        GameSparks::Api::Requests::AuthenticationRequest authRequest(gs);
        authRequest.SetUserName("ExampleUserName");
        authRequest.SetPassword("ExamplePassword");

        authRequest.Send(AuthenticationRequest_Response);
    }

}

//The response function to the Authentication Request
/Example response function
void AMyGameMode::AuthenticationRequest_Response(GameSparks::Core::GS&, const GameSparks::Api::Responses::AuthenticationResponse& response)
{
    GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Red, response.GetJSONString().c_str());
    //Check is response has no errors
    if (!response.GetHasErrors()) {
        GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("Auth response successful"));

        GameSparks::Core::GS& gs = UGameSparksModule::GetModulePtr()->GetGSInstance();
        GameSparks::Api::Requests::AccountDetailsRequest accDetRequest(gs);
        //If no errors then send an accounts details request
        accDetRequest.Send();

    }
}

Now that we've configured what's going to happen when we successfully connect, let's connect to the module through the GameSparks Object or GameSparks Component.

GS Game Object

Retrieve the GS Game Object reference by getting the Game Instance and using it anywhere in your code:


//Get the GS game instance class
    UGSGameInstance* GSI = Cast<UGSGameInstance>(GetGameInstance());

    //Set the OnAvailable delegate
        GSI->GSObj->OnGameSparksAvailableDelegate.AddDynamic(this, &AGSPlayerController::onAvailable);
        //Disconnected the module just incase it's connected (Refresh)
        GSI->GSObj->Disconnect();
        //Connect module
        GSI->GSObj->Connect("key", "secret");

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.

Example function:


//Has to be UFunction
UFUNCTION(BlueprintCallable, Category = "GameSparks")
    void onScriptMessage(FGSScriptMessage message);

Assign the function to the list of functions to invoke when a new message is received. In this example, we're listening for OnScript messages:


//Set a listener for OnScriptMessage
        GSI->GSMessageListener->OnScriptMessage.AddDynamic(this, &AGSPlayerController::onScriptMessage);

Example function body:


void AGSPlayerController::onScriptMessage(FGSScriptMessage message) {
    //Handle script message
    GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("Message!"));
    const FString test = message.Data->GetString("title");
    GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red,test );
}

C++ GameSparks Functions! Refer to the CPP Quick Start Guide for simple C++ GameSparks functions.

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here