Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pointer_stringify is returning garbled text

Discussion in 'WebGL' started by The-Little-Guy, Jul 3, 2017.

  1. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I have have the following `lib.jslib` file

    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2.   IsGuestUser: function (objectName, objectMethodName) {
    3.     gamesmart.user.isGuest(function (result) {
    4.       console.log(Pointer_stringify(objectName), Pointer_stringify(objectMethodName), result);
    5.       gameSmartGameInstance.SendMessage(Pointer_stringify(objectName), Pointer_stringify(objectMethodName), result);
    6.     });
    7.   }
    8. });

    Which gets called from here:

    Code (CSharp):
    1. namespace GameSmart {
    2.   public class User : API {
    3.     [DllImport("__Internal")]
    4.     public static extern void IsGuestUser(string objectName, string objectMethodName);
    5.  
    6.     public static void IsGuest(string objectName, string objectMethodName) {
    7.       IsGuestUser(objectName, objectMethodName);
    8.     }
    9.   }
    10. }
    And is initiated like so:

    Code (CSharp):
    1. public class Test : MonoBehaviour {
    2.   void Start() {
    3.     GameSmart.User.IsGuest("GameSmart", "OnIsGuest");
    4.   }
    5. }
    As seen above I pass GameSmart and OnIsGuest to the JavaScript, and when it gets to the JavaScript I call Pointer_stringify() on both of the values.

    When converted and logged, I get the following output: 0Zހ and ﳀ� I should have gotten GameSmart and OnIsGuest back but I didn't what is causing this to happen?
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Hare you tried to stringify before gamesmart.user.isGuest?

    Code (csharp):
    1.     mergeInto(LibraryManager.library, {
    2.       IsGuestUser: function (objectName, objectMethodName) {
    3.         var jsObjectName = Pointer_stringify(objectName);
    4.         var jsObjectMethodName = Pointer_stringify(objectMethodName);
    5.         gamesmart.user.isGuest(function (result) {
    6.           console.log(jsObjectName, jsObjectMethodName, result);
    7.           gameSmartGameInstance.SendMessage(jsObjectName, jsObjectMethodName, result);
    8.         });
    9.       }
    10.     });
     
  3. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    That fixes it! Why do I have to call Pointer_stringify outside of the function?
     
    Last edited: Jul 5, 2017
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    So, I have started optimizing my function, and added a params string[] args property to the end of the value like this:

    Code (CSharp):
    1. [DllImport("__Internal")]
    2. public static extern void Execute(string action, string objectName, string objectMethodName, params string[] args);
    Code (JavaScript):
    1. Execute: function (action, objectName, objectMethodName) {
    2.     var args = [];
    3.     // Convert "arguments" to an array
    4.     for (var i = 0; i < arguments.length; i++) { args.push(arguments[i]) }
    5.     // Remove the first 3 items: "action", "objectName", "objectMethodName"
    6.     // This will leave us with the methods arguments from params string[] args
    7.     args.splice(0, 3);
    8.     for (var i = 0; i < args.length; i++) {
    9.       args[i] = Pointer_stringify(args[i]);
    10.     }
    11.     switch (jsAction) {
    12.         case 'StoreCategoryItems':
    13.             gamesmart.store.categoryItems({ category: args[0] }, function (result) {
    14.                 gameSmartGameInstance.SendMessage(jsObjectName, jsObjectMethodName, JSON.stringify(result));
    15.             })
    16.             break;
    17.     }
    18. }
    Is there a way for me to get those arguments passed in? because once converted, they also are garbled the same way as in the original post.