Legacy GM Increment score one by one

Dagoba

Member
Hello!

I want to have an effect that when you score, your scoreboard won't just jump from like 150 to 200, but it will increment score one by one from 150 to 200 (score++;)

I tried this with the following method:
Code:
for (i = 0; i < pending_score; i++) {
    global.points++;
}
But it will count it so fast, that it looks like it just adds the 50 score immidiately, and not adding up.

And for obj_controller, I am just drawing the score like draw_text(room_width / 2, 0, "Score: " + global.points);

So how can I achieve this kind of effect?
There could be used and alarm's, but the counting is inside of a script.
Thanks in advance!
 
V

Vyking

Guest
Hmmm. Try an alarm that calls itself until the score is at the desired number.
If that doesn't work, it's beyond me.
 

2Dcube

Member
You could use 2 variables.
Create event:
Code:
displayScore = 0;
actualScore = 0;
displayScore is what you show the player.
actualScore is what the score really is, and you increase displayScore every step as long as it is lower than the actual score (see code below for Step event).

Step event:
Code:
if displayScore < actualScore
  displayScore++;
Draw event:
Code:
draw_text (100, 100, string(displayScore));
 

jaydee

Member
A for loop doesn't run asynchronously. IE, it doesn't execute over time. All iterations are performed immediately in the context of the code. So the code you've written is functionally exactly the same as: global.points += pending_score.

The only way you can get this to work is to execute the "loop" over time. Which means frequent calls to iterate over time.

You could just do this before you draw the score.
Code:
if(pending_score > 0)
{
  global.points++;
  pending_score--;
}
 

JackTurbo

Member
Jaydee's approach should work.

Although I'd do it slightly differently. There is something that doesn't sit right with me about adding the score to a pending variable and adding that over time to the global score. It just feels a backwards.

The way I would approach it would be to add the full value to global.points and have a pointsToDraw or similar variable that is the value to draw to screen and compare the two.

Then in the step event do something like.
Code:
if (pointsToDraw > global.points){
    pointsToDraw ++;
}
if(pointsToDraw < global.points){
    pointsToDraw --;
}
 

2Dcube

Member
The problem is that when large scores get added every few seconds, the counter won't be able to keep up. In that case the player has no idea what the actual score is, it just keeps counting up forever.

A solution is to increase pointsToDraw more if the difference with global.points is bigger.
Step event:
Code:
pointsToDraw = lerp(pointsToDraw, global.points, 0.1);
Draw event:
Code:
draw_text(room_width / 2, 0, "Score: " + string(ceil(pointsToDraw)));
 
Top