Sign In Register

How can we help you today?

Start a new topic
Answered

SparkPlayer

Just started with GameSparks and loving the potential! 


I have however hit a wall. I've been reading the documentation and feel it needs more Unity / C# examples. My issue is that I am unsure and unable to find the answer to getting a handle to the player, documented here (https://docs.gamesparks.net/documentation/cloud-code-api/spark-cloud-code-api/sparkplayer). I have set up the virtual currency 1 and 2, and want to be able to add and retrieve the values as documented in the link above. I have the Unity GameSparks TestUI sample, and have also looked through the source provided but am missing the answer. Any help is greatly appreciated. 


Thanks, 

Bryan


Best Answer

Assuming, I have 2 currencies where Currency 1 is Silver and Currency 2 is Gold.  Your question is around setting these up and then incrementing them from within Unity.

Firstly, in the GameSparks development portal I notice that you have in fact set up 2 virtual goods to act as the currency.  I don’t think you need to do this.  There are always up to 6 currencies you can use for anything you want without having to define any new virtual goods.  I know this can be confusing and there is a pending item on the backlog which will allow you to name these 6 currencies anything you want.  In this example: Gold and Silver.

To give a player more Silver, we need to use the Spark API call:  Spark.getPlayer().credit1(X) where X is the amount you want to increment.  To give a player more Gold, we would use:  Spark.getPlayer().credit2(Y)

The Spark API is only available through cloud code - you don’t make calls to it from within Unity directly.  To call the Spark API from within Unity, you should define a custom event on the GameSparks portal and then set up some cloud code which will get executed when the new event is called.  The custom event can then be called from within Unity.  Broadly, you would follow these steps:

1. Set up an event called AddCurrency and give it 2 parameters (numbers): currencyRef and amount 
2. Edit the AddCurrency event in the Cloud Code section of the configurator and add the following code:

var currencyRef = Spark.getData().currencyRef;
var amount = Spark.getData().amount;

if (currencyRef == 1) {
    Spark.getPlayer().credit1(amount);
} else if (currencyRef == 2) {
    Spark.getPlayer().credit2(amount);
}

Now, before adding the call to the new event in Unity, test that is all works in the Test Harness.  
1. Under authentication, Register a new player
2. Authenticate the new player
3. Under Player, execute AccountDetailsRequest and verify the player has no currency
4. Execute the AddCurrency event using the LogEvent menu item
5. Execute the AccountDetailsRequest again and verify that currency was added to the player

When this is working, you are ready to add the code into Unity.  Please note I am assuming you are using the new version of the SDK I mailed to you.

All you need to do to invoke the new event is the following:

void AddCurrency (int currencyRef, int amount) {

Debug.Log ("Calling AddCurrency ... ");

LogEventResponse logEvtResponse = new LogEventRequest ().SetEventKey ("AddCurrency").SetEventAttribute ("currencyRef",currencyRef).SetEventAttribute ("amount",amount).Send ();

if (logEvtResponse.HasErrors) {
Debug.Log("Log message failed ...");
} else {
Debug.Log("Log message suceeded ..." + logEvtResponse);
}
}


Hope this helps.  Please note there are some Unity specific tutorials getting published next week.




Answer

Assuming, I have 2 currencies where Currency 1 is Silver and Currency 2 is Gold.  Your question is around setting these up and then incrementing them from within Unity.

Firstly, in the GameSparks development portal I notice that you have in fact set up 2 virtual goods to act as the currency.  I don’t think you need to do this.  There are always up to 6 currencies you can use for anything you want without having to define any new virtual goods.  I know this can be confusing and there is a pending item on the backlog which will allow you to name these 6 currencies anything you want.  In this example: Gold and Silver.

To give a player more Silver, we need to use the Spark API call:  Spark.getPlayer().credit1(X) where X is the amount you want to increment.  To give a player more Gold, we would use:  Spark.getPlayer().credit2(Y)

The Spark API is only available through cloud code - you don’t make calls to it from within Unity directly.  To call the Spark API from within Unity, you should define a custom event on the GameSparks portal and then set up some cloud code which will get executed when the new event is called.  The custom event can then be called from within Unity.  Broadly, you would follow these steps:

1. Set up an event called AddCurrency and give it 2 parameters (numbers): currencyRef and amount 
2. Edit the AddCurrency event in the Cloud Code section of the configurator and add the following code:

var currencyRef = Spark.getData().currencyRef;
var amount = Spark.getData().amount;

if (currencyRef == 1) {
    Spark.getPlayer().credit1(amount);
} else if (currencyRef == 2) {
    Spark.getPlayer().credit2(amount);
}

Now, before adding the call to the new event in Unity, test that is all works in the Test Harness.  
1. Under authentication, Register a new player
2. Authenticate the new player
3. Under Player, execute AccountDetailsRequest and verify the player has no currency
4. Execute the AddCurrency event using the LogEvent menu item
5. Execute the AccountDetailsRequest again and verify that currency was added to the player

When this is working, you are ready to add the code into Unity.  Please note I am assuming you are using the new version of the SDK I mailed to you.

All you need to do to invoke the new event is the following:

void AddCurrency (int currencyRef, int amount) {

Debug.Log ("Calling AddCurrency ... ");

LogEventResponse logEvtResponse = new LogEventRequest ().SetEventKey ("AddCurrency").SetEventAttribute ("currencyRef",currencyRef).SetEventAttribute ("amount",amount).Send ();

if (logEvtResponse.HasErrors) {
Debug.Log("Log message failed ...");
} else {
Debug.Log("Log message suceeded ..." + logEvtResponse);
}
}


Hope this helps.  Please note there are some Unity specific tutorials getting published next week.



Perfect! Thanks for the detailed reply :) 

Awesome. You should add this into Unity tutorials/docs. This was the most important tutorial for me in order to understand how the entire system works. Until I found it, I did read many articles but didn't really found this crucial piece of information. Thank you!

Login to post a comment