Legacy GM Adding hearts/ lifes in a game

A

Ann

Guest
Inside of obj.player, step event:

if global.current_score>49
{
global.hearts+=1;
global.current_score=0
}

EXPLANATION
This says that if my current score (coins) happens to be more than 49, then add 1 more life. The score will be reset to 0. And you will have to pick up 50 coins again to add another heart.

THIS IS THE PROBLEM:
If I don't reset the score to zero the code will add 1 heart every frame. That just looks weird and its not gonna work. I only want to add 1 heart ONCE without resetting the score to 0. What piece of code can I add here to make it add to the global.hearts once, and not once every frame.

Hope this explains enough. Do ask if you have any questions :) Btw, all of the variables are placed in a seperate obj. I placed them in an obj called obj_control
 
Last edited by a moderator:
I

Insanebrio

Guest
Just add a second score variable (like global.total_score). When you increase a score, increase both variables and show in the hud the total score that wont be reduced once you reach 50 points.
 

Tornado

Member
You could use modulo operator:
Code:
if (global.current_score mod 50 == 0)
{
    global.hearts+=1;
}
That way you can increase hearts every 50 score points without having to reset it.
But be aware this will only work if the score is always incremented by 1 and this check is performed after each score increment.
 

rIKmAN

Member
This says that if my current score (coins) happens to be 50, then add 1 more life. The score will be reset to 0. And you will have to pick up 50 coins again to add another heart.
Just a small point, but your code doesn't say that - your code says "if global.current_score > 50" which means that the score will have to be at least 51 before the 'if' block is true.
 
A

Ann

Guest
Just a small point, but your code doesn't say that - your code says "if global.current_score > 50" which means that the score will have to be at least 51 before the 'if' block is true.
Yes, you're right. I know. The code doesn't get activated until I have collected 50 coins. What exactly did you have in mind? :)
 
A

Ann

Guest
You could use modulo operator:
Code:
if (global.current_score mod 50 == 0)
{
    global.hearts+=1;
}
That way you can increase hearts every 50 score points without having to reset it.
But be aware this will only work if the score is always incremented by 1 and this check is performed after each score increment.
Thanks! This is pretty useful. But I have a question. Some of the coins I can pick up are more worth than others. Some coins are worth two points, while others are worth only 1. Does this mean that this piece of code will not work?
 
A

Ann

Guest
Just add a second score variable (like global.total_score). When you increase a score, increase both variables and show in the hud the total score that wont be reduced once you reach 50 points.
Ah. I see. That could work as well. Would I have to only create a new variable? Or would I have to make new codes and strings as well?
This new variable goes into the obj_control, where all of my variables are. After that, what do I do? I cant remember.
 

rIKmAN

Member
The code doesn't get activated until I have collected 50 coins.
No, that piece of code inside the 'if' block won't get activated until you have 51 coins, because you are saying 'if total_score is bigger than 50 - if (global.total_score > 50) - and 50 isn't bigger than 50.

Some coins are worth two points, while others are worth only 1. Does this mean that this piece of code will not work?
Mod will work with any value for the coins as it is using mod 50 on the total_score variable, but as FrostyCat says below there are situations where it won't work if you have multiple value coins. (Good catch Frosty)

If you are just starting out, easiest way is to use a second variable that mirrors total_score, and then check and reset this one to update your hearts, leaving total_score unchanged to be used for displaying. It does the same thing Frosty explains below in a slightly different way.

Also, you should edit your post to add new information rather than replying with new posts 3 times in a row and bumping your thread.
 
Last edited:

FrostyCat

Redemption Seeker
Thanks! This is pretty useful. But I have a question. Some of the coins I can pick up are more worth than others. Some coins are worth two points, while others are worth only 1. Does this mean that this piece of code will not work?
Of course it'll stop working, see for yourself what happens if your score is 49 and you pick up 2 points.

What you should do instead in this case is using a moving target.

Create:
Code:
global.next_trigger = 50;
On increment:
Code:
while (global.current_score >= global.next_trigger) {
  global.hearts += 1;
  global.next_trigger += 50;
}
This is a basic flag pattern that all novices should learn, the other two being before-and-after comparisons and continuous-to-one-time flags.
 
Last edited:

Tornado

Member
You can also use this simple formula:
Code:
global.hearts=floor(global.current_score/global.interval)
Where global.interval will be set to 50 in your case.
 
Top