Giving and removing built in Currency from Player ...Is the answer to this forum question still valid?
M
Maria Mercedes Martinez
started a topic
about 6 years ago
I have followed the Unity Virtual Goods tutorial and the Achievements tutorial and then started to search information about how to give/take Currency vs Virtual Goods. I wanted to try to give/take currency without using achievement as a trigger.
It says it is better to use the built in Currency1,2,3, etc than to create virtual goods as your currency.
But the answer is 2 years old. Is the answer still valid and should I follow that mini tutorial in the forum post or is there now a newer better way to give and take currency?
Best Answer
C
Customer Support
said
about 6 years ago
Hi Maria,
When you configure a Virtual Good on the portal the value you enter for currency 1-6 will be the amount debited from a player when they buy that good using a BuyVirtualGoodsRequest, the system will take care of transaction details for you, you'll just need to specify what currency to use and the quantity of goods to buy. This is the simplest way to handle your Virtual Good transactions.
You can also use SparkPlayer calls to add or remove currency from a player in Cloud Code. You can read more about SparkPlayer here.
//get the current balance for a given currency for the current player
var bal = Spark.getPlayer().getBalance1();
//credit the current player with 20 x currency1
Spark.getPlayer().credit1(20);
//debit the current player 5 x currency1
Spark.getPlayer().debit1(5);
If you were to use Virtual Goods as a currency for your players (maybe for trading goods between players) you could handle this in Cloud Code with the following.
//check the quantity of the Virtual Good on the current player
var userName = Spark.getPlayer().hasVGood(vgShortCode);
//add 10 of the given Virtual Good to the current player
var userName = Spark.getPlayer().addVGood(vgShortCode, 10);
//remove 10 of the given Virtual Good from the current player
var userName = Spark.getPlayer().useVGood(vgShortCode, 10);
Adding some logic to this in Cloud Code you could have a trade event that could check if the player had enough of a given Virtual Good to trade with using "hasVgood" and if they do and want to go ahead with the trade, use "useVgood" to remove it from the player and so on. Hope this helps, if you have any other questions just let me know.
When you configure a Virtual Good on the portal the value you enter for currency 1-6 will be the amount debited from a player when they buy that good using a BuyVirtualGoodsRequest, the system will take care of transaction details for you, you'll just need to specify what currency to use and the quantity of goods to buy. This is the simplest way to handle your Virtual Good transactions.
You can also use SparkPlayer calls to add or remove currency from a player in Cloud Code. You can read more about SparkPlayer here.
//get the current balance for a given currency for the current player
var bal = Spark.getPlayer().getBalance1();
//credit the current player with 20 x currency1
Spark.getPlayer().credit1(20);
//debit the current player 5 x currency1
Spark.getPlayer().debit1(5);
If you were to use Virtual Goods as a currency for your players (maybe for trading goods between players) you could handle this in Cloud Code with the following.
//check the quantity of the Virtual Good on the current player
var userName = Spark.getPlayer().hasVGood(vgShortCode);
//add 10 of the given Virtual Good to the current player
var userName = Spark.getPlayer().addVGood(vgShortCode, 10);
//remove 10 of the given Virtual Good from the current player
var userName = Spark.getPlayer().useVGood(vgShortCode, 10);
Adding some logic to this in Cloud Code you could have a trade event that could check if the player had enough of a given Virtual Good to trade with using "hasVgood" and if they do and want to go ahead with the trade, use "useVgood" to remove it from the player and so on. Hope this helps, if you have any other questions just let me know.
Thanks,
Liam
M
Maria Mercedes Martinez
said
about 6 years ago
Thanks for clearing this up for me Liam!
M
Maria Mercedes Martinez
said
almost 6 years ago
One more question Liam,
How can I pass the amount of currency I want to be added to the player's Currency1 into CloudCode from Unity?
Right now I can add currency by creating events and then in CC using the credit method like you taught me above, like so
Spark.getPlayer().credit1(20);
But that means I have to create a new event for every different amount I want to credit or debit.
Can I create an event called ADD_CURR and set an Attribute of "Coin_Curr" (That represents Currency1) and set it to script...
and create another attribute called "Gems" (That represents Currency2) and set it to script...
And then in Unity write something like this
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("ADD_CURR")
.SetEventAttribute("Coin_Curr", 1)
.SetEventAttribute("Gem_Curr", 4)
.Send((response) => ETC...
And then in the CloudCode for the ADD_CURR event add something like
var playerID = Spark.getPlayer();
var playerStones = Spark.getData().Gem_Curr;
and set up a mongo Update?
OR maybe
Spark.getPlayer().credit1(Coin_Curr);
Spark.getPlayer().credit2(Gem_Curr);
I know how to do it through Events/Achievements but not directly through a nice clean Spark Player call, is it possible?
Thanks for your continued help
M
Maria Mercedes Martinez
said
almost 6 years ago
AH I figured it out!
In Unity:
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("ADD_CURR")
.SetEventAttribute("Gem_Curr", 1)
.Send((response) =>
{ ETC...
Create an event ADD_CURR
and add an attribute called Gem_Curr andhave it "Used in Script"
Yes that's it, supply the number in the event from Unity, then in Cloud Code use Spark.getData() to get the value of attribute that was passed in and use it as the amount to credit with Spark.getPlayer().credit. If you have any other questions don't hesitate to ask.
Maria Mercedes Martinez
I have followed the Unity Virtual Goods tutorial and the Achievements tutorial and then started to search information about how to give/take Currency vs Virtual Goods. I wanted to try to give/take currency without using achievement as a trigger.
I came across this post:
https://support.gamesparks.net/support/discussions/topics/1000038536
It says it is better to use the built in Currency1,2,3, etc than to create virtual goods as your currency.
But the answer is 2 years old. Is the answer still valid and should I follow that mini tutorial in the forum post or is there now a newer better way to give and take currency?
Hi Maria,
When you configure a Virtual Good on the portal the value you enter for currency 1-6 will be the amount debited from a player when they buy that good using a BuyVirtualGoodsRequest, the system will take care of transaction details for you, you'll just need to specify what currency to use and the quantity of goods to buy. This is the simplest way to handle your Virtual Good transactions.
You can also use SparkPlayer calls to add or remove currency from a player in Cloud Code. You can read more about SparkPlayer here.
If you were to use Virtual Goods as a currency for your players (maybe for trading goods between players) you could handle this in Cloud Code with the following.
Adding some logic to this in Cloud Code you could have a trade event that could check if the player had enough of a given Virtual Good to trade with using "hasVgood" and if they do and want to go ahead with the trade, use "useVgood" to remove it from the player and so on. Hope this helps, if you have any other questions just let me know.
Thanks,
Liam
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
Hi Maria,
When you configure a Virtual Good on the portal the value you enter for currency 1-6 will be the amount debited from a player when they buy that good using a BuyVirtualGoodsRequest, the system will take care of transaction details for you, you'll just need to specify what currency to use and the quantity of goods to buy. This is the simplest way to handle your Virtual Good transactions.
You can also use SparkPlayer calls to add or remove currency from a player in Cloud Code. You can read more about SparkPlayer here.
If you were to use Virtual Goods as a currency for your players (maybe for trading goods between players) you could handle this in Cloud Code with the following.
Adding some logic to this in Cloud Code you could have a trade event that could check if the player had enough of a given Virtual Good to trade with using "hasVgood" and if they do and want to go ahead with the trade, use "useVgood" to remove it from the player and so on. Hope this helps, if you have any other questions just let me know.
Thanks,
Liam
Maria Mercedes Martinez
Thanks for clearing this up for me Liam!
Maria Mercedes Martinez
One more question Liam,
How can I pass the amount of currency I want to be added to the player's Currency1 into CloudCode from Unity?
Right now I can add currency by creating events and then in CC using the credit method like you taught me above, like so
Spark.getPlayer().credit1(20);
But that means I have to create a new event for every different amount I want to credit or debit.
Can I create an event called ADD_CURR and set an Attribute of "Coin_Curr" (That represents Currency1) and set it to script...
and create another attribute called "Gems" (That represents Currency2) and set it to script...
And then in Unity write something like this
And then in the CloudCode for the ADD_CURR event add something like
I know how to do it through Events/Achievements but not directly through a nice clean Spark Player call, is it possible?
Thanks for your continued help
Maria Mercedes Martinez
AH I figured it out!
In Unity:
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("ADD_CURR")
.SetEventAttribute("Gem_Curr", 1)
.Send((response) =>
{ ETC...
Create an event ADD_CURR
and add an attribute called Gem_Curr andhave it "Used in Script"
And In Cloud Code
Spark.getPlayer().credit1(Spark.getData().Gem_Curr);
Awesome. Gamesparks I love you.
Customer Support
Hi Maria,
Yes that's it, supply the number in the event from Unity, then in Cloud Code use Spark.getData() to get the value of attribute that was passed in and use it as the amount to credit with Spark.getPlayer().credit. If you have any other questions don't hesitate to ask.
Thanks,
Liam
-
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