title: Unity: Javascript callbacks, delegates
body: <p>I would like to use Player.IO from Yahoo Games Network in my Unity javascript code. PlayerIO provides example project for unity written only in C# so I am having problems with getting the callbacks work.</p>
  
  <p><code>PlayerIO.Connect</code> description from Player.IO documentation for Unity (<a href="https://gamesnet.yahoo.com/documentation/reference/unity3d/playerioclient.playerio#Connect" rel="nofollow">available here</a>):</p>
  
  <pre><code>public void Connect (string gameId, string connectionId, string userId, string auth,
  string partnerId, String[] playerInsightSegments,
  Callback&lt;Client&gt; successCallback, Callback&lt;PlayerIOError&gt; errorCallback)
  </code></pre>
  
  <h2><strong>So how to make these callbacks work?</strong></h2>
  
  <p><em>The following part of the question shows the ways I have already tried, but didn't work.</em></p>
  
  <hr>
  
  <p>Firstly I've tried this way (as I know from javascript):</p>
  
  <pre><code>function success(client : Client) {
    //function content
  }
  PlayerIOClient.PlayerIO.Connect(pioGameId, pioConnectionId, pioUserId, pioAuth, pioPartnerId, success, failed);
  </code></pre>
  
  <p>.</p>
  
  <hr>
  
  <p>That was not right and I've learned that I should use delegates.
  From Unity3D official tutorials (<a href="http://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates" rel="nofollow">available here</a>), the right use of delegates in JS would be like this (using the <code>Function</code> type):</p>
  
  <pre><code>var successDelegate : Function;
  
  function success(client : Client) {
    //function content
  }
  
  function Start () {
      successDelegate = success;
      PlayerIOClient.PlayerIO.Connect(pioGameId, pioConnectionId, pioUserId, pioAuth, pioPartnerId, success, failed);
  }
  </code></pre>
  
  <p>This is causing the following error:</p>
  
  <blockquote>
    <p>InvalidCastException: Cannot cast from source type to destination type.
    Main+$Start$2+$.MoveNext ()</p>
  </blockquote>
  
  <p>.</p>
  
  <hr>
  
  <p>After that I've found a topic on Player.IO forum (<a href="https://gamesnet.yahoo.com/forum/viewtopic.php?f=40&amp;t=34388" rel="nofollow">available here</a>) called <em>Utilizing Player.IO in Unity Javascript</em>. Suggested approach of creating delegates in JS by the author of that topic: go straight to the .Net type and declare them as <code>System.Delegate</code>.</p>
  
  <pre><code>var pioConnectSuccessCallback : System.Delegate;
  
  function success(client : Client) {
    //function content
  }
  
  function Start () {
    pioConnectSuccessCallback = System.Delegate.CreateDelegate(typeof(PlayerIOClient.Callback.&lt;PlayerIOClient.Client&gt;), this, "success");
    PlayerIOClient.PlayerIO.Connect(pioGameId, pioConnectionId, pioUserId, pioAuth, pioPartnerId, pioConnectSuccessCallback, pioConnectErrorCallback);
  }
  </code></pre>
  
  <p>This is unfortunately also not working for me, although the guys on the forum present it as a working solution (but it may be outdated).</p>
  
  <p>The error I'm getting:</p>
  
  <blockquote>
    <p>Assets/Main.js(51,35): BCE0023: No appropriate version of
    'PlayerIOClient.PlayerIO.Connect' for the argument list '(String,
    String, String, String, String, System.Delegate, System.Delegate)' was
    found.</p>
  </blockquote>
  
  <p>So it seems like Connect method doesn't like <code>System.Delegate</code> as a parameter or those were not initialized well.</p>