Search Unity

Unity Jslib async problem

Discussion in 'Scripting' started by hromoyDron, Jan 15, 2020.

  1. hromoyDron

    hromoyDron

    Joined:
    Jan 24, 2013
    Posts:
    90
    Hello!

    I try to get value from async js function in index.html file. Next code work fine.
    Code (JavaScript):
    1. async function checkAG()
    2.     {
    3.       try
    4.       {
    5.         await ag.retrieveGame('user_id').then(function(response) {
    6.             var data = response.user_id;
    7.             return data;
    8.         });
    9.       }
    10.       catch(err)
    11.       {
    12.         return "error: " + err.name;
    13.       }
    14.     }
    I use jslib to call this function. I don't know right syntax for async in jslib and further calling from c#
    Code (JavaScript):
    1. mergeInto(LibraryManager.library,
    2. {
    3.  
    4.   getAGUID: function ()
    5.   {
    6.     var uid = GetUID();
    7.     var buffer = _malloc(lengthBytesUTF8(uid) + 1);
    8.     writeStringToMemory(uid, buffer);
    9.     return buffer;
    10.   },
    11.  
    12.   async checkAGUID: function ()
    13.   {
    14.     var data = await checkAG();
    15.     var buffer = _malloc(lengthBytesUTF8(data) + 1);
    16.     writeStringToMemory(data, buffer);
    17.     return buffer;
    18.   },
    19.  
    20.   async setAGUID: function (uid)
    21.   {
    22.     var data = await setAGUID(uid);
    23.     var buffer = _malloc(lengthBytesUTF8(data) + 1);
    24.     writeStringToMemory(data, buffer);
    25.     return buffer;
    26.   },
    27.  
    28. }
    29.  
    30. );

    And than call it in c#

    Code (CSharp):
    1. void Start()
    2. {
    3.         Debug.Log("check:" + checkAGUID());
    4. }
    5.  
    I understand that I should mark this method like async both in jslib and c#, but I don't know right way

    So this code return me errors during building
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I'm not sure this is even possible. At a minimum I imagine you would need to call it from something async-y on the C# side, but someone with more recent C# chops can chime in.

    My understanding is the only way to call from outside Unity back into Unity is with the UnitySendMessage() API.
     
  3. hromoyDron

    hromoyDron

    Joined:
    Jan 24, 2013
    Posts:
    90
    yes, as an option, I can call a separate function:
    Code (CSharp):
    1. unityInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');
    but I would still like to do this through async
    and nowhere can find examples of importing async from jslib

    it would be look strange if it is really impossible
     
  4. anukul

    anukul

    Joined:
    Oct 26, 2014
    Posts:
    2
    has anyone found an answer to this?
     
  5. unnanego

    unnanego

    Joined:
    May 8, 2018
    Posts:
    199
    It doesn't even build if I add async to a function in jslib((( Says it's missing a ")"
     
  6. tsushimayoshiko2000

    tsushimayoshiko2000

    Joined:
    Apr 28, 2021
    Posts:
    2
    I can't believe this problem actually exists lol
     
  7. WoodsFiend

    WoodsFiend

    Joined:
    Oct 2, 2017
    Posts:
    10
    I have had issues with the keyword async when using jslib. There is some workarounds out there. The most dynamic solution I have found is either use JS injection using new Function(async_here), or you could write your async code in a separate JS file and run it from the jslib call. Both involve using SendMessage to communicate to Unity.
     
  8. ateo_jan

    ateo_jan

    Joined:
    Jan 11, 2021
    Posts:
    5
    I don't know if this has also something to do with es6 support.
    It looks like the emscripten version that unity uses to process the jslib files, doesnt understand es6 syntax.
    For example Arrow functions also dont work
     
  9. WoodsFiend

    WoodsFiend

    Joined:
    Oct 2, 2017
    Posts:
    10
    If you want async inside your jslib you can either create the function at runtime from string using new Function(), This allows all ES6 functionality, though may have some security concerns. Otherwise you can use .then() syntax directly in jslib. No awaits, No arrow functions, no "let" because of emscripten ES6 not supported.

    If you wan't async callbacks to C# the best implementation is custom built using the SendMessage as suggested above. General flow would be: Call a jslib function that does some async task, implement a C# wait for variable to change, when the async sends the message to C# it sets the variable, your wait ends and you can continue.
     
    Marks4 likes this.