So this is my code which is basically a simple mining system i implemented into my game. Whilst i’m happy with the code i kind of don’t want it all to be in the update function as it continues on as it’ll end up being very long. The bit i want to switch out is this part
if( miningXP == 20)
curMiningLevel = 2;
if(curMiningLevel == 2)
{
miningXP = miningXP + 10;
Debug.Log(“Congratulations! You Reached Level 2 Mining!”);
}
I know this will grow bigger as i make more if statements to increase the xp gained for each level. So where else could i put this code besides the update function?
Regardless, it’s really not a good idea to hard-code everything like that. You should use an array that holds the possible mining XP values, then you only need 1 if statement, and the code will never have to change if you add more levels later.
Even better, if you make a simple algorithm that gives you the needed xp, like this:
Needed Xp = Levels * 10 + 20;
I’ts even better, than hard coding, because then you don’t need to specify every level, and the leveling up system can be infinite (or you can cap it).
This is my first attempt at creating a simple system like this so it’s all good practice i guess since i’m fairly new to programming. Am enjoying it tho! ^-^
This sounds like a good way to do things so how would i apply this exactly to my script? Also if you don’t mind could i ask you about other things i things i was thinking of implementing?
multiplier is the value that makes the needed xp bigger
if the multiplier is 10, then at level 1 it’s going to take 10 xp for leveling up at level 2 20, at level 3 30 and so on
the base xp is just optional
example:
the baseXp is 20, multiplier is 10
level 1: you need to collect 1 * 10 + baseXp xp to level up, so you need 30 xp
level 2: you need to collect 2 * 10 + baseXp xp to level up so you need 40 xp
…
just create a variable like remainingXp then in the Update() :
remainingXp = curMiningLevel * multiplier + BaseXp - miningXp;
And i forgot to mention, that you need to reset the xp count when leveling up
//This is defining ints at start of code
int neededXP;
int Xp;
int multiplier
int level
int baseXp
//then in function code something like this
neededXp = baseXp + multiplier * level
if (Xp > neededXp){
level++;
xp -= neededXp; // this resets xp but also keeps any surplus of neededXp for example you got 110 xp and needed 100, you will level up and have 10 xp left, or you can just set Xp=0 for hard reset
}
this is just gorbits answer but i tried to simplify it.
If i wanted to include more ores in my game, would i be able to use the same script to include them? Like if the character was level 5 they’d be able to main the 2nd ore etc etc?