GML Final Score Issue [Solved]

Discussion in 'Programming' started by Floppy Ears Interactive, Oct 7, 2019.

  1. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    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: Oct 7, 2019
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,556
    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.
     
  3. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    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?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,556
    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.
     
  5. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    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;
    }
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,556
    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.
     
  7. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    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: Oct 8, 2019
  8. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,556
    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.
     
  9. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    obj_game has been persistent. Oh that could be why.
     
  10. Floppy Ears Interactive

    Floppy Ears Interactive Member

    Joined:
    Sep 7, 2019
    Posts:
    6
    Thank you, you're a lifesaver! That worked!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice