• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Upgrading lvl system

M

mahmoud30pro

Guest
i have this 2 variables

Code:
global.gun
global.gold
I want player can upgrade gun lvl like that

if player have 50 gold he can buy lvl 1 gun and lost 50 gold
if player have 100 gold he can buy lvl 2 gun and lost 100 gold

And like that i did that code

Code:
if global.gun = 0 and global.gun>50
{global.gold-=50
global.gun=1}

if global.gun = 1 and global.gun>100
{global.gold-=100
global.gun=2}
The problem is if player have more than 100 gold and didn't upgrade any thing , When i click on upgrade boutton it's automatic set variable gun to 2 without break !!!

What i do ??
 

Alice

Darts addict
Forum Staff
Moderator
You made three main mistakes:
  • used global.gun variable instead of global.gold
  • checked for gold being larger, rather than larger on equal (if you have 50 gold, you can still afford the Lvl 1 upgrade)
  • didn't separate cases for different upgrades (you used independent ifs instead of if/else)

A quick fix:
Code:
if (global.gun == 0 && global.gold >= 50) {
    global.gold -= 50;
    global.gun = 1;
}
else if (global.gun == 1 && global.gun >= 100) {
    global.gold -= 100;
    global.gun = 2;
}
I did a little cleaning up, in particular:
  • I wrapped the "if" conditions between parentheses
  • changed the equality signs in "if" conditions to double equality signs
  • added semicolons at the end of statements inside "if" blocks
  • replaced "and" operator with "&&"
That's because while GML allows the syntax you used, you might find it easier to move onto other programming languages when you make some good habits. Often, parentheses around if conditions and semicolons at the end of statements are required. Also, a single equality sign means an assignment, whereas double equality sign means comparison. These things are worth keeping in mind.

By the way, the gun upgrading system may be replaced with switch/case like that:
Code:
switch (global.gun) {
    case 0:
        if (global.gold >= 50) {
            global.gold -= 50;
            global.gun++;
            }
        break;
    case 1:
        if (global.gold >= 100) {
            global.gold -= 100;
            global.gun++;
            }
        break;
    case 2:
        // if you want to upgrade beyond level 2...
        break;
    }
Or, if the cost is always 50x the next gun level:
Code:
// you might change the max gun level to something other than 2
if (global.gun < 2 && global.gold >= 50 * (global.gun+1)) {
    global.gun++;
    global.gold -= 50 * global.gun;
    }
Hope this helps. ^^
 
M

mahmoud30pro

Guest
Thank you very very much , I love your post very much , You are my friend now XD
 
Top