Sign In Register

How can we help you today?

Start a new topic

Make An Error Message when Player Disconnects

Hello.


I have been trying to add an error message when my player goes offline. The best thing I have found is the following


void Awake () {

    GS.GameSparksAvailable += HandleGameSparksAvailable;

 }


  private void HandleGameSparksAvailable (bool connection) {

  if (!connection) {

   Debug.Log(connection);//Replace This With Error

  }

  }


This doesn't work, as the connection boolean is never returned true, as GameSparks keeps trying to reconnect instead of reporting there isn't a connection. Additionally, setting the initial void to Update instead of Awake causes weird results. I don't want to just wait for the user to come online, as I need the results of their actions on the screen immediately. All I have are old threads which don't give an answer. 


All I need is an error message by GameSparks when it goes offline, or at least a predictable behaviour I can check. Is this implemented in GameSparks?


Any help would be greatly appreciated.

Thanks


GameSparksAvailable is never triggered until a connection is made. When there is no connection, GS is not available, and therefore the handler is not called. I think there's a "GS.Available" you can use that can check the current connection outside of event handler

The below code checks starts with connection false. If there is a connection to the server within 1.5 seconds then connectionDebug = true. After 1.5 seconds, if there is no connection connectionDebug = false. This is run every 1.5 seconds to avoid lag/console spam. This is a generic unity/c# script and is not exclusive to gamesparks.


SOLUTION


private float maxTime, timeTaken;

  private bool connectionDebug;


  void Awake () {

    StartCoroutine("connectionTest");

  }


  IEnumerator connectionTest () {


    maxTime = 1.5f;


    while(true) {


      Debug.Log("Connected Status: " + connectionDebug);


      Ping testPing = new Ping("8.8.8.8");

      timeTaken = 0.0f;


      while (!testPing.isDone) {


        timeTaken += Time.deltaTime;


        if (timeTaken > maxTime) {

          connectionDebug = false;

          break;

        }


        yield return null;


      }


      if (timeTaken <= maxTime) {

        connectionDebug = true;

      }


      yield return new WaitForSeconds(1.5f);


      yield return null;


    }

  }

Login to post a comment