• 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!

Adding Lives When Reaching a Certain Score?

E

Elric

Guest
Hi, I'm currently learning GameMaker Studio by working on an Asteroids clone (from Shaun Spalding's tutorial). I have pretty much finished the game, and am now adding my own mechanics to further get accustomed to GameMaker and GML.

I'm trying to add the ability to gain lives through achieving a certain score. Basically, adding a new life every time the player scores x-amount of points. I've only begun recently and am still learning the syntax and different functions, so please bear with me.

I've tried this, however, nothing happened.

Code:
if (global.points = global.points + 1000)
{
    global.life = global.life +1
}
I also tried this code:

Code:
if (global.points > 1000)
{
    global.life = global.life + 1
}
The second piece of code actually gave a response, but not the desired one. Instead of the desired effect, it just continuously added an unending amount of lives for each frame after the player achieved over 1000 points.

Any ideas? Am I even using the proper function? Should I be using something other than the "if" function?

I appreciate any help that can be given. Thank you!
 
J

JTC

Guest
I'm assuming this code is being run in the Step event. Consider the logic of your first block. When will the current global.points value equal the current global.points value + 1000? Never. That's why your block never runs.

In the second example, as soon as you get more than 1000 points, the if statement returns 'True' and will fire the block underneath, adding 1 life every Step.

There are many ways of solving this. The simplest is as follows.

Add this to the Create event:

Code:
scoreBenchmark = 1;
Then, in the Step event:

Code:
if (global.points > scoreBenchmark * 1000)
     {
          scoreBenchmark = scoreBenchmark + 1;
          global.life = global.life + 1;
     }
As you can see, the 'scoreBenchmark' variable keeps track of which score thresholds (i.e 1000, 2000, 3000) have 'paid' their additional life to the player, ensuring that they only 'pay' it once!
 
E

Elric

Guest
I'm assuming this code is being run in the Step event. Consider the logic of your first block. When will the current global.points value equal the current global.points value + 1000? Never. That's why your block never runs.

In the second example, as soon as you get more than 1000 points, the if statement returns 'True' and will fire the block underneath, adding 1 life every Step.

There are many ways of solving this. The simplest is as follows.

Add this to the Create event:

Code:
scoreBenchmark = 1;
Then, in the Step event:

Code:
if (global.points > scoreBenchmark * 1000)
     {
          scoreBenchmark = scoreBenchmark + 1;
          global.life = global.life + 1;
     }
As you can see, the 'scoreBenchmark' variable keeps track of which score thresholds (i.e 1000, 2000, 3000) have 'paid' their additional life to the player, ensuring that they only 'pay' it once!
That did just the trick. Very clever, thank you!
 
Another method, just for giggles:

Create Event:
Code:
next_score = 1000;
Elsewhere:
Code:
if (score >= next_score)
{
    global.life += 1;
    next_score += 1000; // Or whatever.  You could have different score requirements for higher score levels, eg: First fives bonus lives need +1000 each, then next 5 +2000 each, after that + 5000 each
}
You can also dispose of the extra variable all together by using a script and only checking when the script is called, example:

Script: add_score
Code:
/// add_score(amount);

var amt = argument[0];

if (floor((score + amt) / 1000) != floor(score / 1000))
{
    global.lives += 1;
}

score += amt;
... And you use it like thus:

Code:
add_score(100); // Adds 100 points
// Instead of...
score += 100;
 
M

Meowmere

Guest
if global.points = 1000
{
global.life +=1
global.points += 100 //you can change this to anything that you think a 1up is worth.
}

is what I woulda done exactly.
 

Imperial

Member
Code:
//Create Event
global.target_points = 1000;

//Once you get one more point
global.points += 1;
global.target_points += 1;

//Step Event
if(global.target_points > 1000)
{
    global.life += 1;
    global.target_points = 0;
}
 
W

Wintermute()

Guest
I'm assuming this code is being run in the Step event. Consider the logic of your first block. When will the current global.points value equal the current global.points value + 1000? Never. That's why your block never runs.

In the second example, as soon as you get more than 1000 points, the if statement returns 'True' and will fire the block underneath, adding 1 life every Step.

There are many ways of solving this. The simplest is as follows.

Add this to the Create event:

Code:
scoreBenchmark = 1;
Then, in the Step event:

Code:
if (global.points > scoreBenchmark * 1000)
     {
          scoreBenchmark = scoreBenchmark + 1;
          global.life = global.life + 1;
     }
As you can see, the 'scoreBenchmark' variable keeps track of which score thresholds (i.e 1000, 2000, 3000) have 'paid' their additional life to the player, ensuring that they only 'pay' it once!
Hi JTC,

I was looking for an extra life solution every 100k and your code fit the bill, thank you!

That is, up until my game became multi-room, and the score carries over from room to room. Now the code doesn't work in the state above, as when I advance to the next room, the code sees the score as being over 100k, and automatically pays the life again..

Edit:

ok whoops, so the thing I was missing was to put the extra life code inside the step event of the persistant object in my first game room that holds the other 'game-long' variables that i want to flow through from room to room, (life, score) and then it works when entering a new room - no new life paid when score is over threshold.
 
Last edited by a moderator:
  • Like
Reactions: JTC
O

Obj_Neil

Guest
I know this is an old thread, but I wanted to add something so when people search this topic, there is another answer.

An easy way to make it so you get an extra life as score increases. First life at 1000, then at 2000, then 4000, then 8000, etc. Just modify JTC's code slightly.

Code:
scoreBenchmark = 1;
Code:
if (global.points > scoreBenchmark * 1000)
     {
          scoreBenchmark = scoreBenchmark * 2;
          global.life = global.life + 1;
     }
You can change it to multiply by 1.5 instead of 2 if you want the increment to be slower. 1000, 1,500, 2,250, 3,375, 5,062, etc.
 
Top