- Home /
Android Google Quest reward data file. How do I wright it in C#?
Hi! I have been stuck on this for days now:/...
When a user has accomplished a Quest in Google Play Games they should get a reward. In the google play console they want me to upload a file/script that get executed when the reward should be delivered. As an example they suggest a Json string like this:
{
"bonusGems": 2134,
"extraSeconds": 12
}
more info here: https://developers.google.com/games/services/common/concepts/quests#quest_basics
That doesnt help me much.. I want to wright it in C# if possible, but how exactly should the script look like? Lets say I just want to increase the players "gems" by 10.
// Erik
I have the same problem. No idea how to tackle this. Have you got it working yet?
No I didnt get it to work:/ Gave it a couple of hours but no luck... Please post if you solved it! //Erik
@Hoffa25 I submitted an answer that i used. It's waiting for moderation atm :)
I too have not been able to get this to work, not sure exactly if its simply something obvious that I am missing.... most lightly it is since no one has answered this question.
@Gokufuin The answer is up now, hope it is some help! If you are still unsure please let me know. And btw it wasn't just you who had trouble! I spent a good few days trying to find out how to do it! It definately is not obvious from the documentation i read.
Answer by 12boulla · Nov 02, 2015 at 06:42 PM
Ok, after hours of searching and asking on forums, i found the answer. This is the data text file that i submitted on the developer console:
{ "Gems" : 10 }
NOTE: This script is in a notepad document and is not c#, but you can use the value of gems, or whatever you want later...
Then in Unity in a C# script, you can get the value of whatever you set in the JSON script. This is how i did it: (I assume you know how to do the script to receive a reward for the quest, but if not i will put it in anyway)
NOTE: Using JSONObject plugin from the asset store
How to get the value:
string reward = System.Text.Encoding.UTF8.GetString(quest.Milestone.CompletionRewardData);
JSONObject rewardJson = new JSONObject(reward);
int gems = int.Parse(rewardJson.GetField("Gems").ToString());
Using it in context:
private void ClaimMilestone(IQuestMilestone toClaim)
{
PlayGamesPlatform.Instance.Quests.ClaimMilestone(toClaim,
(QuestClaimMilestoneStatus status, IQuest quest, IQuestMilestone milestone) => {
if (status == QuestClaimMilestoneStatus.Success)
{
string reward = System.Text.Encoding.UTF8.GetString(quest.Milestone.CompletionRewardData);
JSONObject rewardJson = new JSONObject(reward);
int gems = int.Parse(rewardJson.GetField("Gems").ToString());
PlayerPrefs.SetInt("Gems", PlayerPrefs.GetInt("Gems", 0) + gems);
}
});
}
I hope this helps! I know i was terribly stuck on this, but it felt great to get it solved. If you need any help just comment and i'll see if i can help you. (I hope i was clear in my answer, but if not, tell me!)
Here are some other forum questions i asked that you can look at aswell to help you:
Something I still don't understand is why my code does not recognize the 'JSONObject' ?
Any thoughts on that guys?
@12boulla Im working on other stuff at the moment so I cant check if boullas solution does the trick or not... But it looks promissing:)! As soon as someone confirmes that it works I will mark this question answered. //Erik
It does work very well!
However, when setting the variable in the JSON file, you have to pay attention to the Quotation marks you use in the file.
The "curvy" quotation marks won't work and will give you a error on LogCat. It must be the straight ones, like the one 12boulla used in his example.