Game Maker Studio: How to Save & Load Coins you Pick Up, Room, & Score?
GMS File: https://rb.gy/kg1fz7
Note: Do not make global objects (Blue Question Marks) Persistent, because this game uses Test Instance Count for each room. This drop in works like this, once you collect all coins, it will jump to the next room. Well... if you set the global objects to Persistent, the last room will come up error, due to the fact, there are no coins in room. So last room will never show up, so don't make global objects Persistent. So the last room will have no global objects, with Persistent not checked. Note: You can mark global objects Persistent, and load and save will work, but do not use Test Instance Count and you should be okay.
scoresave, Depth: -500 do not make it Persistent.
Events:
Game Start
Actions:
ini_open("coin14.ini") ///Can change the name to what you want the save ini file as. If you want to start from beginning, before the save, just change out all the coin14's to like coin15, coin16, so the change happens and you restart the game.
global.score1 = ini_read_real("save","score1",0); /////For the score, you can choose a name, example global.scorecount, global.scorego, just be sure that the other code in the global events match, or the code will not work.
ini_close();
set_score_room, Depth: -500 do not make it Persistent.
Events:
Game Start
Actions:
global.score1=0
o_draw, Depth: -500 do not make it Persistent.
Events:
Draw
Actions:
draw_set_color(c_white)
draw_set_font(font0)
{
draw_text(view_xview[0]+50,view_yview[0]+70, string(global.score1) )
}
/////The above code, draws the score text on screen and places it where you want it in the rooms. Draw_Set_Font is the font you choose as you're design.
obj_coin
Events:
Create
Actions:
ini_open("coin14.ini") ///This code is reading the Save part, when you take a coin, its reading the real about to be saved. So when you come back to room the coins taken will still be destroyed.
var taken = ini_read_real("coins",string(id), -1);
ini_close();
if(taken !=-1) instance_destroy();
Left Pressed
Actions:
ini_open("coin14.ini") ///This code is writing and saving the objects pickup, and save the room & score points.
ini_write_real("coins",string(id), 1);
ini_write_real("save","score1",global.score1);
ini_close();
instance_destroy();