Search Unity

Question [jslib][scope] How to call a function from another function in the same library?

Discussion in 'Scripting' started by _watcher_, Jul 24, 2021.

  1. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    including jslib code in unity project:
    Code (csharp):
    1.  
    2. var MyCollection = {
    3.     funcA: function()
    4.     {
    5.         funcB();
    6.     },
    7.     funcB: function()
    8.     {
    9.         // funcB body here
    10.     },
    11. };
    12.  
    13. mergeInto(LibraryManager.library, MyCollection);
    14.  
    Then including the jslib functions in the cs file and running
    Code (csharp):
    1.  
    2. funcA();
    3.  
    Throws error in WebGL build:
    Code (csharp):
    1.  
    2. exception thrown: ReferenceError: funcB is not defined,_funcA@blob:http://localhost......................
    3.  
    How can i access funcB from funcA when this code runs in browser?
     
    Meltdown likes this.
  2. hadtofall

    hadtofall

    Joined:
    Aug 28, 2020
    Posts:
    7
    Have you solved this problem? I'm having the same problem.
    ----
    Code (CSharp):
    1. var JsLib = {
    2.   postMessage: function (method, info) {
    3.     console.log("post 0000000");
    4.   },
    5.   funLaunchInfo: function () {
    6.     console.log("funLaunchInfo >> start");
    7.     postMessage("launchInfo", null);
    8.     console.log("funLaunchInfo >> end");
    9.   }
    10. };
    11. mergeInto(LibraryManager.library, JsLib);
    ------------------------
    BUT:
    postMessage Never Run
    Got This

    Code (CSharp):
    1. funLaunchInfo >> start
    2. funLaunchInfo >> end
     
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Does anyone have an answer for this?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I mean this is just a basic JavaScript scoping problem. Look you can even do it in your browser console. Here's my exploration, with the solution too:
    Screen Shot 2022-12-05 at 11.34.05 PM.png
     
  5. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    You can even do it in your browser console? Well done.

    Now how about trying it in the actual problem domain of a .jslib file in a Unity WebGL build where the issue occurs...
    funcA executes when called from Unity but doesn't know what MyCollection is...

    Code (JavaScript):
    1. var MyCollection = {
    2.     funcA: function()
    3.     {
    4.         MyCollection.funcB();
    5.     },
    6.     funcB: function()
    7.     {
    8.         console.log("funcB");
    9.     },
    10. };
    11. mergeInto(LibraryManager.library, MyCollection);
       
    exception thrown: ReferenceError: MyCollection is not defined
     
    Last edited: Dec 6, 2022
    GreenTVlad likes this.
  6. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    anyone has updates on this?
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    Well, js libs are always quite fiddly as they restrict their scope quite heavily. However this SO question / answer seems to have a solution. Never tested this myself.
     
    xucian likes this.
  8. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    I actually went the extra thousand miles and learned quite a lot along the way, making an actual library for CS-js interop, where you can even call C# async to JS async, which works really satisfyingly. wondering if I should release it on asset store.
    it helps me pretty well in dragonshift