Public Group

# Unity How to make an Idle game or incremental game?

This topic is 429 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am trying to get a grasp on those "Idle games/Clicker games" or "Incremental games". I want to write one in Unity3D, but I am not sure how to deal with those high numbers. I wonder how they do to present this? Does anyone here know a trick? Just to make one thing clear first though: I don't think they are all using "Arbitrary precision data types", Sure you _can_ use them but that would probably be like using military/space spec. components in a game console (you _can_ but very much more expensive (creepingly slow in this case)). There are several games out there from several companies, for several platforms, including small battery driven devices, and those do simply not have the power to shuffle that much data without becoming too battery/CPU hungry. So hence I am sure they must have different solutions. So is there any idea on how to implement this _without_ using "Arbitrary precision"? I have googled for a clear description with well commented C# code for use with Unity3D but nothing. Some Youtube video however suggested that for each "Exponent" they divided or multiplied the main number with 1000 but that was not explained either fully. And asking Hyper Hippo would not work I am sure. (As for the rest of the game I am sure there are tutorials, how difficulty is controlled and so on.)
I intend to use Unity3D/C#

##### Share on other sites

So you never said what the problem with high numbers is for you so I am assuming that you reached the int limit: 2,147,483,647

First thing. If you use values like a 100 for each tic/click/whatEver then keeping score and dividing by 100 is a easy way to store data.

So player does 2 click gets score as 200 you divide by 100 and store the int as 2. So you store the clicks not the score.

The whole point behind making a clicker game is to solve the problem of storing numbers, so read the following at your own risk:

Spoiler

The common way of storing numbers like this is to use tens, I forgot what it's called but here is a example:


int one;
int ten;
int hundred;
int thousand;

//So we can store player score as:
//player score = 1500
thousand = 1;
hundred  = 5;
ten      = 0;
one      = 0;

Debug.Log(string.Format("Player Score: {0}{1}{2}{3}", thousand, hundred, ten, one));
//Returns: "Player Score: 1500"

As you can see there is no limit to the above system and all you need to do is add a new int for every new digit. Because each only uses 0-9 you never cross the int limit.

##### Share on other sites

That's one problem yes, the limit. Another is, how to have multiple "adders"? (Called "Profits" in AdCap), All of those need to be able to store data the same way, and also, how to do maths between this, for example if an adder is supposed to multiply it's value by a vertain number. Doing one of the four calculation operations (+-*/) on them, they need to be compatible somehow. But how to do that?

##### Share on other sites
1 hour ago, CubeUser said:

That's one problem yes, the limit. Another is, how to have multiple "adders"?

You would make a class for handling the numbers.

That class would have a get and a set function. Inside the class you would store numbers like that but when dealing with the math you would getplayer score as a structure made from those numbers. You would do the math per int channel and then store it back again.

##### Share on other sites
Just now, Scouting Ninja said:

You would make a class for handling the numbers.

That class would have a get and a set function. Inside the class you would store numbers like that but when dealing with the math you would getplayer score as a structure made from those numbers. You would do the math per int channel and then store it back again.

How would all that look like? How would the math be done?

##### Share on other sites
4 hours ago, CubeUser said:

How would all that look like? How would the math be done?

Piece by piece. You would use an array to make things smoother.

Tried and tested:

Spoiler


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {
//This is a class
public class KeepScore
{
//Here we define our variables
public int[] ThousandMax; //The [] is a array

//When we create a new copy we can input these values. Note it's the class input
//It has the same name as the class
public KeepScore(int InThousands, int InHundreds, int InTens, int InOnes)
{
//Here we say how many itmes is in the array then what each is
ThousandMax = new int[4];

ThousandMax[0] = InThousands;//arrays start at 0 so our 4 array is 0-3
ThousandMax[1] = InHundreds;
ThousandMax[2] = InTens;
ThousandMax[3] = InOnes;
}
//The above class now keeps the score
}

//We make a normal function to add the scores piece by piece
public KeepScore AddTwoScores(KeepScore InScoreA, KeepScore InScoreB){

KeepScore OutScore = new KeepScore

//Next we check if one value exceeds 9
int IndexCounter = 0;
foreach ( int increment in OutScore.ThousandMax){
if (increment > 9) {
OutScore.ThousandMax[IndexCounter] -= 10;//if it does subtract 10
if (IndexCounter > 0){ //Don't add if it is more than a thousand 0 is the first array item and is thousand
OutScore.ThousandMax[IndexCounter-1] +=1; //Remember the larger number is lower in the array
}
}
IndexCounter +=1;
}

return OutScore; //Return it as a score
}
//Lets make a string converter
public string ScoreToString(KeepScore InScore){
string OutString = string.Format ("{0}{1}{2}{3}", InScore.ThousandMax[0], InScore.ThousandMax[1], InScore.ThousandMax[2], InScore.ThousandMax[3]);
return OutString;
}

//now lets use what we made
void Start(){//Unity calls this at the start
//Make two scores
KeepScore Player1Score = new KeepScore (1,2,0,0);
KeepScore Player2Score = new KeepScore (0,9,0,0);

KeepScore Total = AddTwoScores (Player1Score, Player2Score);

print(ScoreToString(Total)); //returns : 2100

//Now lets try to break it
Player1Score = new KeepScore (9,9,9,9);
Player2Score = new KeepScore (9,9,9,9);

print(ScoreToString(Total)); //returns : 9998 because it is 1-9998 but we don't support that much
}
}

You could also do it as a function in the class.

Here I turned the add function to auto so that you don't have to type each one. I just wanted to show how it works first.

Spoiler


public KeepScore AddTwoScores(KeepScore InScoreA, KeepScore InScoreB){
KeepScore OutScore = new KeepScore(0,0,0,0); //Make a empty one
int IndexCounter = 0;
foreach (int increment in OutScore.ThousandMax){
//##########Change###############
OutScore.ThousandMax[IndexCounter] =
InScoreA.ThousandMax [IndexCounter] + InScoreB.ThousandMax [IndexCounter];//Add the increment
//##########Change###############

//Next we check if one value exceeds 9
if (OutScore.ThousandMax[IndexCounter] > 9) {
OutScore.ThousandMax[IndexCounter] -= 10;//if it does subtract 10
if (IndexCounter > 0){ //Don't add if it is more than a thousand 0 is the first array item and is thousand
OutScore.ThousandMax[IndexCounter-1] +=1; //Remember the larger number is lower in the array
}
}

IndexCounter += 1;
}

return OutScore; //Return it as a score
}

If I do any more than this for you it would be my game. Good luck

##### Share on other sites

Thank you :-) It's quite a lot so I will save this to disk. In the now obsolete Microsoft VisualBasic 6 there was a data type called "Double" that could save really huge values, it could reach as far as "Adventure Capitalist". But I am not sure if that data type is in Unity C# and if it is, I doubt it is possible to cast the values to a string to check what number somes after "+E" to control things or translate. I guess it was there for some database purpose. I will experiment and see if I can find something similar once I get this you helped me with clear :-D

##### Share on other sites
1 hour ago, CubeUser said:

"Double" that could save really huge values

They are in all languages that I know.

10 hours ago, Scouting Ninja said:

So you never said what the problem with high numbers is for you so I am assuming that you reached the int

That is why I wrote this. I assumed you knew there was doubles and longs but for some reason could not use them. So I showed that way instead.

Edit:

I feel I should clear up something.

Doubles are inaccurate and Longs don't store much higher numbers.

int     = int32 = 2 147 483 647

long = int64 = 9 223 372 036 854 775 807

So if you started your game in the thousands it isn't that long before it breaks past int64.

Doubles are unstable and in your game this will matter.

for example: 1/3 will produce 0.333 to what ever number the double can hold. However it will be wrong because it's a infinite number stored on a finite computer.

To take advantage of the doubles storing you need to devide, so sooner or later you will get mistakes. Because the integer part of a double has a similar limit to a int64.

Also subtracting a double from a double will often lead to inaccuracies.

For this reason games that use large numbers often use the system I showed before.

Edited by Scouting Ninja

##### Share on other sites
On 2017-11-06 at 1:49 AM, Scouting Ninja said:

They are in all languages that I know.

That is why I wrote this. I assumed you knew there was doubles and longs but for some reason could not use them. So I showed that way instead.

Edit:

I feel I should clear up something.

Doubles are inaccurate and Longs don't store much higher numbers.

int     = int32 = 2 147 483 647

long = int64 = 9 223 372 036 854 775 807

So if you started your game in the thousands it isn't that long before it breaks past int64.

Doubles are unstable and in your game this will matter.

for example: 1/3 will produce 0.333 to what ever number the double can hold. However it will be wrong because it's a infinite number stored on a finite computer.

To take advantage of the doubles storing you need to devide, so sooner or later you will get mistakes. Because the integer part of a double has a similar limit to a int64.

Also subtracting a double from a double will often lead to inaccuracies.

For this reason games that use large numbers often use the system I showed before.

Do why not implement a division routine that that divides the double value and then truncates like for instance  "int(value1*100)/3"/10?  (Somehow the result should be cast back to a double). I mean anything divided with 3 will be inaccurate anyway sooner or later anyway  as it is an infinite number of 3's. I also searched the web for Adventure Cppitalist and "double" and it came up that someone said it uses that type. Also there are more things to the original method you mentioned: Those large values are also found in upgrades, their prices and also how they influence the "Score". They must be able to also subtract, multiply and divide. How are those operations done on your array example?

Edited by CubeUser

##### Share on other sites
9 hours ago, CubeUser said:

Do why not implement a division routine that that divides the double value and then truncates like for instance  "int(value1*100)/3"/10?

Because you convert to a int. So you lose the precision of the double. If you use a int at any time it will become your bottleneck.

You will basically cut away the number if it's larger than what the int can take. For example:

double MyDouble = 0.1 111 111 111 999
MyDouble  * 10 000 000 000 000 = 1 111 111 111 999

(int)MyDouble * 10 000 000 000 000 = -2147483648 //This is the int failing to store the value of the double.

So using your above trick you would be limited to what a int can store. So your back to problem one, int can't store much.

9 hours ago, CubeUser said:

. I mean anything divided with 3 will be inaccurate anyway sooner or later anyway  as it is an infinite number of 3's.

Yes exactly. You can't use a computer to divide by 3. It's one of the common problems you will get while working with games.

To avoid division errors you can use multiplication  1 /2 = 1 *0.5 = 0.5;    100 *0.5 = 50;

This keeps things in what number system you are using because 1/3 =0,3333333(float) but 1 * 0.333  = 0.333. So it's a easy way to limit numbers without using int or min and max.

9 hours ago, CubeUser said:

I also searched the web for Adventure Cppitalist and "double" and it came up that someone said it uses that type.

You are free to use it. It's your own choice. Doubles should do all you need.

9 hours ago, CubeUser said:

their prices and also how they influence the "Score". They must be able to also subtract, multiply and divide. How are those operations done on your array example?

Piece by piece using basic math. So 120 /2

The key is to work from the Ones upwards.

Ones 0/2 = 0;

Tens 2/2 = 1;

Hundreds  1/2= 0.5; // We use a if statement to send the 5 to the tens.// Tens is now 6

= 0 6 0 = 120 /2 = 60;

555 / 4 = 138.75

Ones 5/4 = 1.25 // We discard the 0.25 as it isn't supported.

Tens 5/4  = 1.25 // We discard 0.05 but keep the 2 as a one// Ones is now 3

Hundreds = 1.25// We add 2 to Tens and 5 to ones. // Tens is now 3 // Ones is now 8

= 1 3 8 = 555/4 = 138.

This system has a name I just can't remember what it is. It's used in large 3D games to keep track of measurements. Like in space games. In fact I learned it for space games. I had whole planets shaking around in space because of the doubles.

This number system Is a shortcut. Where instead of trying to store data smartly on your PC, you hack it and store each ten in it's own 2-4 bytes. A googol is 1 to the 100th power. You would need a 101 int to store it using the system I showed. That is 4* 101 = +/- 404bytes depending on many factors, less than a single profile picture.

A googol is 101 digits, looks like this 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

A double can store 52 + 971 = 1023 digits. More than 10 googols.

So for a game where players only see the numbers they won't notice the errors of the doubles. You can use them safely.

Again, your original question did not make clear your problem, so I assumed you had problems with using doubles and decimals.

Edited by Scouting Ninja

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634102
• Total Posts
3015536
×