Currency system similar to "Clicker Heroes" in C#

Hey there!

I’ve been working on an idle clicker game for little under a week now and I’ve seen to hit a stump in development. A lot of the core features of the game are done (Upgrades, points over time, sounds etc…) But, this is just bugging me.

The game needs to be able to handle large numbers, you know, the numbers that put Unity into the negatives, and I have an idea on how I want to do this, it’s just the execution, I can’t do it!

So, I need to be able to make numbers (once they reach like 10,000 etc…) To be formatted like “10k” Instead of “10000” and then from there make it so that instead of “10000k” I could have “10m” I’m not sure how to approach this and I was wondering if you guys could help?

I’m not asking specifically for a script either, that wouldn’t work, I was just wondering if you could tell me how you’d approach it, and how else it could be done efficiently.

P.S. Here is the URL to ClickerHeroes, so that you understand the system I am hoping for… http://www.clickerheroes.com/

The other point to be aware of with idle games is the underlying limitations of the data struct you are using. For example an int maxes out at 2,147,483,647. A float can go much higher, but lacks the precision for an idle game. Even an int64 maxes out at 9,223,372,036,854,775,807. I've seen idle games that go beyond this limit. The best solution would be to chain int64 together. There are a few answers around that show how to do that.

Thanks for this, would it be better to do this right now? Or would it be just as easy near the end of development?

I would suggest implementing it early. Controlling the amount of money a player has is a fundamental mechanic to idle games. Check out my answer to this similar question for a basic script on chaining int64. http://answers.unity3d.com/questions/803248/questions-about-numbers-c.html

This goes beyond any creative solution of "try it this way" your posts correct and improve on a lot of my and other members post with cold hard facts of Unity. Sounds a bit harsh on re-reading but I am genuinely impressed by the likes of yourself @BoredMormon and people like BurgZerg Arcade on YouTube. Kudos even if your Karma rating is already through the roof.

Hey @BoredMormon I know, I already viewed a number of your Youtube posts & subscribed. Recommend others do as well. Not that I'm holding you up as the only source of information on Unity. Just a very, very good one. The community is a key part of Unity. Anyway it's late where I am so at the risk of being too gushingly positive I'll leave it now, but thanks.

2 Answers

2

OK you just hold the number in a variable(e.g. myNumber), then when you want to display it just test the size off the number so

if(myNumber > 999 && myNumber < 1000000)
{
    tempFloat = myNumber / 1000;
    numToDisplay = (int)tempFloat;
    TextToDisplay = "k";
}

if lower than 1000 the set numToDisplay = myNumber and TextToDisplay = “”

if larger than 999999 then divide by 1000000 and set your text to “m”

You may need to look up rounding so it always rounds down but it should work along those lines.

Okay, I've tried using this piece of code, I'm just lost a little (I'm only an amateur coder) So, I integrated this into my code and I think the problem that I am facing is because of the way I try to display the value, is this the correct code to be using? gameObject.GetComponent ().text = LM.numToDisplay.ToString() + LM.TestToDisplay; (All one line) in this script,the LM. refers to my LevelManager script, this is where the values of the variables are stored, any suggestions?

close: gameObject.GetComponent().text but it also depends on how where that script is as you're referencing the local gameObject. Try it and if it fails post the script or as much of it as you can.

Ha that needs to be in Code! gameObject.GetComponent<Text>().text = LM.NumToDisplay.ToString() + LM.TextToDisplay;

Okay! I seem to have it working! Thanks for your help.

thinking about you might have posted that yourself as it removes HTML tags from normal text so <Text> Would be removed as it looks like HTML unless it's in code!

Well, I Don’t know if there may be a formatting numbers this way via string.Format() (that would be the best way but I didn’t find it with a quick view) the naive way of doing it is writting a function that does the job. Something like this should do it. Can’t test it right now.

	private string formatNumber(int number) {
		// more than a million add a "m"
		if(number > 999999) {
			return (number / 1000000).ToString() + "m";
		}
		// more than a thousand - add a "k"
		else if(number > 999) {
			return (number / 1000).ToString() + "k";
		}
		// else to nothing
		return number.ToString();
	}

Pretty similar code, like the idea of turning it straight into a string but whichever way you do you'll need to round the figure off. @Mexallion not accusing you of plagiarism, although if it was that's astounding typing skills with only a minute difference in posting times! :¬)

I Didn't see your post I don't tend to reload the page while writing :D should delete my answer though

Don't know what the etiquette is on Answers but you can leave if there if you want, it does do subtly different stuff to mine so it could help someone else.