Forum Scripting Callback losing scope?

Post your problems and discussions relating to scripting in Unity here.

Callback losing scope?

Postby quickfingers » February 5th, 2011, 9:16 am

Having a weird issue with the joinroom success callback doesn't appear to be in the same scope as the rest of my class? I have no idea how this might happen, I'm pasting my class here for anyone a bit more knowledgable in C# than me to take a look over:
Code: Select all
using PlayerIOClient;
using UnityEngine;
using System.Collections;

public class MultiplayerManager : MonoBehaviour {

    public MultiplayerGUI gui;
    private Client client;
    private RoomInfo[] rooms;
    private Connection connection;
    public bool joinedRoom;


    // Use this for initialization
    void Start() {
        PlayerIO.UnityInit(this);

        // create a random userid
        // TODO enable quickconnect kong / facebook accounts
        string userid = "Guest" + Random.Range(0, 10000);

        PlayerIO.Connect(
            "surrender-xbaoyl1lkqvzfwma7rzdq",   // Game id (Get your own at playerio.com. 1: Create user, 2:Goto admin pannel, 3:Create game, 4: Copy game id inside the "")
            "public",                  // The id of the connection, as given in the settings section of the admin panel. By default, a connection with id='public' is created on all games.
            userid,                     // The id of the user connecting. This can be any string you like. For instance, it might be "fb10239" if you´re building a Facebook app and the user connecting has id 10239
            null,                     // If the connection identified by the connection id only accepts authenticated requests, the auth value generated based on UserId is added here
            OnConnectSuccess,
            OnError
        );
    }

    private void OnError(PlayerIOError error) {
        gui.ShowError(error.ToString());
    }

    private void OnConnectSuccess(Client client) {
        this.client = client;
        client.Multiplayer.DevelopmentServer = new ServerEndpoint("192.168.0.9", 8184);
        StartCoroutine(PollRoomList());
        gui.Init(MultiplayerGUI.Mode.InLobby);
    }

    IEnumerator PollRoomList() {
        while (!joinedRoom) {
            client.Multiplayer.ListRooms("GameRoom", null, 0, 0, OnRoomList);
            yield return new WaitForSeconds(3f);
        }
    }
    void OnRoomList(RoomInfo[] rooms) {
        this.rooms = rooms;
        gui.UpdateRoomList(rooms);
    }

    public void JoinRoom(string roomId) {
        client.Multiplayer.JoinRoom(roomId, null, OnJoinedRoom, OnError);
    }
   
    public void CreateNewRoom(string roomName, string roomPassword) {
        //Create or join the room
        client.Multiplayer.CreateJoinRoom(
            roomName, //Room id. If set to null a random roomid is used
            "GameRoom", //The room type started on the server
            true, //Should the room be visible in the lobby?
            null,
            null,
            OnJoinedRoom,
            OnError);
    }

    private void OnJoinedRoom(Connection newConnection) {
       
        connection = newConnection;
        Debug.Log("Joined room!");
        //connection.OnMessage += handlemessage;
        gui.Init(MultiplayerGUI.Mode.Joined);
        connection.OnMessage += HandleMessage;
        joinedRoom = true;

    }

    void HandleMessage(object sender, PlayerIOClient.Message m) {
        Debug.Log(m);
    }
}


Now the problem is in the OnJoinedRoom callback. None of those lines actually do anything they are supposed to. The debug.log does print the message Joined room to the console but it has no stack trace whatsoever which is at the very least bizarre... it doesn't set joinedRoom to true and my gui.init call doesn't get called. Strange. The OnConnectSuccess callback works fine and is formatted the same way. Is there something going on behind the scenes I'm not aware of?

Many thanks
User avatar
quickfingers
 
Posts: 7
Joined: January 28th, 2011, 6:14 am
Location: London, UK

Re: Callback losing scope?

Postby Izzimach » February 6th, 2011, 12:50 am

The only thing that comes to mind is:

1. Player.IO may be calling your callbacks from another thread, and
2. Calling Unity functoins from other threads is not guaranteed to work

So if you call gui.init(...) and your MultiplayerGUI script calls some unity functions, that may cause the problems you are seeing. Dunno why it work for OnConnectSuccess and not for OnJoinedRoom, however. And in any case, Unity 3.0 was supposed to pop up errors when you tried calling unity functions from other threads. Did you check if it works without the call to gui.Init?
User avatar
Izzimach
 
Posts: 4
Joined: January 17th, 2011, 7:17 pm
Location: Rocky Mountain High

Re: Callback losing scope?

Postby RocketBunny » February 6th, 2011, 2:15 am

I basically recreated what you have in your code and my code worked fine. Try my code and see if it works. You will need to make sure your server code sends back the Welcome and UserJoined messages to see that output. Also make sure you change my server name "PlayerIOTest" to whatever your server code dll is called.

Code: Select all
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using PlayerIOClient;

public class Main : MonoBehaviour
{
   bool joinedRoom = false;
   Client mClient = null;
   
   private string mStatusMsg = "Waiting...";

   IEnumerator Start()
   {
      PlayerIOClient.PlayerIO.UnityInit(this);
      try
      {
         PlayerIOClient.PlayerIO.Connect("<insert your game ID here>", "Public", "GuestUser", null,
            delegate(Client client)
            {
               Debug.Log("SUCCESS!");
               mStatusMsg += "Connected to PlayerIO\n";

               mClient = client;

               StartCoroutine(PollRoomList());
            },
            delegate(PlayerIOError error)
            {
            }
         );
      }
      catch (Exception e)
      {
         Debug.Log("ERROR: " + e.ToString());
      }
      yield return new WaitForSeconds(0);
   }

   IEnumerator PollRoomList()
   {
      mStatusMsg += "Polling room list...\n";
      while (!joinedRoom)
      {
         yield return new WaitForSeconds(3);
         if (!joinedRoom)
         {
            mClient.Multiplayer.ListRooms("PlayerIOTest", null, 0, 0, OnRoomList);
         }
      }
   }
   void OnRoomList(RoomInfo[] rooms)
   {
      mStatusMsg += "Room count=" + rooms.Length.ToString()+"\n";
      Debug.Log("Room count=" + rooms.Length.ToString());
      for (int a = 0; a < rooms.Length; a++)
      {
         Debug.Log(rooms[a].Id);
         mStatusMsg += "Found Room " + rooms[a].Id + "\n";
      }
      if (rooms.Length == 0)
      {
         mStatusMsg += "Creating room...\n";
         CreateNewRoom("RobsRoom", "");
      }
      else
      {
         mStatusMsg += "Joining room...\n";
         JoinRoom(rooms[0].Id);
      }
      //gui.UpdateRoomList(rooms);
   }

   public void JoinRoom(string roomId)
   {
      mClient.Multiplayer.JoinRoom(roomId, null, OnJoinedRoom, OnError);
   }

   public void CreateNewRoom(string roomName, string roomPassword)
   {
      //Create or join the room
      mClient.Multiplayer.CreateJoinRoom(
         roomName, //Room id. If set to null a random roomid is used
         "PlayerIOTest", //The room type started on the server
         true, //Should the room be visible in the lobby?
         null,
         null,
         OnJoinedRoom,
         OnError);
   }

   private void OnError(PlayerIOError error)
   {
      Debug.Log("ERROR: " + error.ToString());
   }

   private void OnJoinedRoom(Connection newConnection)
   {
      mStatusMsg += "Joined room \n";
      Debug.Log("Joined room!");
      newConnection.OnMessage += delegate(object sender, PlayerIOClient.Message m)
      {
         Debug.Log("Message:"+m.Type);
         switch( m.Type )
         {
            case "Welcome":
            {
               Debug.Log("Welcome Message");
               mStatusMsg += "You joined with ID " + m.GetInt(0).ToString()+".\n";
               break;   
            }
            case "UserJoined":
            {
               mStatusMsg += "User with ID " + m.GetInt(0).ToString() + " is in the room.\n";
               break;
            }
         }
      };
      joinedRoom = true;
   }

   void OnGUI()
   {
      GUI.Label(new Rect(10, 10, 600, 600), mStatusMsg);
   }
   
}


Cheers,

-Rob.
RocketBunny
Paid Member
 
Posts: 23
Joined: June 16th, 2010, 9:49 pm

Re: Callback losing scope?

Postby quickfingers » February 7th, 2011, 12:42 am

Thanks for the replies guys, Izzimach it seems you're right that the callback is in another thread. Removing the unity call (gui.Init) fixed the problem but Unity wasn't showing any errors before. Now I'm trying to fix it without some ugly hack but it might end up being that way. Anyway thanks to both of you for helping with this
User avatar
quickfingers
 
Posts: 7
Joined: January 28th, 2011, 6:14 am
Location: London, UK

Re: Callback losing scope?

Postby Oliver » February 7th, 2011, 4:41 pm

It seems like a RocketBunny rushed to the rescue on this one, so i don't have to address the actual issue.

On the subject of execution context: Yes, callbacks are not executed by a unity thread, and this probably causes issues (like this one.

We have a few ideas we want to try out, to see if we can somehow get unity to execute the callbacks for us, so we won't have these issues. Time will tell if we're successful :)

Best,
Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Callback losing scope?

Postby quickfingers » February 7th, 2011, 9:16 pm

thanks Oliver, good luck I hope you can implement that :)
User avatar
quickfingers
 
Posts: 7
Joined: January 28th, 2011, 6:14 am
Location: London, UK

Re: Callback losing scope?

Postby omkarpatil » May 9th, 2011, 1:39 pm

Oliver wrote:It seems like a RocketBunny rushed to the rescue on this one, so i don't have to address the actual issue.

On the subject of execution context: Yes, callbacks are not executed by a unity thread, and this probably causes issues (like this one.

We have a few ideas we want to try out, to see if we can somehow get unity to execute the callbacks for us, so we won't have these issues. Time will tell if we're successful :)

Best,
Oliver


Hi guys have newly joined Player.IO and i like it but for one thing, the errors it gives while accessing Unity objects.
In the GotMessage Callback i am trying to Instatiate() an object and it is throwing error at runtime. Whereas the same works perfect in a non Player.IO callback (Awake() function) .Hope there has been some progress on what Oliver mentioned.
Any help would be much appreciated.
Thanks.
omkarpatil
 
Posts: 3
Joined: May 8th, 2011, 1:38 pm

Re: Callback losing scope?

Postby RocketBunny » May 9th, 2011, 2:21 pm

Hey there,

I don't think Oliver has had a chance to trial the idea that he mentioned.

The way I got around it was to add objects I want to instantiate to a queue (when inside a PlayerIO delegate) and then on Update(), I run through the queue and look for any objects that haven't been initialized properly and instantiate at that point.

Hope this helps.

-Rob.
RocketBunny
Paid Member
 
Posts: 23
Joined: June 16th, 2010, 9:49 pm

Re: Callback losing scope?

Postby omkarpatil » May 9th, 2011, 2:27 pm

RocketBunny wrote:Hey there,

I don't think Oliver has had a chance to trial the idea that he mentioned.

The way I got around it was to add objects I want to instantiate to a queue (when inside a PlayerIO delegate) and then on Update(), I run through the queue and look for any objects that haven't been initialized properly and instantiate at that point.

Hope this helps.

-Rob.


Thanks RocketBunny, would try it now.
omkarpatil
 
Posts: 3
Joined: May 8th, 2011, 1:38 pm

Re: Callback losing scope?

Postby Oliver » May 10th, 2011, 2:00 pm

I actually did go in to look at it, and the callbacks are actually already being called by Unity. We're using Unity's WWW class to make the webrequests...

For some reason however, those callbacks are different. I haven't found a fix.

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am


Return to Scripting



cron