SFS connexion between scenes

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Post Reply
Tatanan
Posts: 112
Joined: 07 Jan 2014, 12:12
Contact:

SFS connexion between scenes

Post by Tatanan »

Hello.
I have been analysing your code for Unity and have some doubts.
The code for SmartFoxConnection.cs is this one:

Code: Select all

using UnityEngine;
using Sfs2X;

// Statics for holding the connection to the SFS server end
// Can then be queried from the entire game to get the connection

public class SmartFoxConnection : MonoBehaviour
{
	private static SmartFoxConnection mInstance; 
	private static SmartFox smartFox;
	public static SmartFox Connection {
		get {
            if (mInstance == null) {
                mInstance = new GameObject("SmartFoxConnection").AddComponent(typeof(SmartFoxConnection)) as SmartFoxConnection;
            }
            return smartFox;
        }
      set {
            if (mInstance == null) {
                mInstance = new GameObject("SmartFoxConnection").AddComponent(typeof(SmartFoxConnection)) as SmartFoxConnection;
            }
            smartFox = value;
        } 
	}

	public static bool IsInitialized {
		get { 
			return (smartFox != null); 
		}
	}
	
	// Handle disconnection automagically
	// ** Important for Windows users - can cause crashes otherwise
    void OnApplicationQuit() { 
        if (smartFox.IsConnected) {
            smartFox.Disconnect();
        }
    } 
}
First of all, the reason to create mInstance is just to let Unity call OnApplicationQuit()? I don't see any other use at all.

Second: LoginGUI script is attached to the scene and supposed to be destroyed when switching the scene. Could or should we use DontDestroyOnLoad for keeping this class working in the new scene? I don't know if I'm wrong but, would it be easier than destroyed and creating the class and the listeners every time? I mean, you do keep the connexion trought the static var SmartFoxConnection.Connection, but not the listeners not the GameObject, why don't you keep all with DontDestroyOnLoad?

What I od is

Code: Select all

GameObject connectorGameObject = new GameObject ("Connector Object");
Object.DontDestroyOnLoad (connectorGameObject);
_connector = connectorGameObject.AddComponent<Connector> ();
where Connector is the class which contains my SmartFox var and extends MonoBehaviour.

Regards.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS connexion between scenes

Post by Lapo »

Hi,
the developer who worked on those examples no longer works with us, so we're not able to provide specific details as to why certain objects were created and why.
If you have a better strategy that suits your use case, I'd suggest to stick with your solution. Our examples are not to be taken literally, besides the use of the API itself.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply