Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Writing a large number to the phone's local storage

Discussion in 'Android' started by GHEBAH, Jun 4, 2021.

  1. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    Good afternoon

    Among my projects is the game "clicker". I have encountered a problem, I understand why it occurs, but, unfortunately, I can not solve it. Count The value of clicks is written to the variable, and the lvl up variable is written to the number of clicks that you need to make to go to the next level. These two variables have an int data type and the number in lvlup naturally grows with each level. At a certain point, when the number becomes very huge, I get a strange negative number, instead of a positive one. I assume that this is due to the int data type and the value simply does not fit there anymore, I could just do it instead of int and for a long time , but. The value from the graph variable is entered in the phone's local storage with the string PlayerPrefs.setInt ("Counts", Count); and if I change the data type, I get the error "error CS1503: Argument 2: cannot convert from "long" to " int ""
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,875
    You could always do PlayerPrefs.SetString("Counts", Count.ToString()) and then Count =
    Convert.ToInt64(PlayerPrefs.GetString("Counts"))
     
    GHEBAH likes this.
  3. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    From the solutions all that comes to me is this: 1-Use calculations, for example, divide a number so that it takes up less space, but then you will have to use floating-point numbers everywhere 2-Change the logic of the game so that there are no such large numbers, but I would not like this
     
  4. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    Interesting suggestion, I'll try it now
     
  5. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    I think all my problems have been solved, thank you!
     
  6. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    ah no! An error occurred(for some reason not immediately)

    FormatException: Input string was not in a correct format.
    System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <567df3e0919241ba98db88bec4c6696f>:0)
    System.Number.ParseInt64 (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <567df3e0919241ba98db88bec4c6696f>:0)
    System.Int64.Parse (System.String s, System.IFormatProvider provider) (at <567df3e0919241ba98db88bec4c6696f>:0)
    System.Convert.ToInt64 (System.String value) (at <567df3e0919241ba98db88bec4c6696f>:0)
    Menu.FixedUpdate () (at Assets/Menu.cs:69)

    line 69
    Count = Convert.ToInt64(PlayerPrefs.GetString("Counts"));
     

    Attached Files:

  7. GHEBAH

    GHEBAH

    Joined:
    Feb 3, 2019
    Posts:
    20
    Apparently, some of the other scripts wrote something else to this variable in the registry after adding the line: PlayerPrefs. setString ("Counts", 0.toString ()) before Count = Convert.ToInt64(PlayerPrefs.GetString("Counts")); (temporarily, to solve the problem) Everything became nomralno (other scripts also fixed)
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,875
    To be on the safe side, you could use https://docs.microsoft.com/en-us/dotnet/api/system.int64.tryparse?view=net-5.0 instead, in case the passed value is not a valid number, you could implement a fallback.