Variables unaffected by Loading help

Started by geork, Sat 22/01/2011 23:23:45

Previous topic - Next topic

geork

Hey all!
  I've been trying to make some 'constants', meaning Global Variables which remain the same even if you load a previous save. So if you save, then change some 'constant' variables, then reload your save, the 'constant' variable remain changed. I've been trying to use Files for this, but they don't seem to be working: here's the code I tried:
Code: ags

int Test //This is in GlobalVariables


function SaveGame( /*some unimportant variable in here*/){
  File *i = File.Open("Constants.dat",eFileWrite);
  i.WriteInt(Test);
  i.Close();
 //Game saving commands
}
function LoadGame(/*More unimportant variables*/){
  //Game loading commands
  File *i = File.Open("Constants.dat",eFileRead);
  Test = i.ReadInt();
  i.Close();
  Display("%d",Test);
}
 

  I saved the game when the variable "Test" had a value of 0, then I activated a trigger which would set "Test" to 1. I then loaded my save, but my display command showed "Test" to be 0 again!
  I figured that maybe Files are re-written when a game is loaded from a past save, so I changed my SaveGame() function to this:
Code: ags

function SaveGame( /*some unimportant variable in here*/){
  //Game saving commands, notice these now happen BEFORE the file is written!
  File *i = File.Open("Constants.dat",eFileWrite);
  i.WriteInt(Test);
  i.Close();
}

  But I still got the same result. Can anybody help solve this?
    Thanks

Khris

Files are definitely not overwritten when a game is loaded; that doesn't make any sense.
The problem is, you said it yourself: you set the variable to 1 after having saved the game. Thus, you end up writing 0 into the file, not 1.

geork

urrrggghhhh!
  I'm so sorry, you are, of course, completely right and I feel like such a moron for doing that mistake!
   But thanks for pointing it out, Otherwise I'd still be stuck ;)
    Cheers

geork

Okay, taking your advice I've modified the code like this:
Code: ags

function LoadGame(/*Varibles*/){
  File *i = File.Open("Constants.dat",eFileWrite);
  i.WriteInt(Test);
  i.Close();
  RestoreGameSlot(LoadNum);
  File *j = File.Open("Constants.dat",eFileRead);
  Test = j.ReadInt();
  j.Close();

//let the restore game window close, then call: 
  Display("%d",Test);

  Unfortunately, no dice: "Test" still comes up with a 0 after I've loaded a previous save , even though before I did that "Test" was definately 1!
   Am I overlooking something again, or is there another solution?
    Thanks

Khris

This time, the code is the problem:
Like several other functions, RestoreGameSlot() isn't executed immediately:
QuoteNOTE: The game will not be restored immediately; instead, it will be restored when the script function finishes executing.

Thus, the game doesn't get restored until after you've read the file and put the value into Test, overwriting Test with the savegame's value in the process.

Try using on_event / eEventRestoreGame.

geork

Hi
  Yep, works like a charm, albiet a data storing virtual sort of charm...
  Thankyou very much!

SMF spam blocked by CleanTalk