1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Need help saving global variables.

Discussion in 'Programming' started by Cdiddysir, May 15, 2019 at 1:57 AM.

  1. Cdiddysir

    Cdiddysir Member

    Joined:
    Feb 17, 2019
    Posts:
    43
    Title says it all. I just need help saving a global variable between games. So whenever the player loads the game, the amount of how much the global variable has remains intact.

    So the game starts out with the player having 0 "global.tokens" but when you pick one up it adds to the total token count. I want that total token count to be saved between games, how would I go about doing this?
     
  2. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    498
    Something like this should work.


    To save:
    Code:
    if file_exists("save.ini"){
     ini_open("save.ini");
          ini_write_real("Save", "tokens", global.tokens);
     ini_close();
    }
    
    To load:
    Code:
    if file_exists("save.ini"){
     ini_open("save.ini");
       global.tokens =  ini_read_real("Save", "tokens", 0);
     ini_close();
    }
    

    Edit:
    You may need to include the file for the above to work, or else omit the if file_exists() check. I'm not sure if Gamemaker creates a new file if you try to open a non existing one.
     
    Cdiddysir likes this.
  3. Cdiddysir

    Cdiddysir Member

    Joined:
    Feb 17, 2019
    Posts:
    43
    Where exactly would I put all of that code? I have the global tokens set in the ob_game object, and if the player interacts with the ob_token, then their global.token count increases by 1
     
  4. Gzebra

    Gzebra Member

    Joined:
    Aug 3, 2017
    Posts:
    63
    When the player exit the game.
     
  5. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    498
    If the data is important you could save it any time it is changed if you wanted to, so you would put the to save code block right after changing the variable.

    Right after you change the global.tokens variable you would run this:
    Code:
    if file_exists("save.ini"){
    ini_open("save.ini");
          ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
    ini_close();
    }
    
     
    Cdiddysir likes this.
  6. Cdiddysir

    Cdiddysir Member

    Joined:
    Feb 17, 2019
    Posts:
    43
    Cool, and then I would load it in the ob_game object right?
     
  7. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    498
    Loading once on start sounds like it would be best, so you could put it in the very first object of your game.
     
    Cdiddysir likes this.
  8. Cdiddysir

    Cdiddysir Member

    Joined:
    Feb 17, 2019
    Posts:
    43
    Hmm it didn't work. Can I show you my code?
     
  9. Cdiddysir

    Cdiddysir Member

    Joined:
    Feb 17, 2019
    Posts:
    43
    Code:
    //create event for ob_game_menu (The first object in the game, which leads to the main menu upon startup)
    
    if file_exists("save.ini"){
     ini_open("save.ini");
       global.tokens =  ini_read_real("Save", "tokens", 0);
     ini_close();
    }


    Code:
    //create event for ob_game
    
    global.tokens = 0
    

    Code:
    ///player collision event with ob_token
    
    global.tokens++;
    
    instance_destroy(other);
    audio_play_sound(a_token,10,false)
    
    
    if file_exists("save.ini"){
    ini_open("save.ini");
          ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
    ini_close();
    }
     
  10. pipebkOT

    pipebkOT Member

    Joined:
    Dec 13, 2018
    Posts:
    72
    To load the game
    Code:
    //create event for ob_game_menu (The first object in the game, which leads to the main menu upon startup)
    
    if file_exists("save.ini"){
     ini_open("save.ini");
       global.tokens =  ini_read_real("Save", "tokens", 0);
     ini_close();
    }
    
    else
    {
    //Inits all variables
    
    global.tokens=0
    }

    DELETE THIS:
    because you are overwriting the variable back to zero:rolleyes: and you don't want that!



    To save the game:

    Code:
    ini_open("save.ini");
          ini_write_real("Save", "tokens", global.tokens); // the 3rd argument is what is being saved, which is the current value of global.tokens
    ini_close();
    }
     
    Last edited: May 15, 2019 at 7:06 AM

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