Is there elsewhere i can put this code?

void Update ()
   
    {

        if(Input.GetKeyDown(KeyCode.LeftShift))guiOn = !guiOn;

        if(mining)
        {

            mineTime += Time.deltaTime;
           
            if(mineTime >= mineDelay)
            {

                mining = false;
                toggleMine = true;

                if(Random.value > 0.5)
                   
                {
                    miningXP = miningXP + 5;
                    bronzeOre = bronzeOre + 1;
                    Debug.Log("You Got Some Bronze Ore!");
                   
                   
                    if( miningXP == 20)
                        curMiningLevel = 2;
                   

                   
                   
                    if(curMiningLevel == 2)
                    {
                        miningXP = miningXP + 10;
                    Debug.Log("Congratulations! You Reached Level 2 Mining!");
                    }
                   

                }
                else
                {
                    Debug.Log("You Missed The Rock");


                }
            }
        }
    }

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?

For organization, you can split it up into other functions and just call those from Update.

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.

–Eric

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? :slight_smile:

Okay, which answer are you speaking about?

Yours with the algorithm. I understand how it’s supposed to work but i’m not sure how to implement it into my script. Could you help :)?

Surely :slight_smile:
Instead of:

if (miningXp == 20)  {
   miningLevel = 2;
} else if (miningXp = 30) {
   miningLevel = 3;
}...

You could do this:

if (miningXp > miningLevel  * multiplier + baseXp) {
   miningLevel++;
}

You could modify the multiplier with script to:

modifier = miningLevel * miningLevel //very cruel
or
modifier = miningLevel * 2 //little bit cruel
or
modifier = miningLevel + 3 //not so cruel

Okay this is what I’ve got!
I think this is right but, i’m not sure what to input for the values to make it work okay. Any input :')?

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

All these things are trial and error, if you feel, that the gameplay isn’t hard enough, you could patch these variables

Ah great i’ll have a try!
Any idea how to create an option so i can see the amount of xp remaining till next level?

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

They an easy way to reset it :')?

just create formula for leveling like

//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.

Is there anyway i can set it so that when my Mining XP increase’s the Remaining XP also goes down?

yeah just that variable remainingXP calculation put outside if statment because it updates only when you level up

if (blabla){
  //bla bla
}
remainingXp = blabla;

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?

i dont know your whole code but yeah this is useable for every ore and needs some editing if you want ore to be mined after level 2