Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What does this line of code do?

Discussion in 'Scripting' started by surajsirohi1008, Dec 30, 2017.

  1. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    Code (CSharp):
    1.  public override void OnReceivedBroadcast(string fromAddress, string data)
    2.         {
    3.             OnServerDetected(fromAddress.Split(':')[3], data);
    4.  
    5.         }
    I didn't find any documentation on String.split .
     
  2. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
  3. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
  4. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    You mean specifically regarding that line of code? If so, as per previous post, the value the element at index 3 from a string that is split using ':' as the delimiter. (Note: this is the value that the expression will contain, this has nothing to do with printing anything out - this is just the value that is passed as the first argument to OnServerDetected)

    What it exactly contains depends on the string sent.
    As a simple example:

    Code (CSharp):
    1.  
    2. string example = "This:is:a:simple:example";
    3. string demo = example.Split(':')[3];
    4. //demo now contains "simple"
    5.  
    If you dont understand splitting strings, here is a brief tutorial
    https://www.dotnetperls.com/split


    What exactly are you trying to understand? The entire code block or just the splitting of the string?
    From context, the code block is passing a portion of the server address and the full message data to an event.
     
    surajsirohi1008 likes this.
  5. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    Thanks a lot, the fromAdress string stored an IP address, so using split returned the last digit of the IP address.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    As was sort of implied in a previous post by @MickM , whenever you have (or want) code that is not part of Unity's API, you can find it in Microsoft's docs. For other examples, you can often find those in searches, too. :)
     
  7. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    266
    Unity documentation is way better than Microsoft, for beginners. I tried to find it on Microsoft website but I didn't find the actual usage as in an example.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, that can often be true. I'm talking about things that aren't covered here. Perhaps the microsoft docs didn't quite make sense when you looked at them (that can happen ) :)

    Anyways, it's just something to keep in mind, but you said you did try there, so you already knew.. which is cool. :)
     
    surajsirohi1008 likes this.
  9. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    A bit part of game dev/programming is research. Most problems you encounter have been encountered by many people before you. Getting comfortable with hunting down resources and solutions is something that will pay off in the end.

    Something like this is pretty simple.
    eg. First result for googling "c# string split tutorial"
    http://csharp.net-informations.com/string/csharp-string-split.htm

    People like to help but are more willing if you demonstrate you try and help yourself but also - if it takes a few hours for someone to reply... you dont want to be stuck dead for that long eh!
     
    surajsirohi1008 likes this.