Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: geork on Sat 22/01/2011 23:23:45

Title: Variables unaffected by Loading help
Post by: geork on Sat 22/01/2011 23:23:45
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:

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:

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
Title: Re: Variables unaffected by Loading help
Post by: Khris on Sun 23/01/2011 00:41:55
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.
Title: Re: Variables unaffected by Loading help
Post by: geork on Sun 23/01/2011 00:59:21
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
Title: Re: Variables unaffected by Loading help
Post by: geork on Sun 23/01/2011 20:06:40
Okay, taking your advice I've modified the code like this:

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
Title: Re: Variables unaffected by Loading help
Post by: Khris on Sun 23/01/2011 20:54:16
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.
Title: Re: Variables unaffected by Loading help (SOLVED)
Post by: geork on Mon 24/01/2011 17:52:05
Hi
  Yep, works like a charm, albiet a data storing virtual sort of charm...
  Thankyou very much!