Sign In Register

How can we help you today?

Start a new topic
Answered

Difference between GetGSDataList and deprecated GetObjectList

I'm learning how players can Friend each other in my game. I have gone through this tutorial here (https://support.gamesparks.net/support/discussions/topics/1000053058) and was able to see it work in my test harness. 

Now I am integrating it into Unity/C# and have come up with a question.

Right now I am trying to get the search player to work in the game. I created a Friending script so when player inputs a name like shane and clicks the search button, the player can see a list of all the shanes in the game.

  

public class Friending : MonoBehaviour 
{
	public InputField searchItemInput;
	
	public List<object> afriendList = new List<object>();
 
        Dictionary<string, object> afriendslist;
	
	public void searchFriend()
	{
		string friend = searchItemInput.text;
		new GameSparks.Api.Requests.LogEventRequest().SetEventKey("findPlayer")
		.SetEventAttribute("displayName", friend) 
		.SetEventAttribute("offset", 0)
		.Send((response) =>
          {
              if (!response.HasErrors)
			  
			  {
				foreach (Dictionary<string, object> afriend in response.ScriptData.GetObjectList("player"))
					{
						Debug.Log(afriend["playerName"]);
					}					
			  }
			  else
			  {
				 Debug.Log("There are errors " + response.Errors); 
			  }
		  });
	}

 This works, I get shane, shane2 and shane3 (I made 3 different shanes so I could simulate a real search) 


My problem:    My VSCode tells me that GetObjectList is deprecated and I should use GetGSDataList but when I change it I get an error.


I can't use foreach... Pretty sure I need to go back to my C# books but I'm hoping someone could point me in the right direction.

 


Best Answer

Sorry I was mixing the deprecated method to get nested objects and the new way. 

foreach(GSData aFriend in response.ScriptData.GetGSDataList("player")){
    string playerName = aFriend.GetString("playerName");
}

   

Basically, (and this goes back to my point prior that they made it better to get nested values), GSData objects can be nested even inside lists, so you can always use the GSData methods to get at the root values. Notice how inside the foreach loop, I'm dealing with GSData objects again, so I can just call GetString (or GetInt or whatever the type the value is).


Hi Ryan,


Thanks for taking a look at this for me. You solution is giving me this error :


 

List<GSData>' does not contain a definition for 'BaseData' and no extension method 'BaseData' accepting a first argument of type 'List<GSData>' could be found (are you missing a using directive or an assembly reference

 And yes parsing nested GSData is such a pain... I'm going to try to post how I do it to see what people think. 


In the meantime, not sure what reference I should use to solve this GSDataList issue. I have tried all the usings

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using GameSparks.Core;

using System.Collections.Generic;

using GameSparks.Api;

using System.Configuration;

using GameSparks.Api.Requests;

using GameSparks.Api.Responses; 


and I still get the error.


Thanks again for time!


Ryan! I can't thank you enough for helping me to learn this. 

This works and this info is eye opening. 

"GSData objects can be nested even inside lists, so you can always use the GSData methods to get at the root values"


So much to learn! :)

Just wanted to add, I understand that the problem is that I can't turn a GSData object into a generic List object. 

I would like to know if there is a way to make this work using GetGSDataList without having to create a whole different script to parse it correctly. 


Awesome! Glad I could help.

 I just typed this up here but I think something like this should work:

foreach (KeyValuePair<string, object> afriend in response.ScriptData.GetGSDataList("player").BaseData){
    Debug.Log(afriend.Value);
}


Btw figuring out how to parse nested GSData objects correctly is a total pain and that they changed it recently doesn't help. Though I think the change is good if you have arrays of nested GSData objects of arrays of etc.


Answer

Sorry I was mixing the deprecated method to get nested objects and the new way. 

foreach(GSData aFriend in response.ScriptData.GetGSDataList("player")){
    string playerName = aFriend.GetString("playerName");
}

   

Basically, (and this goes back to my point prior that they made it better to get nested values), GSData objects can be nested even inside lists, so you can always use the GSData methods to get at the root values. Notice how inside the foreach loop, I'm dealing with GSData objects again, so I can just call GetString (or GetInt or whatever the type the value is).

Login to post a comment