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

Asking for help for lives...

N

Neocraftz1553

Guest
Hello community. I am making my first shoot em' up on game maker(A gradius fangame) and I need some help with a certain mechanic that's been bugging me. So, I have a ship shoot projectiles at enemies, and when the enemy is hit, the projectile gets destroyed and so does the enemy, and a line of simple code gives the player points:

hud_game.points+=25;

The object that has the name hud_game draws lives and scores.

I'm trying to make it so that when the player reaches a certain amount of points, the player gets rewarded with lives, such as every 1000 points. I tried doing:

if (hud_game.points > 1000)
{
global.pLives += 1;
}

But that just spams the player with lives, because as long as the points are 1001 or over, it'll keep rewarding the player lives, letting them basically kamikaze through enemies without worry.

Is there a way so that once the player reaches or passes a certain number of points, it'll reward the player once?
 

RangerX

Member
Count the score and have another variable to know when you should give a life:

When you give score...

score+=1;
count+=1;

Then in some step event...

if(count>=1000)
then
{
lives+=1;
count=0;
}


See what I did there?
 
N

Neocraftz1553

Guest
Count the score and have another variable to know when you should give a life:

When you give score...

score+=1;
count+=1;

Then in some step event...

if(count>=1000)
then
{
lives+=1;
count=0;
}


See what I did there?
Ah, I see, I can't believe I didn't think of making another variable. What I did originally, was to reset the score. Thank you!
Gameplay1.png
 

Attachments

Yal

šŸ§ *penguin noises*
GMC Elder
I usually have a 'next life' threshold variable that I increase every time the player gets a life, and you'd check whether score > next_extralife. This approach means you don't need to change two variables every time you get score, and it's easier to replicate a thing common in shmups... extra lives at different intervals. Gradius III is one of my favorite games, and I clearly remember how it gives the first extra life and 20000 points and then every 50000 points :p If you just keep track on the number of points left to the next life, you don't know whether that amount originally was 20000 or 50000, so you basically lose information.

Anyway, whatever works works, I guess, so pick whichever you prefer :3
 

Roderick

Member
I usually have a 'next life' threshold variable that I increase every time the player gets a life, and you'd check whether score > next_extralife. This approach means you don't need to change two variables every time you get score, and it's easier to replicate a thing common in shmups... extra lives at different intervals. Gradius III is one of my favorite games, and I clearly remember how it gives the first extra life and 20000 points and then every 50000 points :p If you just keep track on the number of points left to the next life, you don't know whether that amount originally was 20000 or 50000, so you basically lose information.

Anyway, whatever works works, I guess, so pick whichever you prefer :3
To do that, in your initialize code, set next_life to 20000, then add the following in your checks:

if (score >= next_life) {lives += 1; next_life += 50000}

If I'm misunderstanding, and it's supposed to be 20k, 50k, 100k, 150k, etc, you'd use

if (score >= next_life) {lives += 1; if (next_life == 20000) {next_life = 50000;} else {next_life += 50000;} }

I really wouldn't recommend adding points one at a time and checking against each one. That's adding 200 extra calculations just to add 100 points, 2000 calculations for 1000 points, etc. My method uses 5 calculations, regardless of the number of points added, and only needs to be called once per step, not every time points are added.
 
N

Neocraftz1553

Guest
I usually have a 'next life' threshold variable that I increase every time the player gets a life, and you'd check whether score > next_extralife. This approach means you don't need to change two variables every time you get score, and it's easier to replicate a thing common in shmups... extra lives at different intervals. Gradius III is one of my favorite games, and I clearly remember how it gives the first extra life and 20000 points and then every 50000 points :p If you just keep track on the number of points left to the next life, you don't know whether that amount originally was 20000 or 50000, so you basically lose information.

Anyway, whatever works works, I guess, so pick whichever you prefer :3
Ah, I see what you did, thanks. I was trying to do that earlier. I also didn't know you could do
(example > something)
Thanks much!
 
Last edited by a moderator:
N

Neocraftz1553

Guest
To do that, in your initialize code, set next_life to 20000, then add the following in your checks:

if (score >= next_life) {lives += 1; next_life += 50000}

If I'm misunderstanding, and it's supposed to be 20k, 50k, 100k, 150k, etc, you'd use

if (score >= next_life) {lives += 1; if (next_life == 20000) {next_life = 50000;} else {next_life += 50000;} }

I really wouldn't recommend adding points one at a time and checking against each one. That's adding 200 extra calculations just to add 100 points, 2000 calculations for 1000 points, etc. My method uses 5 calculations, regardless of the number of points added, and only needs to be called once per step, not every time points are added.
You got it right the first time, but thanks for the other piece of code, it'll come in handy!
 

Roderick

Member
@Neocraftz1553 One other thing that might be useful: If you want to cap the number of lives, you can do so easily with a simple modification to the code I provided. Just change the part that says

lives += 1;

to

lives = min(5, lives+1);

That makes it so that it sets the lives variable to 5 (the cap in this example) or the current value of lives, plus 1, whichever is smaller.

So if you have 3 lives, it adds one to get 4, sees that it's less than 5, and sets lives to 4.

Then you get another and it adds one (to 5), compares it to 5, sees that they're the same, and sets lives to 5.

Each time after that, it adds one, gets 6, sees that 5 is now smaller than lives + 1, and leaves it at 5.

You could do the same thing by adding an if statement after increasing lives, or just running a min() statement after, but it's good practice to use the minimum number of calculations you can. Adding one extra isn't going to hurt your performance, but the more often you have "just one extra call", the quicker you see your game slow down, or possibly even form memory leaks.
 
Top