how to return request data to a variable from a function in unity?
K
Kelvin Petry
started a topic
about 6 years ago
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
D
Drew Ferraro
said
over 5 years ago
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.
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...
Customer Support
said
about 6 years ago
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
K
Kelvin Petry
said
about 6 years ago
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; }
F
Fabian Kuhn
said
about 6 years ago
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.
P
Pr
said
about 5 years ago
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!
K
Kelvin Petry
said
over 5 years ago
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
D
Drew Ferraro
said
over 5 years ago
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.
Kelvin Petry
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?
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
- Oldest First
- Popular
- Newest First
Sorted by PopularKyle Stankovich
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...
Customer Support
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
Kelvin Petry
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;
}
Fabian Kuhn
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.
Pr
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!
Kelvin Petry
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
Drew Ferraro
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
-
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