Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Calling a jslib function from jspre code.

Discussion in 'Web' started by AdhikS, Apr 15, 2024.

  1. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    I am trying to call a function defined in the .jslib from a .jspre file
    Example
    My jslib file looks like

    Code (JavaScript):
    1. var NativeLib = {
    2.  
    3. OnPeerAdded : function(peer)
    4.     {
    5.         //Send peer info to unity
    6.     }
    7.  
    8. };
    9. mergeInto(LibraryManager.library, NativeLib);
    And my jspre code looks like|
    Code (JavaScript):
    1. function OnPeerAdded(peerInfo)
    2. {
    3.     //Need to call .jslib OnPeerAdded here
    4. }
    How can I achieve that?
     
  2. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
  3. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    Thanks for the quick reply @unityruba
    I have tested this link and its working fine in case I wanted to invoke any message from js to unity : https://docs.unity3d.com/Manual/web-interacting-browser-unity-to-js.html
    But there is no example which shows how to call a function of .jslib from .jspre file

    Also I tried something like this

    Code (JavaScript):
    1. function callMergeStrings() {
    2.     const str1 = 'Hello';
    3.     const str2 = 'World';
    4.  
    5.     // Allocate memory for the strings
    6.     const str1Ptr = Module._malloc(lengthBytesUTF8(str1) + 1);
    7.     const str2Ptr = Module._malloc(lengthBytesUTF8(str2) + 1);
    8.  
    9.     // Copy the strings to the allocated memory
    10.     stringToUTF8(str1, str1Ptr, lengthBytesUTF8(str1) + 1);
    11.     stringToUTF8(str2, str2Ptr, lengthBytesUTF8(str2) + 1);
    12.  
    13.     // Call the mergeStrings function from MyLibrary.jslib
    14.     const mergedStringPtr = Module.ccall(
    15.         'SendMessageToUnity',      // Name of the function in the .jslib file
    16.         'number',            // Return type (pointer to a string)
    17.         ['number', 'number'], // Parameter types (pointers to char arrays)
    18.         [str1Ptr, str2Ptr]    // Arguments passed to the function
    19.     );
    20.  
    21.     // Read the merged string from the pointer
    22.     const mergedString = Pointer_stringify(mergedStringPtr);
    23.     console.log('Received merged string:', mergedString);
    24.  
    25.     // Free the allocated memory
    26.     Module._free(str1Ptr);
    27.     Module._free(str2Ptr);
    28.     Module._free(mergedStringPtr);
    29. }
    this is a function in a .jspre file which merge 2 strings and call a method "SendMessageToUnity" which exist in .jslib
    But for some reason its not working.

    I will gather some more info, but meanwhile can you provide me some hint or a solution for fixing the above problem.
     
  4. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    563
  5. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    @Marks4 Thanks for the reference
    Can you tell me how can i call this function of jspre?

    Code (JavaScript):
    1. Module['CoolJsPlugin'] = Module['CoolJsPlugin'] || {};
    2. Module['CoolJsPlugin'].RunCoolFunction = function () {
    3.     _CoolFunction();
    4. }
    Suppose I have a function which wants to call RunCoolFunction.
    Should I simply call like RunCoolFunction()?
     
  6. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    @Marks4
    I have tried the solution that you suggested
    This is my jspre file


    Code (JavaScript):
    1. Module['CoolJsPlugin'] = Module['CoolJsPlugin'] || {};
    2. Module['CoolJsPlugin'].RunCoolFunction = function () {
    3.     _CoolFunction();
    4. }
    5.  
    6. window.CoolJsPlugin = Module['CoolJsPlugin'];
    7.  
    8. function RunTest()
    9. {
    10.     if (CoolJsPlugin && CoolJsPlugin.RunCoolFunction)
    11.     {
    12.         console.log("Calling Cool Function");
    13.         CoolJsPlugin.RunCoolFunction();
    14.     } else
    15.     {
    16.         console.error("CoolJsPlugin or RunCoolFunction is not available.");
    17.     }
    18. }
    I am calling RunTest from the .jslib but its giving me error at


    Code (JavaScript):
    1. Module['CoolJsPlugin'].RunCoolFunction = function () {
    2.     _CoolFunction();
    3. }
    Says _CoolFunction is not defined

    Am I doing something wrong here?
     
  7. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
    Sorry for misunderstanding your original question! I'll see if we can add even more examples to that page that cover this case.

    Generally, the names merged into the library generates a function name prefixed with a `_` (see this Emscripten note)
    If they're defined starting with a `$` will have that character stripped and no underscore added.

    With that being said, how did you define `CoolFunction`? can you share that code? did you do it exactly as the other thread?

    Code (JavaScript):
    1. var CoolJsPlugin= {
    2.   $CoolContainer: {
    3.       data: "data",
    4.   },
    5.   CoolFunction: function() {
    6.     console.log("Cool");
    7.   },
    8. };
    9. autoAddDeps(CoolJsPlugin, '$CoolContainer');
    10. mergeInto(LibraryManager.library, CoolJsPlugin);
     
  8. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
  9. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
    ah, you're just missing the `$` then:
    Code (JavaScript):
    1. $CoolFunction: function() {
    2.     console.log("Cool");
    3.   },
    4.  
    5.  
    then you should see it generated as `CoolFunction` instead of `_CoolFunction` :)
     
  10. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    @unityruba
    thanks for the suggestion
    let me try
    will let you know if this works
     
    unityruba likes this.