Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: ylegrand on September 02, 2017, 01:41:30 PM

Title: Variables lost when saving the game
Post by: ylegrand on September 02, 2017, 01:41:30 PM
Good afternoon,

I was searching information about the saving system if the current declared/defined variables are also saved.
I found the thread http://forum.dead-code.org/index.php?topic=5976.msg32597;topicseen#msg32597 (http://forum.dead-code.org/index.php?topic=5976.msg32597;topicseen#msg32597), in which it's said that every variable should be saved.

Unless I did something wrong, it's not the case for me. let me give you some context:

(http://www.ylegrand.com/externe/wme_2.png)

To be sure, I also tried to set it in base.inc with another value than 0: it confirms my doubts: when I save the game, it come back to the initial value (and not always 0).

By the way, I have the same behaviour when I go to another scene and when I come back.

Is it an option somewhere to enable to save my globals/statics?
Or did I misunderstood anything?

Thanks in advance for your help, and have a nice weekend :)
Title: Re: Variables lost when saving the game
Post by: Mnemonic on September 02, 2017, 01:46:57 PM
Do NOT set the variable value in base.inc. That way the value will be reset everytime a new script (which includes base.inc) is executed.
Set default variable values in some script that only gets executed once (game.script, typically).
Title: Re: Variables lost when saving the game
Post by: ylegrand on September 02, 2017, 02:00:22 PM
Hi,

Thanks a lot for your quick answer. I did first a small test it by placing a global definition in the game.script. I get the folloing log error :

Code: [Select]
15:03:58:  Compiling script 'actors\Migounette\Migounette.script'...
15:03:58:    Error@line 16: Variable 'Statevelo' is referenced but not defined

Does it mean that I need to control the existence of my globales eveywhere I use them in my scripts ?
Title: Re: Variables lost when saving the game
Post by: Mot on September 03, 2017, 12:12:12 AM
Salut ylegrand

I think you misunderstood what Mnemonic wrote.

You can keep the definition of your custom global variable within base.inc:

Code: WME Script
  1. #include "scripts\const.inc"
  2.  
  3. global Scene;
  4. global Keyboard;
  5. global actor;
  6.  
  7. global MyCustomVariable;

Then in a script that is executed only once (such as game.script), you give it the initial value:

Code: WME Script
  1. #include "scripts\base.inc"
  2.  
  3. //setting default values
  4.  
  5. MyCustomVariable=0;

Why should you assign its initial value in a script that is executed only once? Because you don't want to reset its value multiple times (unless that's what you intend), as it would be the case if you initialized it in a frequently called script or in base.inc (that is included by default in many scripts).

Quote
Does it mean that I need to control the existence of my globales eveywhere I use them in my scripts ?

If you define your global variable within base.inc, as you can see above, as long as you include in your scripts this line (which by default comes along in scripts created from a template): #include "scripts\base.inc" you can just go ahead and use your custom global variable.

Code: WME Script
  1. #include "scripts\base.inc"
  2.  
  3. ...
  4.  
  5. MyCustomVariable = somefunction ();
  6. MyCustomVariable = MyCustomVariable + 3;
  7.  
  8. //The initial value is no longer there, now my custom global variable holds the last value I assigned it

Notice the difference between defining a global variable, initializing a global variable and using such global variable.
Title: Re: Variables lost when saving the game
Post by: ylegrand on September 03, 2017, 08:07:49 PM
Hi Mot,

 That makes perfectly sense. Without seeking excuses, I come from the PHP which can be much more lax in terms of declaration/definition of variables. So it didn't jump out at me.

Thank you very much for taking the time to re-explain me the basics. Highly apprecieted.

I wish you both a good evening.