Lose SFConnection on Scene Load

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

dforma
Posts: 5
Joined: 14 Jul 2019, 17:54

Lose SFConnection on Scene Load

Postby dforma » 21 Jul 2019, 05:50

I've taken a copy of Tris, and substituted loading my own game scene in Unity instead of Game, but when GameController starts which I've added to the topmost object in my scene, 'sfs' is null and IsInstantiated is false.

I'm using the Javascript server code and am using trace messages to help troubleshoot, but can't troubleshoot/undertand why the connection is persisent using the Tris Game scene, but is lost loading my own.

I'm thinking I may need to get a Java IDE and debug Java server code to see why the connection may be disappearing, or becoming inaccessible/releeased.

But hoping you can suggest some things to check before I go that longer route.
Thanks
User avatar
Lapo
Site Admin
Posts: 21013
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Lose SFConnection on Scene Load

Postby Lapo » 22 Jul 2019, 07:51

Hi,
you don't need a Java IDE to dig into the problem. The null object is on the client side, therefore you just need a bit of client debugging.

Before that I would also recommend to review the Tris game tutorial here:
http://docs2x.smartfoxserver.com/ExamplesUnity/tris

In particular the "Connection and Login" section where it is explained how a global connection object is maintained throughout the different scenes.

Hope it helps.
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 21013
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Lose SFConnection on Scene Load

Postby Lapo » 22 Jul 2019, 07:57

In particular I think you need to make sure this code is present in your custom scene:

Code: Select all

if (SmartFoxConnection.IsInitialized) {
        sfs = SmartFoxConnection.Connection;
    } else {
        SceneManager.LoadScene("Login");
        return;
    }


Anyways make sure to keep the tutorial I linked above as a reference to fix your current scene.
Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
dforma
Posts: 5
Joined: 14 Jul 2019, 17:54

Re: Lose SFConnection on Scene Load

Postby dforma » 22 Jul 2019, 18:33

Thanks for your response. And sorry these are beginner-type questions.

I had indeed studied the Tris examples page, and have that code you shared in my code already, is was taken from the original Tris GameController code.

However, when stepping through the code execution, whenever I hit that IF statement, the SmartFoxConnection is NOT initialized (pardon, mistyped 'IsInstantiated' earlier) and falls through to the 'LoadScene(Login)' and sfs remains null.

All I have done so far in the code is to replace (in LoginController) the 'LoadScene("Game")' with 'LoadScene("ChessCam"). When using 'Game,' the connection remain useable, but when using "ChessCam" it is not.

In the ChessCam scene, all I have done so far is to add an object to the top (named 'SuperVisor') and added the existing GameController script component. BUT I also note when debugging that Unity seems to be first executing other code snippets in my game which are lower in the object hierarchy before it hits the GameController Awake() in the topmost object, as if already processing OnClick events from the game - if any of that sheds light - where I would have through the GameController should execute first.

I expect to have to revise GameController, but for now just want to ensure the connection remains active, and then will look at other changes to be made to that file to suit my game.

Are there any factors which are scene-dependent?

Anyway, had enclosed some server log details in my other post, but hope you may have some guess or things to check to aid in my troubleshooting.
Thanks
User avatar
Bax
Site Admin
Posts: 4478
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Lose SFConnection on Scene Load

Postby Bax » 23 Jul 2019, 08:36

From your description it seems you are not following the Tris example entirely.
In fact, after the login is performed, the Lobby scene is loaded. That's where players meet (in a dedicated Room), and create a game Room to play.

Anyway...

As my colleague Lapo pointed out, the Lobby and Game scenes access the SmartFoxConnection class when checking the IsInitialized property. If you check that class, that property is false if the sfs object (instance of SmartFox class) is null. So it seems that in your code you are not setting the SmartFoxConnection.Connection property, which holds a reference to sfs.

In our Tris example, SmartFoxConnection.Connection is set as soon the connection is established, in the OnConnection method of the LoginController class:

Code: Select all

// Save reference to SmartFox instance; it will be used in the other scenes
SmartFoxConnection.Connection = sfs;


Maybe this is missing in your code?
Paolo Bax
The SmartFoxServer Team
dforma
Posts: 5
Joined: 14 Jul 2019, 17:54

Re: Lose SFConnection on Scene Load

Postby dforma » 26 Jul 2019, 02:28

Thank you for your suggestions.

What I currently have is the Tris game environment exactly as from the examples folder, with the correct server extensions and correct Unity build settings.

If I load the "Game" scene in the LobbyController, I can access the connection when GameController Awakes.

But if I merely substitute my custom scene ("ChessCam" - is present in Build Settings) in the existing LoadScene where the room is joined, then 'when GameController starts, sfs' is null, and IsInitialized is false. So 'Game' works fine but loading my scene file affects the connection.

In all other regards, everything is exactly as in the example, including all code fragments you have mentioned earlier.

I am at a loss to troubleshoot what is occurring, but there seems to be something scene-dependent involved.
Thank you
Last edited by dforma on 26 Jul 2019, 02:29, edited 1 time in total.
User avatar
Bax
Site Admin
Posts: 4478
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Lose SFConnection on Scene Load

Postby Bax » 26 Jul 2019, 07:36

Does your scene contain a Controller object to which the GameController script is assigned?
Paolo Bax
The SmartFoxServer Team
User avatar
Bax
Site Admin
Posts: 4478
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Lose SFConnection on Scene Load

Postby Bax » 26 Jul 2019, 08:02

I just did a simple test and everything works as expected:

- added a new "Test" scene to the Tris project
- inside the new scene, created an empty GameObject, called "Controller"
- assigned a new script to the Controller object, called "TestScript"
- added the following code to the script:

Code: Select all

   void Awake()
   {
      Application.runInBackground = true;

      if (SmartFoxConnection.IsInitialized) {
         Debug.Log("-OK- SFS CONNECTION INITIALIZED");
      }
      else {
         Debug.Log("-KO- SFS CONNECTION NOT INITIALIZED");
         SceneManager.LoadScene("Login");
      }
   }

- changed the LobbyController.OnRoomJoin listener to make it load the Test scene

When the modified project is executed (with the Test scene opened in the Editor), the else branch of the code is executed and the Login scene is loaded. After the login and after clicking the "Start new game" button, the -OK- SFS CONNECTION INITIALIZED is printed as expected.

So my guess is that there's something wrong in your scene, or you changed something else. Maybe you can start again from scratch following the steps above.
Paolo Bax
The SmartFoxServer Team
dforma
Posts: 5
Joined: 14 Jul 2019, 17:54

Re: Lose SFConnection on Scene Load

Postby dforma » 26 Jul 2019, 18:34

I'm certain that your test works correctly, and that there is something unusual in my scene which is the cause. I just wish I could better troubleshoot or capture what is occurring. I was just hoping you'd seen something like this before, but I will report if I find anything out.

I will just have to start with a working controller and try to reconstruct my game scene around that. If anything to try or check to further diagnose occurs to you, that would be much appreciated.

Thanks

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 21 guests