Sign In Register

How can we help you today?

Start a new topic
Answered

How to convert data from ChallengeInstance on cloud code to Challenge on the front end in Unity?

I wrote some cloud code that would find all running challenges, issued challenges, and received challenges.


It creates 4 lists (your turn running, opponent turn running, issued, and received) and sets them as lists of challenges in the script data.


Now on the front end I am having trouble getting access to these lists as List<Challenge> types.  


I access the lists by doing the following:


List<object> response.ScriptData.GetObjectList("issuedChallenges");


And that is getting me the list.  However it is a generic object, and I want to iterate over this, but I want to access the items as the Challenge objects that they are so I can get the data from them easily.


How do I go about doing this?  I can't find out where on the namespace (if it exists) where the Challenge object is in unity.  


The way I was previously doing this was doing a normal challenge request to get the running ones, then doing a foreach on the response.ChallengeInstances, however since I'm doing it this other way now (so that I can get all the challenge types in 1 request, instead of having to "nest" 3 different requests), it isn't working the same way since it's not a Challenge type like it is in the response.ChallengeInstance when iterating over it.


Any suggestions?


Best Answer

Hi Andrew


If the issuedChallenges is an array of our standard challenge json objects then you can do the following to get it into our _Challenge objects:


List<object> items = response.ScriptData.GetObjectList("issuedChallenges");
foreach(object item in items){
    ListChallengeResponse._Challenge challenge = new ListChallengeResponse._Challenge (new GSData ((IDictionary<string,object>)item));
}


It's a bit ugly, but it will work.


Gabriel


Hi Andrew, I could be late, but this may help someone else.


https://docs.gamesparks.net/documentation/request-api/challenges-request-api/listchallengerequest


Recently they added states attribute (earlier we need to run for each - i wrote a cloud code for doing that fetch on cloud - https://gist.github.com/manwithsteelnerves/601acda9967c0008c3d4#file-gamesparks-list-challenge-response - now this is no more required :B )


now "states" is a variable which takes array of states . So you can send the request with the list of expected states you want. Let me know if you face any trouble.




Answer

Hi Andrew


If the issuedChallenges is an array of our standard challenge json objects then you can do the following to get it into our _Challenge objects:


List<object> items = response.ScriptData.GetObjectList("issuedChallenges");
foreach(object item in items){
    ListChallengeResponse._Challenge challenge = new ListChallengeResponse._Challenge (new GSData ((IDictionary<string,object>)item));
}


It's a bit ugly, but it will work.


Gabriel

Login to post a comment