• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need help with a variable

N

NeveZz

Guest
So I have a variable (global.coins) and I want it to be the same value as before i restarted the game. I have a initialization room that sets the global.coins value to 0. How to fix this?
 

Alexx

Member
A quick and easy method would be to use an INI file to save value at game end and load at game start.
 
N

NeveZz

Guest
Take a look at the manual pages about File Handling.
I've never been more confused in my life...
Code:
///When I initialize the game

 ini_open( 'savedata.ini' );
 global.coins = ini_read_real( 'save1', Coins, 0 );
 ini_close();
Code:
///When bullet hits enemy i save game...

 ini_open( 'savedata.ini' );
 global.coins = ini_read_real( 'save1', Coins, 0 );
 ini_close();
Giving this:
############################################################################################
FATAL ERROR in
action number 1
of PreCreate Event
for object obj_tl_load:
Variable obj_tl_enemy1.Coins(100004, -2147483648) not set before reading it.
at gml_Script_scr_coins (line 2) - global.coins = ini_read_real( 'save1', Coins, 0 );
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_coins (line 2)
called from - gml_Room_rm_loader_Create (line 1) - script_execute(scr_coins);
 
N

NeveZz

Guest
ok managed to get the game running, but the coins are not saving.
Code:
///Receive Coins

  global.coins += 10;
  ini_open("savedata.ini");
  ini_write_real("save1", "Coins", global.coins);
  ini_close();
Code:
///When Game is Opened

globalvar coins;
 ini_open('savedata.ini');
 global.coins = ini_read_real('save1','Coins',0);
 ini_close();
 

Rob

Member
ok managed to get the game running, but the coins are not saving.
Code:
///Receive Coins

  global.coins += 10;
  ini_open("savedata.ini");
  ini_write_real("save1", "Coins", global.coins);
  ini_close();
Code:
///When Game is Opened

globalvar coins;
 ini_open('savedata.ini');
 global.coins = ini_read_real('save1','Coins',0);
 ini_close();
Are you sure the save code is running? Is global.coins being overwritten AFTER it loads from the .ini?
Try putting a "show_message("ini loaded, global.coims: " + string(global.coins)); with the load code
 
Top