• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

GML Final Score Issue [Solved]

I keep having a problem with the final score on Space Rocks. The final score keeps staying at zero. I don't understand how to get the final score to be something other than zero. Please help.:bunny:
 
Last edited:
It's probably because you are setting score to 0 in inappropriate places. That tutorial should have instructions for how to persist a value across rooms (i.e. global variables and where to/not to set them), and you need to pay better attention to that.
I've rewatched the video a few times before I posted this. I keep comparing the code but, I'm confused on where the inappropriate places would be?
 

FrostyCat

Member
I've rewatched the video a few times before I posted this. I keep comparing the code but, I'm confused on where the inappropriate places would be?
Variables don't reset back to 0 unless you tell them to. Unless you post the exact code you're using, what event it comes from and where you have instances executing that code, nobody can tell you what's wrong.

If you set the score to 0 in the Create event of a persistent controller, an inappropriate place to do so would be in its Room Start event. You will lose the existing score whenever you change rooms for any reason. A persistent controller should only set the score to 0 in its Create event, and you should not place additional copies of the controller in more than one room.

If you set the score to 0 in the Create event of a non-persistent object, placing an instance of it in every room or in the final score room would be in appropriate. That will also lose the existing score when you change into that room. You should note the room where the score first becomes relevant, then create a transition room with an instance of that object in it and room_goto(your noted room); in its Room Creation Code. And instead of going straight into the game from the menu or anywhere else intending to start a new game, go to this transition room instead.

This is about scoping out where the score variable should start existing and where it should not be overwritten. It's not sexy enough a skill for tutorials to try covering it in droves, but there's nothing sexy about getting "variable not set" errors or losing variable values that you need to keep.
 
Variables don't reset back to 0 unless you tell them to. Unless you post the exact code you're using, what event it comes from and where you have instances executing that code, nobody can tell you what's wrong.

If you set the score to 0 in the Create event of a persistent controller, an inappropriate place to do so would be in its Room Start event. You will lose the existing score whenever you change rooms for any reason. A persistent controller should only set the score to 0 in its Create event, and you should not place additional copies of the controller in more than one room.

If you set the score to 0 in the Create event of a non-persistent object, placing an instance of it in every room or in the final score room would be in appropriate. That will also lose the existing score when you change into that room. You should note the room where the score first becomes relevant, then create a transition room with an instance of that object in it and room_goto(your noted room); in its Room Creation Code. And instead of going straight into the game from the menu or anywhere else intending to start a new game, go to this transition room instead.

This is about scoping out where the score variable should start existing and where it should not be overwritten. It's not sexy enough a skill for tutorials to try covering it in droves, but there's nothing sexy about getting "variable not set" errors or losing variable values that you need to keep.
Okay I didn't know that. Ah gotcha. Here is the code. It's inside of a Draw Event on the object obj_game.

switch(room){
case rm_game:
draw_text(20, 20, "SCORE: "+string(score));
draw_text(20, 40, "LIVES: "+string(lives));
break;

case rm_start:
draw_set_halign(fa_center);
var c = c_yellow;
draw_text_transformed_color(
room_width/2, 100, "SPACE ROCKS",
3, 3, 0, c,c,c,c, 1
);
draw_text(
room_width/2, 200,
@"Score 1,000 points to win!
UP: Move
LEFT/RIGHT: Change Direction
SPACE: Shoot
>> PRESS ENTER TO START <<
"
);
draw_set_halign(fa_left);
break;

case rm_win:
draw_set_halign(fa_center);
var c = c_lime;
draw_text_transformed_color(
room_width/2, 200, "YOU WON!",
3, 3, 0, c,c,c,c, 1
);
draw_text(
room_width/2, 300,
"PRESS ENTER TO RESTART"
);
draw_set_halign(fa_left);
break;

case rm_gameover:
draw_set_halign(fa_center);
var c = c_red;
draw_text_transformed_color(
room_width/2, 150, "GAME OVER",
3, 3, 0, c,c,c,c, 1
);
draw_text(
room_width/2, 250,
"FINAL SCORE: "+string(score)
);
draw_text(
room_width/2, 300,
"PRESS ENTER TO RESTART"
);
draw_set_halign(fa_left);
break;
}
 
Look for places where you set score to 0. That's the main point of the issue, the text showing a score of 0 is only a symptom.
So this? It's within the object event of obj_game. The thing is that FriendlyCosmonaut had it at zero.
score = 0;
lives = 3;
draw_set_font(fnt_text);
randomize();
 
Last edited:

FrostyCat

Member
There's no such thing as "object event". Do you mean the Create event?

You should make obj_game persistent, and have only one instance of it in the first room and let it carry over into other rooms. DO NOT put it in rm_gameover. If you do, score = 0; will run on that instance's Create event and erase your score.
 
There's no such thing as "object event". Do you mean the Create event?

You should make obj_game persistent, and have only one instance of it in the first room and let it carry over into other rooms. DO NOT put it in rm_gameover. If you do, score = 0; will run on that instance's Create event and erase your score.
obj_game has been persistent. Oh that could be why.
 
There's no such thing as "object event". Do you mean the Create event?

You should make obj_game persistent, and have only one instance of it in the first room and let it carry over into other rooms. DO NOT put it in rm_gameover. If you do, score = 0; will run on that instance's Create event and erase your score.
Thank you, you're a lifesaver! That worked!
 
Top