Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: Variables lost when saving the game  (Read 5873 times)

0 Members and 1 Guest are viewing this topic.

ylegrand

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 13
  • Keep trying
    • View Profile
Variables lost when saving the game
« 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, 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:
  • I declare a global in base.inc with a value of 0
  • I change it according to some specific triggers in my game
  • When I log it, I can see the correct value
  • I save the game
  • When I log again the same value, it's back to 0, exactly as it it was reseted to the initial value



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 :)
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Variables lost when saving the game
« Reply #1 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).
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

ylegrand

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 13
  • Keep trying
    • View Profile
Re: Variables lost when saving the game
« Reply #2 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 ?
« Last Edit: September 02, 2017, 08:37:38 PM by ylegrand »
Logged

Mot

  • Occasional poster
  • **
  • Karma: 3
  • Offline Offline
  • Posts: 54
    • View Profile
Re: Variables lost when saving the game
« Reply #3 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.
« Last Edit: March 28, 2019, 04:54:35 PM by Mot »
Logged

ylegrand

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 13
  • Keep trying
    • View Profile
Re: Variables lost when saving the game
« Reply #4 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.
Logged
 

Page created in 0.036 seconds with 24 queries.