Search Unity

Can C# register a Javascript callback?

Discussion in 'Scripting' started by Texaggie96, Feb 3, 2015.

  1. Texaggie96

    Texaggie96

    Joined:
    Jan 23, 2009
    Posts:
    81
    I have a Javascript component (JavascriptComponent.js for demonstrative sake) file (which I have to use) in my /Plugins folder so my C# files can recognize it. My C# script (in regular /Assets folder) can get the javascript component with GetComponent<JavascriptComponent>() and access all the variables in the JavascriptComponent just fine. What i want to do in create a callback in the Javascript to another c# script. http://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates shows how to do create a callback in Javascript. But this doesn't appear to work when I set the function as a C# method. This is how I am doing it.
    JavascriptComponent.js:
    Code (csharp):
    1.  
    2. ...
    3. var coolIntBro = 1;
    4. var MyCoolCallback : Function;
    5. ...
    6. // later some event will trigger this
    7. MyCoolCallback(1234);
    8.  
    ACScomponent.cs:
    Code (csharp):
    1.  
    2. ...
    3. // add C# component
    4. OtherCSComponent myOtherCSComponent = randomGameObject.AddComponent<OtherCSComponent>();
    5.  
    6. // get the Javascript component we are interested in
    7. JavascriptComponent javascriptComponent = someGameObject.GetComponent<JavascriptComponent>() as JavascriptComponent;
    8.  
    9. // set a var in the javascript component - this works fine
    10. javascriptComponent.coolIntBro = 2;
    11.  
    12. // try to set callback - does not work
    13. javascriptComponent.MyCoolCallback = myOtherCSComponent .CSCallback;
    14. ...
    15.  
    OtherCSComponent.cs
    Code (csharp):
    1.  
    2. public void CSCallback( int someInt)
    3. {...}
    4.  
    The error i get is this: error CS0428: Cannot convert method group `CSCallback' to non-delegate type `Boo.Lang.ICallable'. Consider using parentheses to invoke the method.

    Is there someway to make this work? (I really really really do not want to use SendMessage for JS firing events back into C#).
     
  2. Texaggie96

    Texaggie96

    Joined:
    Jan 23, 2009
    Posts:
    81
    Well, I finally figured it out and wanted to would post this in case someone comes across the problem of getting Javascript to talk to C#. You have to change how you define the callback in Javascript to force it treat it not like an "object" and adding regular ": Function" is not good enough (as demonstrated above).
    JavascriptComponent.js
    Code (csharp):
    1.  
    2. var coolIntBro = 1;
    3. var MyCoolCallback : function(int); // specify the parameters you are going need, notice lower case "f"
    4. ...
    5.  
    With this you should be able to avoid SendMessage.
     
    Last edited: Feb 6, 2015
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The correct type for delegates in Unityscript is "function(type params) : returnType". e.g.:

    Code (csharp):
    1. private var foo : function(int) : Vector3;
    2.  
    3. function Start () {
    4.     foo = SomeFunction;
    5.     transform.position = foo(5);
    6.     foo = AnotherFunction;
    7.     transform.eulerAngles = foo(200);
    8. }
    9.  
    10. function SomeFunction (x : int) : Vector3 {
    11.     return Vector3(x, x, x);
    12. }
    13.  
    14. function AnotherFunction (x : int) : Vector3 {
    15.     return Vector3(x*2, x/2, x+2);
    16. }
    That's compatible with C# delegates. In this example, foo can only be assigned functions that take an int and return a Vector3.

    --Eric