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. Dismiss Notice

Best way to store version number?

Discussion in 'Scripting' started by QFSW, Oct 29, 2016.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Hi all, little query here

    I want to add a version number to my game. one that can be displayed in the corner and can be used to check if its up to date etc etc

    What would be the best way to store this, so it can be displayed like a string (ie X.YY.ZZ) but still comparable like an integer? (==,!=,>,<,>=,<=)

    Thanks in advanced :)
     
  2. JeffHardddyyy

    JeffHardddyyy

    Joined:
    Dec 6, 2015
    Posts:
    23
    It's all how you rather have it.


    I would look at the values (it says for java script but it works for C# too.)

    But To save you some time, I'll answer the ones you listed :

    == : Equal to
    != : Not Equal To
    > : Greater Than
    < : Less Than
    >= : Greater Than/Equal To
    <= : Less Than/Equal to
     
  3. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Sorry if that came off wrong, I know what those mean, I meant so that you could compare the version numbers with those statements if that makes sense
     
  4. JeffHardddyyy

    JeffHardddyyy

    Joined:
    Dec 6, 2015
    Posts:
    23

    I apologize, I might of read it wrong too..

    Just to make sure :
    You want it so it immediately checks to see if theres a update out, And if there's a new version, it'll say?
     
  5. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    You could store it as three integer and compare them one after the other. You could even create a custom datatype.

    Code (CSharp):
    1. public struct VersionNumber {
    2.     public int x { get; set; }
    3.     public int y { get; set; }
    4.     public int z { get; set; }
    5.  
    6.     public override string ToString() {
    7.         return string.Format("{0:0}.{1:00}.{2:00}", x, y, z);
    8.     }
    9.  
    10.     public static bool operator <(VersionNumber a, VersionNumber b) {
    11.         return a.x < b.x || a.x == b.x && a.y < b.y || a.x == b.x && a.y == b.y && a.z < b.z;
    12.     }
    13.  
    14.     public static bool operator >(VersionNumber a, VersionNumber b) {
    15.         return a.x > b.x || a.x == b.x && a.y > b.y || a.x == b.x && a.y == b.y && a.z > b.z;
    16.     }
    17.  
    18.     public static bool operator <=(VersionNumber a, VersionNumber b) {
    19.         return a.x < b.x || a.x == b.x && a.y < b.y || a.x == b.x && a.y == b.y && a.z < b.z || a.x == b.x && a.y == b.y && a.z == b.z;
    20.     }
    21.  
    22.     public static bool operator >=(VersionNumber a, VersionNumber b) {
    23.         return a.x > b.x || a.x == b.x && a.y > b.y || a.x == b.x && a.y == b.y && a.z > b.z || a.x == b.x && a.y == b.y && a.z == b.z;
    24.     }
    25.  
    26.     public static bool operator ==(VersionNumber a, VersionNumber b) {
    27.         return a.x == b.x && a.y == b.y && a.z == b.z;
    28.     }
    29.  
    30.     public static bool operator !=(VersionNumber a, VersionNumber b) {
    31.              return a.x != b.x || a.y != b.y || a.z != b.z;
    32.     }
    33.  
    34.     public override bool Equals(object obj) {
    35.         return obj is VersionNumber && (VersionNumber) obj == this;
    36.     }
    37.  
    38.     public override int GetHashCode() {
    39.         return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
    40.     }
    41. }
    And if you are certain, that the versionnumber follows a specific pattern (like outlined in the ToString method: #.##.##), you could add a toInt property and compare that instead.

    Code (CSharp):
    1. public int toInt {
    2.     get { return (x * 100 + y) * 100 + z; }
    3. }
     
    Last edited: Oct 29, 2016
    JeffHardddyyy likes this.
  6. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    I can handle that stuff, I'll make it specific what im asking to avoid any confusion

    I want a variable/object where I can store the version number
    e.g "1.3.43"
    and this needs to be comparable to another of the same type
    so i need to be able to do Version>1.3.43 etc. cant use an integer as it would lose the details but a string wouldnt be comparable that way
     
  7. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    This seems to do what i was thinking of, gimme a chance to test it out and I'll let you know :)
     
  8. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Ignore my ignorance, but how would I declare a variable of this type and give it a value or do i need to make a constructor? would you also know how to make it accesible via the inspector?
     
  9. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    You can have a constructor, or simply write something like this:

    Code (CSharp):
    1. VersionNumber vn = new VersionNumber {x = 2, y = 34, z = 7};
     
  10. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    ah okay, if i add the constructor ill be able to use either method, correct?
     
  11. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Yes, but if you write a constructor, be sure to call the parameterless constructor. Something like this:

    Code (CSharp):
    1. public VersionNumber(int x, int y, int z) : this () {
    2.         this.x = x;
    3.         this.y = y;
    4.         this.z = z;
    5.     }
    Edit: If you want to see it in the Inspector, add the System.Serializable attribute to the struct, and change the x,y,z properties into fields (or add serialized private fields and use the properties as readonly accessor, but that breaks the field initializer way of creating an object of the type)
     
    Last edited: Oct 29, 2016
  12. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Awesome, thanks for the help :)
     
  13. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    One last request, would making a string to VersionNumber construction be as easy as just splitting it by decimal and taking the different parts?
     
  14. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Not sure if it is the most clever way, but this works:

    Code (CSharp):
    1. public static VersionNumber FromString(string input) {
    2.     string[] parts = input.Split('.');
    3.  
    4.     if (parts.Length != 3) throw new ArgumentException("String does not denote a valid VersionNumber");
    5.  
    6.     return new VersionNumber(
    7.         System.Convert.ToInt32(parts[0], CultureInfo.InvariantCulture),
    8.         System.Convert.ToInt32(parts[1], CultureInfo.InvariantCulture),
    9.         System.Convert.ToInt32(parts[2], CultureInfo.InvariantCulture)
    10.     );
    11. }
     
  15. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Whilst waiting I gave it a crack
    Code (CSharp):
    1. public VersionNumber(string Version)
    2.     {
    3.         try
    4.         {
    5.             string[] Parts = Version.Split('.');
    6.             try { this.x = int.Parse(Parts[0]); } catch { this.x = 0; }
    7.             try { this.y = int.Parse(Parts[1]); } catch { this.y = 0; }
    8.             try { this.z = int.Parse(Parts[2]); } catch { this.z = 0; }
    9.         }
    10.         catch { this.x=0; this.y = 0; this.z = 0; }
    11.     }
    does this look fine or do you recommend i use yours?
     
  16. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    If it works, use it. Yours has the benefit that it doesn't throw an exception, but it can silently generate a invalid object, if the string doesn't match the template. You decide what you prefer.
     
  17. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Does your's have any benefits over mine? just because if it does, i'll happily integrate the two together
    mine seems to work from what ive tested
     
  18. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    They are basically the same. Convert.ToInt32 returns 0 if the string is null, int.Parse throws an exception. You can read more about this here.
     
  19. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    ah okay, ill use your approach there then :)
     
  20. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    why not just use what is built into .net and c# System.Version it lets you comparing version numbers, as well as printing them.
     
  21. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    Whelp...apparently that's a thing. Well, forget I ever said anything in this thread.
     
  22. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,905
    Sigh XD
    I've just finished making the thing I needed it for now, so I dont know if i'll bother switching over