How to preserve smartfox client when switching scenes?

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

Moderators: Lapo, Bax

fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

How to preserve smartfox client when switching scenes?

Postby fernando24691 » 22 Oct 2010, 18:02

Hi everybody.

I'm making a little application in unity 3 + sfs pro trial, because I think sfs is very powerful and let indie developers make great things in less time.

I have done a loggin scene when the user logs in SFS with an username and password. Once the login has been successfull and the player is in the room that I wanted, then I need to load a new scene (actually, the SFS room where my player is) and continuing scripting and using smartfox from there, without losing the connection with the server.

But I just don't know how to do that. I have tried to simply load a new scene and create a new smartfox client, but nothing happens, and smartfox.IsConnected() is equal to false.

Thank you so much! :D
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 22 Oct 2010, 19:27

I dont know if you've taken a look at the examples that are provided with the API.

They have a static class that is independent of Unity scenes, where you can register - and in the new scene get back - your connection.

Alternatively you can use the dont destroy on load setting on a monobehaviour. Just beware that you will persist the entire gameobject and all its other components too.

Best bet is to do whats done in the examples (in my oppinion)
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 22 Oct 2010, 22:08

Thank you so much. I will try it and post the results! :D
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 23 Oct 2010, 00:03

Thank you so much ThomasLund, I followed your tip and I got a succesfull result.

This is what I did (for people experiencing the same problem):

The only thing I need to keep the connection up with the server is the smartfox client variable. The rest of handlers and functions can be redefined as well as in the C# examples, when they use "RegisterSFSCallbacks" and "UnregisterSFSCallbacks".

So, the only thing we have to keep existing is that variable. And the way to do that is through the "static" keyword, as it makes the variable global and we can access it from anywhere.

If you have a first scene, where you declared your SFSclient variable, and you connnect and handle your first events from there, you only have to declare that SFSclient variable as "public static". When you load a new scene, you should create a new SFSclient and assign to it the previous value of the first SFSclient. Thus, you can work from there with the original SFS variable, keeping the conection up.

For more information take a look at:

http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html

PD: You may think that every script (and that implies every variable) contained into a scene will be destroyed when loading a new scene. There is no problem with that, since "static" parameter keep that variable existing, despite the fact that all the other variables inside the same script were destroyed. "Public" parameter makes it accessible from another scripts.

There would be something like:

In the script attached to the scene that has to be destroyed:

Code: Select all

public static SmartFoxClient smartFox;

//Do connection stuff and close scene;


In the new scene (and other ones that will load after this):

Code: Select all

public SmartFoxClient smartFox;

void Awake() {
      smartFox = OldScript.smartFox;
}

Do something and close scene
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 23 Oct 2010, 01:25

yes thats one way of doing it, tho a static class is a cleaner way...
if you look at the SFSIsland demo you can see how it works.
the class holds the connection info which you can access from anywhere in your game and it won't be destroyed... you don't attach it to a gameobject at all.
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 23 Oct 2010, 11:41

Yes but, I already attached to an empty entity the script which has the "public static smartfox client" declaration, and it stills working despite I detroy that script when loading a new level.

It is important to make it apart from any scene, or static variable will still alive anyway?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 23 Oct 2010, 12:42

i'm not sure for a 100% but i think statics will keep working... tho like i said, the staic class is a cleaner way.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 3 guests