GMS 2.3+ Code that detects the first time game runs


GM Version: Studio
Target Platform: Windows
Download: code below
Links: n/a

Code that detects the first time the game is run.
Certain games don't like players to go back in their path. You can see in games like the Amnesia Series where it's all about autosaves and not having much controll over the saves.
This will help you do that sort of thing or you can adapt to it and do what suits you best.

To get this to work you need 3 things.
  1. A .ini file;
  2. Object to check the ini file at the start of the game;
  3. Object to change the firstrun value in the .ini file.
First you create a .txt file like this:


firstrun = 1

;We'll use 1 for true and 0 for false
;Remember 1 = true; 0 = false
Change the extension of the ".txt" file to ".ini".
I've named mine "firstrun.ini"

A .ini file is uselfull for holding a specific value of data and it will hold that value until it's overwritten or deleted, unlike game variables which reset everytime you start the game.
We need the firstrun to be a fixed value and to not reset.

So now we are going to create an object. I called mine "obj_firstrunchecker". That object will check the value of the .ini file.

//obj_firstrunchecker - > Create Event

globalvar firstrun;    //It's important that this variable is global

ini_open("firstrun.ini"); //Always open the ini file
firstrun = ini_read_real("player","firstrun",1); //The 1 means that if there's no value or no firstrun.ini file, the function will return 1
ini_close(); //Always close the ini file

if (firstrun == 0) {

    //Commands go here

    //These are the commands I used
    //I wanted to do an autosave/autoload feature and this is how I did it


We are not done yet. We need another object to change the value of "firstrun" in our .ini file.

//obj_firstrunchanger -> Create Event

firstrun = 0; //set the value of the global variable firstrun


ini_write_real("player", "firstrun", firstrun); //Change the value of the .ini file with the value of the global variable

Wait! You have those objects but what to do now?

This part takes no code, only logic:

  1. The ini file will go into the included files of your project;
  2. The obj_firstrunchecker should run when the game starts, in my case, so it can auto load;
    1. You can put it also in a place where it matters to check if there was a previous run (eg. a change that only should occur if the player gets past a specific level).

Code not working?
Here's what to
NOT do:

//declaring variables after they're used
//opening ini files after the changes

firstrun = ini_read_real("player","firstrun",1);

if (firstrun == 0) {

    //commands go here


globalvar firstrun;

Where you place your code matters every single time. Even if you have this object to create all of your game variables:

globalvar a;
globalvar b;
globalvar c;
globalvar d;
If this object doesn't run when the game starts and the "a" var is used at game start, every functions that have or need the "a" var will NOT work.

Hope this helps! ;)
Last edited:


In fact you can detect in same way that game was closed in normal way, or it crashed.

You just need to have "persistent" object with game start and game end event, and save in ini that game started (eg.: started=1), then started=0 on game End. If game starts, and started==1, that means, it crashed or was killed from task manager or PC was turned off by hard reset/loss of power.