Unity: Initialize Warpclient in singleton initializer

0 votes

Hi,

I have a singleton class called GlobalManager and I'd like to init Warpclient singleton in my GlobalManager singleton. Here's the initialization:

public static GlobalManager GetInstance()
    {
        if( !instance )
        {
            instance = (GlobalManager)Instantiate(Resources.Load("GlobalManager"typeof(GlobalManager)));
            instance.listen = new Listener();
            WarpClient.initialize(apiKey,secretKey);
            instance.AddListeners();
            WarpClient.GetInstance().Connect(username);
            DontDestroyOnLoad(instance);
        }
        return instance;
    }

Everything works fine, but when I leave and unsubscribe the room and then reload the scene by LoadScene(), I can't subscribe and join the room anymore, the WarpClient's state is "connected" however. Then, when I'm trying to disconnect, it disconnects successfully, but when I try to connect after that, it is not connecting and the WarpClient's state is always 1 ("connecting"). Can someone help me with this issue?

asked Nov 10, 2014 in AppWarp by sirsinel (11 points)

1 Answer

0 votes
Why you wrote a wrapper around WarpClient, it is already a singleton class?
How are the listeners being defined and used?

If listener's object was active in scene 1 for example then it won't work when you switch to another scene as that scene won't contain those listeners.

It's very important for us to understand in more details how is your GlobalManager works and how it maintains the WarpClient and listeners. So please share some more details. Also can you confirm that if everything works fine in case you are not changing the scenes.
answered Nov 10, 2014 by Suyash Mohan (900 points)
It's not a wrapper specially for WarpClient, it's a singleton for my other purposes where I decided to init WarpClient. As for the listeners, they are defined in a separate function AddListeners() which is called from the code above:

void AddListeners(){
        WarpClient.GetInstance().AddConnectionRequestListener(listen);
        WarpClient.GetInstance().AddChatRequestListener(listen);
        WarpClient.GetInstance().AddUpdateRequestListener(listen);
        WarpClient.GetInstance().AddLobbyRequestListener(listen);
        WarpClient.GetInstance().AddNotificationListener(listen);
        WarpClient.GetInstance().AddRoomRequestListener(listen);
        WarpClient.GetInstance().AddZoneRequestListener(listen);
        WarpClient.GetInstance ().AddTurnBasedRoomRequestListener (listen);
    }

This problem is not related to listeners cause after reloading the scene I do directly WarpClient.GetInstance().Disconnect() (the listener logs "onDisconnectDone: 0" text), then WarpClient.GetInstance().Connect("unique_user_name") (no new logs are shown), then I check the WarpClient's connection state and it is always 1 (Connecting). By the way, there's a mistake in API docs, they say that the state 0 means disconnected and state 2 means connected, but I get 0 when I connected and 2 when I disconnected.
As for the understanding how my GlobalManager code works, I showed everything related to this issue. The code inside my GetInstance() is executed once per game and everything works fine until I reload the same scene with Application.LoadLevel("MyScene"); If I remove  DontDestroyOnLoad(instance) from the code, the code inside my GetInstance() function is executed every time I reload the scene and everything works fine, but I don't want to initialize WarpClient and reconnect every time I refresh the scene. I can provide you with my unity project if you don't mind,
Yes! It will be very helpful in debugging if you can share your project. You can send your project at support@shephertz.com
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...