-1
\$\begingroup\$

Whenever the game sessions ends and the Game Over scene is loaded, the score is stored in this float, which is in another script that I do not have on here:

gameOverScore

How would I get gameOverScore to show in Hindi? The script I made earlier for the actual conversion is not quite working:

    char[] hindiNumbers = {'\u0966','\u0967', '\u0968', '\u0969', '\u096A', '\u096B', '\u096C', '\u096D', '\u096E', '\u096F'};
    int [] digits={0,0,0,0,0,0,0,0,0,0};
    //char[] hindiScore;

    private float gameOver_LastestScore;    //Score brought FROM Managers...

    public float gameOver_LastestScore_;    //Score GOING BACK TO Managers...

    private static GameOver_HindiConversion instance;

    void Awake ()
    {
        instance = this;
    }

    public static GameOver_HindiConversion Instance
    {
        get
        {
            return instance;
        }
    }

    void Start () 
    {
        gameOver_LastestScore = PlayerPrefs.GetFloat ("PlayerPref_ScoreCount");

        gameOver_LastestScore_ = convertToHidi ((int)gameOver_LastestScore);
    }

    public int convertToHidi(int score)
    {
        //Start: split score into digits
        int remainingScore = score, i = 0;
        while (remainingScore > 0) 
        {
            int digit = remainingScore % 10;
            ///Debug.Log (digit.ToString ());
            digits [i] = digit;

            remainingScore = remainingScore / 10;
            Debug.Log (remainingScore.ToString ());
            i++;
        }
        ///Debug.Log ("i="+ i.ToString ());
        //End: split score into digits

        //Start: Display digits in hindi

        for (int j = i; j > 0; j--) 
        {
            //textView.text += hindiNumbers [digits [j - 1]].ToString();

            gameOver_LastestScore += hindiNumbers [digits [j - 1]];
        }
        return (int)gameOver_LastestScore;

        //End: Display digits in hindi
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I don't think I understand what you're doing- you convert (int) digits into an array of digits then you concatenate (char) values and return an (int) to store into a (float). Can you provide an example input and the outcome you expect? \$\endgroup\$
    – Felsir
    Commented Jun 22, 2017 at 15:53

2 Answers 2

0
\$\begingroup\$

Instead of converting the number to int you should construct a String from the characters instead. The only thing you have to change is your last part.

String number = "";
for (int j = i; j > 0; j--) {
    number += Character.toString(hindiNumbers [digits [j - 1]]);
}

// Use number elsewhere...

The variable number is now your number represented in Hindi numbers. You'd also have to save the number as a String instead of an integer since an integer can't be defined in hindi numbers (nor can any other primitive number data type).

\$\endgroup\$
2
  • \$\begingroup\$ You may want to consider using the StringBuilder class instead of repeatedly concatenating — it's designed to optimize this kind of text construction. \$\endgroup\$
    – DMGregory
    Commented Jun 23, 2017 at 21:51
  • \$\begingroup\$ @DMGregory If you're going to concatenate often this is true but since this will probably only be run ever so often there's really no point unless there's a lot of concatenations. \$\endgroup\$
    – Charanor
    Commented Jun 23, 2017 at 22:03
0
\$\begingroup\$

If I understand correctly, this might do what you want. Just replace your convertToHidi with the provided one.

    public string convertToHidi(int score) {
        var scoreChars = score.ToString().ToCharArray();

        for (var i = 0; i < scoreChars.Length; i++) {
            scoreChars[i] = hindiNumbers[scoreChars[i] - 48];
        }

        return string.Join("", scoreChars);
    }

You pass your score, and it returns a string, not an int. The string will be using your hindi characters for each digit.

You can set it like

gameOver_LastestScore_ = convertToHidi(gameOver_LastestScore)

Try to use more descriptive variable names to avoid confusion. gameOverLastestScoreText would be easier to understand than gameOver_LastestScore_.

If you want to test it online, it is here.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .