Sign In Register

How can we help you today?

Start a new topic

LogEventRequest async event in Unity

I am trying to create an async operation with Unity 2017.3, but I can't find a way to wait for the event to come back.


I have to use await Task.Delay(2000); for example to make it work, but I would like to be able to wait until the event is back (response has some data).


How can I do it?


Hi Pr,


Below I've provided an example of what I think you're trying to achieve. What this will do is initially send an AuthenticationRequest with a given username and password, once this request has returned in an async fashion it will check the response for errors. If the response contains one (such as the the account doesn't exists) it will then proceed to attempt a RegistrationRequest. This is very similar to how you could do this for a standard LogEventRequest. I hope this helps.


Kind regards,

 - Steve



 

private void Authenticate(
            string username,
            string password,
            Action<string, string> onReg,
            Action<string, string> onAuth)
        {
            new GameSparks.Api.Requests.AuthenticationRequest()
                .SetUserName(username)
                .SetPassword(password)
                .Send(r =>
            {
                if (!r.HasErrors) OnAuth(r.DisplayName, r.UserId, onAuth);
                else
                {
                    _authGui.AddLogEntry(GetError(r.Errors));
                    
                    new GameSparks.Api.Requests.RegistrationRequest()
                        .SetDisplayName(username)
                        .SetUserName(username)
                        .SetPassword(password)
                        .Send(r2 =>
                    {
                        if (r2.HasErrors) _authGui.AddLogEntry(GetError(r2.Errors));
                        else OnAuth(r2.DisplayName, r2.UserId, onReg);
                    });
                }
            });
        }

 

Thanks for your help!

 

Basically, OnAuth() is another method that will trigger when you get your message back?

Ok my previous post was not clear anough actually, sorry. This is not exactly what I need.


I'll try to explain a bit better:

I have created a class that contains all the log event to GS. I want this class methods to return a list of string, or return "response".


for example (the code is not functional as it is right now, I just wrote something quick to help you understand what I'm trying to achieve):


public static async Task<List<string>> GSRequestCharacters(){

    GS stuff...

    await ? (LogEventRequest...)

    return when the task is done;

}


The other method is something like that:


Public async void RequestCharacters(){

      List<string> list = LogRequester.GSRequestCharacters();

      Task.Wait(2000); <= I would like to avoid using that and wait correctly for the return.

      Debug.Log(...)

}


When I return the result of GSRequestCharacters(), it's always empty, because the method GSRequestCharacters() doesn't wait for the logEvent to come back (if I don't use a delay).


Using Coroutine doesn't allow me to pass parameters back in methods.


Thanks a lot!

just to let you know that I found the issue.


I needed to create a Task (TaskCompletionSource<bool> waitForResult = new TaskCompletionSource<bool>();) and wait for the task to be "completed".


It works now.


Thanks for your help!

Login to post a comment