Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Am i using the ping class right?

Discussion in 'Scripting' started by mokoton, May 23, 2015.

  1. mokoton

    mokoton

    Joined:
    Aug 29, 2014
    Posts:
    5
    Code (CSharp):
    1.     Ping ping;
    2.     List<int> pingList;
    3.  
    4.     void Start ()
    5.     {
    6.         pingList = new List<int> ();
    7.         ping = new Ping ("216.52.241.254");
    8.  
    9.         StartCoroutine (PingUpdate());
    10.     }
    11.  
    12.     IEnumerator PingUpdate()
    13.     {
    14.         yield return new WaitForSeconds (1f);
    15.         if (ping.isDone)
    16.         {
    17.             pingList.Add(ping.time);
    18.             Debug.Log (ping.time);
    19.             ping = new Ping ("216.52.241.254");
    20.         }
    21.         StartCoroutine (PingUpdate ());
    22.     }
    I am still new to unity, corountine and the ping class so i am not sure if i am doing it right.
    The code is supposed to collect pings once every sec(or more) and add it to a list.
    is there any hidden bugs in my code or like is there a better way of doing what i am trying to do.
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356

    From what i just read, you should use Ping.Ping(ip) instead of new Ping(ip), so


    Code (CSharp):
    1.     Ping ping;
    2.     List<int> pingList;
    3.  
    4.     void Start ()
    5.     {
    6.         pingList = new List<int> ();
    7.         ping = Ping.Ping("216.52.241.254");
    8.  
    9.         StartCoroutine (PingUpdate());
    10.     }
    11.  
    12.     IEnumerator PingUpdate()
    13.     {
    14.         yield return new WaitForSeconds (1f);
    15.         if (ping.isDone)
    16.         {
    17.             pingList.Add(ping.time);
    18.             Debug.Log (ping.time);
    19.             ping = Ping.Ping("216.52.241.254");
    20.         }
    21.         StartCoroutine (PingUpdate ());
    22.     }
     
  3. mokoton

    mokoton

    Joined:
    Aug 29, 2014
    Posts:
    5
    but isnt ping.ping a constructor?
    and so when do you use
    new ping and when do you use ping.ping?
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356

    Ping.Ping() is a seperate method, look in the documentation.
     
  5. mokoton

    mokoton

    Joined:
    Aug 29, 2014
    Posts:
    5
    but
     

    Attached Files:

  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,353
    That is the constructor, you'd say '... new Ping(...'.

    The only thing I would say is wrong with your code is that you only wait a second for your ping to finish. There's no guarantee that ping is done in that time. You should yield null until the ping is complete... but if you ALSO want a 1 second delay between pings... do both.

    Furthermore, you can just loop in PingUpdate instead of starting a new coroutine.

    Something like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class PingExample : MonoBehaviour
    6. {
    7.  
    8.     private List<int> _pingTime = new List<int>();
    9.  
    10.     void Start()
    11.     {
    12.         this.StartCoroutine(PingUpdate());
    13.     }
    14.  
    15.     System.Collections.IEnumerator PingUpdate()
    16.     {
    17.         RestartLoop:
    18.         var ping = new Ping("216.52.241.254");
    19.  
    20.         yield return new WaitForSeconds(1f);
    21.         while (!ping.isDone) yield return null;
    22.  
    23.         Debug.Log(ping.time);
    24.         _pingTime.Add(ping.time);
    25.  
    26.         goto RestartLoop;
    27.     }
    28. }
    29.  
    Personally I like using goto for my loop, you could also just say 'while(true)'.
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,411
    Wow! goto! did I just fall through a time wormhole and travel 30 years back in time?

    Only kidding, it's just been a long time since I've seen it used.:D

    Just like to add that there's no reason not to here btw.
     
    MihkelT and Elegare2 like this.
unityunity