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
C
Customer Support
said
over 8 years ago
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:
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:
Hope this helps. Please note there are some Unity specific tutorials getting published next week.
B
Bryan Mackenzie
said
over 8 years ago
Perfect! Thanks for the detailed reply :)
M
Michal Sefl
said
about 8 years ago
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!
A
AlphaDog Ware
said
over 4 years ago
4 years and still no reference to this in the tutorial docs?
A
AlphaDog Ware
said
over 4 years ago
This is awesome! How is this not in some tutorial yet? I couldn't find anything this helpful in regards to currency anywhere. Thank you very much!
Bryan Mackenzie
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
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.
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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.
Bryan Mackenzie
Perfect! Thanks for the detailed reply :)
Michal Sefl
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!
AlphaDog Ware
4 years and still no reference to this in the tutorial docs?
AlphaDog Ware
This is awesome! How is this not in some tutorial yet? I couldn't find anything this helpful in regards to currency anywhere. Thank you very much!
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics