How to do a highscore?

Discussion in 'Programming' started by #Frogglets ;-;, Sep 14, 2018 at 1:25 PM.

  1. #Frogglets ;-;

    #Frogglets ;-; Member

    Joined:
    Jul 29, 2018
    Posts:
    15
    Hi,
    for my game I want one highscore to appear which is your personal best rather than a whole table. My code was:

    with (obj_player)
    if obj_player.hp <= 0
    {
    global.finalscore += global.thescore;
    }

    and:

    if global.finalscore >= global.highscore
    then{
    global.highscore = global.finalscore;
    draw_text(100,100,string(global.highscore));
    }

    draw_text(100,100,string(global.highscore));

    whenever I try to do this tho my highscore equals out to 0 everytime and I dont know how to resolve it. Any help would be greatly appreciated!!!
    Thanks, A_Froggo
     
  2. The Shatner

    The Shatner Member

    Joined:
    Jun 25, 2016
    Posts:
    93
    Hey there!
    I would suggest debugging it. Something might be happening with the global.finalscore or the global.thescore vars.
    Try printing them onscreen at all times during the game and see if they are above 0.
    For example:
    In the obj_player DRAW EVENT:
    Code:
    draw_self();
    draw_text(100,100,"finalscore is "+string(global.finalscore));
    draw_text(100,150,"thescore is "+string(global.thescore));
    
    Also, avoid doing calculations on the Draw event. Move them to the Step Event and just show the results on the Draw Event. Too many calculations inside the Draw Event may slow down the game and cause other issues.
     
  3. #Frogglets ;-;

    #Frogglets ;-; Member

    Joined:
    Jul 29, 2018
    Posts:
    15
    h
    hi The Shatner!!
    so, whenever I go to a new room my score is set to 0 and my finalscore is either not getting updated or getting set back to 0.
    Any idea how to resolve it??
     
  4. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    249
    You most likely have global.finalscore = 0 in some object's create event. If that object is not persistent then you are also most likely creating it again when you change rooms which will run the create event again setting it to 0 each time. So the options are to make the object that initializes global.finalscore persistent or have a persistent controller object initialize it or have an if statement check if it = undefined
     
  5. #Frogglets ;-;

    #Frogglets ;-; Member

    Joined:
    Jul 29, 2018
    Posts:
    15
    HI!!!
    I got it to work by having a room at the start of the game with all my persistant vars in it!
    Thanks for the help though! I really do appreciate it!
     
    Bayesian likes this.
  6. The Shatner

    The Shatner Member

    Joined:
    Jun 25, 2016
    Posts:
    93
    Good to know you're on the right path, Frogglets!
    Another suggestion I would give you is to save/fetch the data from an INI file. For example:
    Code:
    finalscore = global.finalscore;
    ini_open("data.ini"); //this can be any filename, such as "custom.ini" or "imissthegoodolddays.ini", it will be created in case it doesn't exist
    comparerscore = ini_read_real("hiscore","hiscore",0); //this tells GMS to read what's in the "hiscore/hiscore" part of your INI file. In case it's not existant, it will return "0"
    if (finalscore > comparerscore) { //that is, if your current finalscore is higher than the one previously saved
    ini_write_real("hiscore","hiscore",real(finalscore); //writes the new record to the INI file
    }
    ini_close(); //always close your INI to avoid memory leaks
    
    This way, you can close your game and the highscore will always be there.

    For more info on the INI saving system, check https://docs.yoyogames.com/source/dadiospice/002_reference/file handling/ini files/index.html (GMS 1.x) or https://docs2.yoyogames.com/source/..._reference/file handling/ini files/index.html (GMS 2.x).

    I must remind you that the INI can be easily edited on many platforms, so for advanced usage you'll have to implement an encryption method to avoid user "tampering". However, that is usually only necessary on commercial projects and the like. And encryption is quite a difficult thingy to implement on GMS.
     

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