Visual C# Setup

The following steps will show in detail how to integrate the GameSparks SDK into a new Visual C# Project.

Downloading the new SDK

The GameSparks Visual C# SDK is provided as a standard NuGet package and can be downloaded here.

Importing the SDK

Windows

1. Open the Package Manager Console.

2. Type: Install-Package yourpath\GameSparksSDK.yourversion.nupkg.

3. Go to Project>Project Settings>Build>Conditional Compilation Symbols and add this at the end of the line:

MacOS

1. Open the Add NugGet Packages window from the Project menu.

2. Select the GameSparks SDK:

Configuring the SDK

To configure the SDK for your game:

1. Add the GSPlatform.cs file in your project.

2. You have to fix its namespace and then from line 208 to line 233 modify the ApiKey, ApiSecret, ApiCredential, ApiStage methods, all to reflect the settings of your GameSparks portal.

3. Finally you need to implement the initialization of the GameSparks SDK like this:

using GameSparks.Core;
using GameSparks.Api;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Api.Messages;

...

private GSPlatform platform;

private void InitGS()
{

//Verbose debugging log
GS.TraceMessages = false;

//Getting the GS platform
platform = new GSPlatform();

//Connect to the backend
GS.Initialise(platform);

//The function in charge of reacting towards GameSpark's availablity
GS.GameSparksAvailable = (available) =>
{
    //When connected to the backend do:
    if (available)
    {
        platform.DebugMsg("SDK is available");

        Login();
    }
    //Else if disconnected or unable to connect:
    else
    {
        platform.DebugMsg("SDK is not available");
    }
  };
}

private void Login()
{
//Authenticate a user to be able to use GS API
//You can set a response function in the send method parameter .send(function)
  new AuthenticationRequest().SetUserName("u").SetPassword("p").Send((authResponse) =>
  {
      new AccountDetailsRequest().Send((accountDetailsResponse) =>
      {
          platform.DebugMsg(accountDetailsResponse.DisplayName);
      });
  });
}

//Setting the onScriptMessage function as a listener for any Script Messages
GameSparks.Api.Messages.ScriptMessage.Listener += onScriptMessage;

//Example function
private void onScriptMessage(ScriptMessage message)
{
  //Access the data object in the message and find the string 'title'
  platform.DebugMsg(message.Data.GetString("title").ToString());
}

//Create a GSRequestData object
GSRequestData scriptData = new GSRequestData()
.AddNumber("myNumber", 10)
.AddString("myString", "the value");

//Passing the GSRequestData object as the request's scriptData
new RegistrationRequest().SetUserName("username").SetPassword("password").SetScriptData(scriptData).Send(func);

//Retrieving
long myNumber = response.ScriptData.GetNumber ("myNumber");
string myString = response.ScriptData.GetString ("myString");

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