Sign In Register

How can we help you today?

Start a new topic
Answered

how to return request data to a variable from a function in unity?

 

 Hi


I have a function giveSolution() which makes some stuff in cloud code and then returns the solution. The thing is that i would like to "return" the solution in a function:


public solution giveSolution(){


new GameSparks.Api.Requests.LogEventRequest().SetEventKey("giveSolution").Send((response) => {
  if (!response.HasErrors) {

  //prepare Solution


  return Solution;
  } else {

return null;

  }
  });


}


Solution sol1 = giveSolution(); << this is how i want to call the whole stuff


Problem is that sol1 will always be null, because unity just moves on and doesnt wait for the whole function to finish.


Is there a proper way to have it "just working" without worrying? I know i need to wait for the data, but is it possible to make the script wait until the whole thing is finished and then continue?


Best Answer

hi all,


I think the keyword you are looking for is "Synchronous" (e.g., blocking). Since GameSparks SDK mostly with non-blocking, async calls, you yourself would have to do the work to make an async method synchronous.


I would recommend the Async & Await and UniRX for these paradigms. They work amazingly: https://medium.com/@neuecc/async-await-support-for-unity-with-unirx-421a958408ed


Drew


Sorry for reviving this old post, but I see there was no solution posted. Did the OP fix this? I am having the same exact problem...

Hi Kelvin!

You can return data from the cloud to the client by setting the data you want to return as scriptData using the setScriptData method in the logEventRequests cloud code.

You can identify which Event in the logEventRequests cloud code like this:

var thisEvent = Spark.getData().eventKey;

Spark.setScriptData("name", "value");

if(thisEvent == "YOUR_EVENT")//identify the event
{
      Spark.setScriptData("name", "value");//set values to be returned as scriptData
}

Does that make sense?

Regards,
-Dave

 

No, this is not what i was asking for.

I already got that whole thing working. The correct data is send back. The problem is that "delay" (because the data has to go through the internet to my pc).

Here is the real code i want to get working:

Runes r1 = GameSparksHandler.instance.giveRune();
  if (r1!= null) {
  print("r1 has the correct data");
  } else {
  print("r1 is null");
  }
Problem: "r1 is null" is always getting printed... My Guess is because unity just goes on and doesnt wait for giveRune(); to finish

Ok so how to do this?

Here is my giveRune() code:

  public Runes giveRune() {
  Runes a = null;
  new GameSparks.Api.Requests.LogEventRequest().SetEventKey("giveRune").Send((response) => {
  if (!response.HasErrors) {
  GameSparks.Core.GSData data = response.ScriptData.GetGSData("player_Data");
  int hp = (int)data.GetInt("hp");
  a = new Runes(hp);
  } else {
  Debug.Log("error");
  }
  });
  return a;
  }



This is not how asynchronous work in unity is done.


Your second function will always return null, since the delegate will only be called when the request done. The easy but dirty way would be to do sth like this.


void giveRune()

{

 new GameSparks.Api.Requests.LogEventRequest().SetEventKey("giveRune").Send((response) => {
  if (!response.HasErrors) {
  GameSparks.Core.GSData data = response.ScriptData.GetGSData("player_Data");
  int hp = (int)data.GetInt("hp");

  doSthWithRune(new Runes(hp));

  } else {
  Debug.Log("error");
  }
  });

}


void doSthWithRune(Rune r)

{

// work with your rune here

}


this code is not tested but this could be one possible workflow.

I was wondering if someone was able to make it work with UniRX. Just added the plugin to Unity, but not sure how to use the syntax...


Thanks!

 I didnt fix it. I did something different but looking at this problem now i found a solution to "do" it.


If we take my example from above:


The plan is to use a Coroutine and make a loop until the request is done.


requestRuneFromGamesparks();

while(true){

if(flagFromRequestisSet){

break;

}

yield;

}


Solution = solutionFromRequest;


i just did it rough but if you have more questions ill try to answer them


Answer

hi all,


I think the keyword you are looking for is "Synchronous" (e.g., blocking). Since GameSparks SDK mostly with non-blocking, async calls, you yourself would have to do the work to make an async method synchronous.


I would recommend the Async & Await and UniRX for these paradigms. They work amazingly: https://medium.com/@neuecc/async-await-support-for-unity-with-unirx-421a958408ed


Drew

Login to post a comment