help with unity and unregister callback delegate

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

digiplayer
Posts: 7
Joined: 10 Apr 2009, 16:48

help with unity and unregister callback delegate

Postby digiplayer » 11 Apr 2009, 20:12

Hello everyone,

I am having an issue with unity and unregistering the callback delegates in my custom login script. I have created a script called LoginGui.js which contains the following:

Code: Select all

import System;
import SmartFoxClientAPI;
import SmartFoxClientAPI.Data;
import SmartFoxClientAPI.Util;
import SmartFoxClientAPI.Http;

var smartFox : SmartFoxClient = new SmartFoxClient();
var SmartFox : HttpConnection;
var shuttingDown = false;
var serverIP : String = "myip";
var serverPort : String = "myport";

public var zone : String = "simpleChat";
public var debug : boolean = true;
public var gskin : GUISkin;

var username : String = "";
var pass : String = "";
var loginErrorMessage : String = "";
var connectionAttempt : boolean = false;
   /************
     * Unity callback methods
     ************/

   function OnApplicationQuit() {
      shuttingDown = true;
   }


   function Awake()
   {
      Application.runInBackground = true;
      smartFox.Connect(serverIP, Convert.ToInt32(serverPort));

      // Register callback delegate
      SFSEvent.onConnection = OnConnection;
      SFSEvent.onConnectionLost = OnConnectionLost;
      SFSEvent.onLogin = OnLogin;
      SFSEvent.onRoomListUpdate = OnRoomList;
      SFSEvent.onDebugMessage = OnDebugMessage;
      SFSEvent.onExtensionResponse = onExtensionResponse;
            
   }
   
   function FixedUpdate()
   {
      smartFox.ProcessEventQueue();
   }

   function OnGUI()
   {

      GUI.skin = gskin;
      GUI.Label(new Rect(2, -2, 680, 70), "");   
      
      if (smartFox.IsConnected())
      {
         // Login
         GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
         username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);

         GUI.Label(new Rect(10, 136, 100, 100), "Password: ");
         pass = GUI.TextField(new Rect(100, 136, 200, 20), pass, 25);

         GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);

         if (GUI.Button(new Rect(100, 166, 100, 24), "Login")  || (Event.current.type == EventType.keyDown && Event.current.character == "\n"))
         {
            smartFox.Login(zone, username, pass);
         }

      } else {
         GUI.Label(new Rect(10, 150, 100, 100), "Waiting for connection");
         GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
      }
   }

   function UnregisterSFSSceneCallbacks() {
      // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
      
      //SFSEvent.onConnection = null;
      //SFSEvent.onConnectionLost = null;
      //SFSEvent.onLogin = null;
      //SFSEvent.onRoomListUpdate = null;
      //SFSEvent.onDebugMessage = null;
      //SFSEvent.onExtensionResponse = null;
      
      //SFSEvent.onConnection = onConnection;
      //SFSEvent.onConnectionLost -= OnConnectionLost;
      //SFSEvent.onLogin -= OnLogin;
      //SFSEvent.onRoomListUpdate -= OnRoomList;
      //SFSEvent.onDebugMessage -= OnDebugMessage;
      //SFSEvent.onExtensionResponse -= onExtensionResponse;
      
   }

   /************
    * Callbacks from the SFS API
    ************/

   function OnConnection(success : boolean,  error : String)
   {
      if ( success ) {
         Debug.Log("Connected to server...");
      } else {
         loginErrorMessage = error;
      }
   }

   function OnConnectionLost() {
      loginErrorMessage = "Connection lost / no connection to server";
   }

   function OnDebugMessage(message : String)
   {
      Debug.Log("[SFS DEBUG] " + message);
   }

   function OnLogin(success : boolean, name : String , error : String)
   {
      if ( success )
      {
         Debug.Log("User: " + name + " has logged in");
      } else
      {
         loginErrorMessage = error;
      }
   }
   
   
   function OnRoomList(roomList : Hashtable)
   {
      Debug.Log("Onroomlist Event called");
      UnregisterSFSSceneCallbacks();
      Application.LoadLevel("Port Alexandria");
   }
   
   function onExtensionResponse(evt : SFSObject)
   {
      Debug.Log("RECIEVED RESPONCE");
      var dataObject : SFSObject = evt;
      
      var cmd : String = dataObject.GetString("_cmd") ;
      var id : String =  dataObject.GetString("id") ;
      var username : String =  dataObject.GetString("name") ;
      
      Debug.Log("ID: " + id);
      Debug.Log("username: " + username);
      Debug.Log("CMD: " + cmd);
      
      if (cmd == "logOK")
      {
         myUserId = Convert.ToInt32(id);
         myUserName = username;
         Debug.Log("Login successfull");
      }
      else if (cmd == "logKO")
      {
         Debug.Log("Login unsuccessfull");
      }
   }

function Update ()
{

}


I have also created a java extension that is used to validate the credentials im the mysqldatabase. Everything is fine except that idoes not load the level because i can not figured out how to unregister the callback delegates in a js script. I have looked through the forums and have had no luck finding the answer. Can anyone help? Any suggestions would be appreciated.

Thanks
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Apr 2009, 08:03

I cannot help on that one (personally). I've never tested (or for that matter used) the API with javascript in Unity. (Just wanted to give you fast reply from me)

But maybe someone else has tried and/or knows how to do it. I guess you have tried the += and -= that I used in the tutorials and found them not working. Right?

/Thomas
digiplayer
Posts: 7
Joined: 10 Apr 2009, 16:48

Postby digiplayer » 12 Apr 2009, 11:29

Thanks for the reply Thomas. Yes I have tried the -= and it is not allowed for those types.

I was really hoping to use javascript on the client and java on the server side because it would really speed my development. I have since changed the demo cs script to do what i need. It looks like I will be learning C# scripting.

I have kept the js version and if anyone has a suggestion I will give it a try.

Thanks again.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Apr 2009, 13:48

Good luck!

And you will see that C# is much closer to Java than JavaScript :-)

(I'm a java man myself, but would rather script in C# than js - especially when you can use Visual Studio for editing scripts)

/Thomas

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 1 guest