Search Unity

How can I make my games Score Formatted for Example 1000-1k

Discussion in 'Scripting' started by GuzLe, Jul 26, 2017.

  1. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    So I have already read a lot of answers but i am very very new too Unity and coding and have very little understanding.. I am a graphics designer and am having a little fun with this project I have used tutorials for all my code so not sure where i am supposed too put things. Thanks for the help

    Here is my scripts :)

    [GoldPerSecond]
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class gps : MonoBehaviour {
    5.  
    6.     public UnityEngine.UI.Text gpsDisplay;
    7.     public RocksClick click;
    8.     public ItemManager[] items;
    9.  
    10.     void Start (){
    11.         StartCoroutine (AutoTick ());
    12.    
    13.     }
    14.  
    15.     void Update(){
    16.         gpsDisplay.text = GetGoldPerSec () + " g/sec";
    17.     }
    18.  
    19.     public int GetGoldPerSec(){
    20.         int tick = 0;
    21.         foreach (ItemManager item in items) {
    22.             tick += item.count * item.tickvalue;
    23.         }
    24.             return tick;
    25.     }
    26.  
    27.     public void AutoGoldPerSec(){
    28.         click.gold += GetGoldPerSec ();
    29.     }
    30.  
    31.     IEnumerator AutoTick(){
    32.         while (true) {
    33.             AutoGoldPerSec ();
    34.             yield return new WaitForSeconds (1);
    35.         }
    36.     }
    37. }
    38.  
    [I have this Script connected to my balance for the Updates]
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RocksClick : MonoBehaviour {
    5.  
    6.     public UnityEngine.UI.Text gpc;
    7.     public UnityEngine.UI.Text goldDisplay;
    8.     public float gold = 0.00f;
    9.     public int goldperclick = 1;
    10.  
    11.     void Update(){
    12.         goldDisplay.text = " " + gold;
    13.         gpc.text = goldperclick + " g/click";
    14.     }
    15.  
    16.     public void Clicked(){
    17.         gold = gold + goldperclick;
    18.     }
    19.  
    20. }
    21.  
    Thanks very much -GuzLe
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    First off, I'd recommend that you set your text when it's updated, rather than in the method called "Update" :)
    Change it when it's changed, so to speak.

    Besides that, check for when your value is greater than 1,000 and then divide by 1000. Keep the fraction if you want it, or format/round it off, or simply keep the integer, whatever your preference is.
     
    GuzLe likes this.
  3. GameDevJon

    GameDevJon

    Joined:
    Jul 21, 2017
    Posts:
    25
    You'd like to convert your 1000 to 1k?

    What happens if you have 1500 should it be 1.5k?

    What about 1256? Should it round down to 1.2k? or 1.2k?

    Either way, this is pretty simple
    .
    Code (CSharp):
    1. string ScoreAsText = (score / 1000).tostring("D") + "K";
    Lets say the score is 3475.
    (score / 1000) gives you 3.475
    .tostring() makes a number into a string
    .tostring("D") makes a number into a string with no decimal places
    + "K" obviously adds the K
     
  4. GameDevJon

    GameDevJon

    Joined:
    Jul 21, 2017
    Posts:
    25
    Sorry. Unity prefers Floats. Change the D to an F
     
  5. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    Unfortunately i am a newbie at Unity like literally I have no idea where i would do that or how i would do it. Appreciate the help though :p
     
  6. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    Thank you so much for this and yeah 1500 to 1.5k pretty much.. and 1,000,000 to 1m
    however where would i put this in my code, I believe i have seen that before but wherever i put it, it never did anything
     
  7. GameDevJon

    GameDevJon

    Joined:
    Jul 21, 2017
    Posts:
    25
    Wherever you display the money to your player.


    Code (CSharp):
    1. if (score > 100000) textScore = (score / 10000).ToString("F") + "B";
    2. else if (score > 10000) textScore = (score / 10000).ToString("F") + "M";
    3. else if (score > 1000) textScore = (score / 1000).ToString("F") + "K";
    This is quite taxing. Probably better to use Log10. But will work. This just counts the amount of 0's.
     
  8. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    ok thanks i will give this a shot and ill upload my code if it dont work.. I am sorry I have never coded in my life
     
    GameDevJon likes this.
  9. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    I did it wrong :p
    Code (CSharp):
    1. public class RocksClick : MonoBehaviour {
    2.  
    3.     public UnityEngine.UI.Text gpc;
    4.     public UnityEngine.UI.Text goldDisplay;
    5.     public float gold = 0.00f;
    6.     public int goldperclick = 1;
    7.  
    8.     void Update(){
    9.         goldDisplay.text = " " + gold;
    10.         gpc.text = goldperclick + " g/click";
    11.  
    12.         if (gold > 1000000000) goldDisplay = (gold / 1000000000).ToString("F") + "B";
    13.         else if (gold > 1000000) goldDisplay = (gold / 1000000).ToString("F") + "M";
    14.         else if (gold > 1000) goldDisplay = (gold / 1000).ToString("F") + "K";
    15.     }
    16.  
    17.     public void Clicked(){
    18.         gold = gold + goldperclick;
    19.     }
    20.  
    21. }
    22.  
     
  10. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    It looks like you're trying to create a cookie clicker game where you can have crazy amounts of money up to the zillions (literally). In that case, simply having a float for gold won't cut it. Just a FYI.

    But yes, you'll want to move your text update stuff into Clicked() so it only updates when gold actually changed.
     
  11. GuzLe

    GuzLe

    Joined:
    Jul 26, 2017
    Posts:
    11
    Your spot on mate I am just for fun lol and have no idea about any of this stuff! And a video told me too put it in a float how would i change it into something else mate?

    And shall i move the GoldDisplay text into clicked aswell or just the if/else bits??
     
  12. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Pretty much everything you've got in the Update() function is for updating the gold display, so yes.

    As for how to handle insanely large numbers, you'd pretty much have to do some special handling for this. Like a large number class that stores how many times the number was divided by 1000, plus the base amount, for example.

    Or use a byte array that you can keep increasing as the number gets bigger...

    System.Numerics.BigInteger is also an option, but that's .Net 4.0 and above.