Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: anarchist on February 26, 2011, 07:08:16 PM

Title: Custom actor variables
Post by: anarchist on February 26, 2011, 07:08:16 PM
I have my actors and want to set some custom variables, which I can access using actor_name.customVariable. Those variables will be set in the beginning of each chapter.

First question is how to set such variables? If I set them in the actor_name.script should they be global or simple var? Second question is when to initialize them? I know if I initialize them for instance in the actor_name.script, they will be reset every time I Game.LoadActor3d() to load the actor. Is this true?
Title: Re: Custom actor variables
Post by: Spellbreaker on February 26, 2011, 09:29:43 PM
- deleted misinformation - :)  ::rock ::rock ::rock
Title: Re: Custom actor variables
Post by: metamorphium on February 26, 2011, 11:45:37 PM
nonsense,

Code: WME Script
  1.  
  2. actor.AnyVariable = "test";
  3. Game.Msg(actor.AnyVariable);
  4.  
  5.  

simple as that.
Title: Re: Custom actor variables
Post by: Mnemonic on February 27, 2011, 12:12:56 PM
And if you're setting them directly in the actor's script, you can use

Code: WME Script
  1. this.AnyVariable = "test";
  2.  

As for Game.LoadActor(), it creates an entirely new actor object, so no, the custom properties are not retained.
Title: Re: Custom actor variables
Post by: anarchist on February 27, 2011, 04:07:29 PM
Thanks a lot guys for your quick answers.

So, if I define them in the actor's script like this:

Code: WME Script
  1. this.AnyVariable = "test";
  2.  

will the variable be reset every time I call Game.LoadActor()? I am a bit confused as to what parts of scripts are executed when. In the WME documentation (help and metamorphium's book) it is said that whenever you include a script, the variables that are set as

Code: WME Script
  1. var variable = value

inside that script (outside functions and event handlers) are reset to value with each include. So, if I have such code in lets say base.inc it will not work. If I have it inside game.script or game_loop.script, will they be reset every time the game is run, even if the user loads a saved game?

Thanks again.
Title: Re: Custom actor variables
Post by: Mnemonic on February 28, 2011, 12:05:26 PM
Scripts in WME are always owned by some game object. So when the object is instantiated, its scripts are started, and when the object is deleted, its scripts are terminated. Using Game.LoadActor() creates a new actor object (which in turn starts its scripts). When you delete the actor, its scripts are deleted as well.

Local variables (declared with "var") are part of the running script. When the script is terminated, its local variables cease to exist.
Global variables (declared with "global") exist all the time, they are not dependent on any scripts (the scripts only use existing global variables or create new ones).
Custom object properties (set with this.SomePropery or actor.SomeProperty) are owned by their parent object. When the object is deleted, so are its custom properties.
Title: Re: Custom actor variables
Post by: anarchist on March 01, 2011, 03:36:34 PM
What about includes? In the WME Book, in chapter 2.5 it says:

Quote
Important:
Never initialize your variables in the include files. They will get reinitialized with every inclusion of your script.

So if I have a script.inc file which contains:

Code: WME Script
  1. var variable = 10;
  2.  

and include this script in two scripts attached to 2 entities in a scene. Does this mean that the code will be executed twice, one for each include, each time the scene is loaded?
Title: Re: Custom actor variables
Post by: Mnemonic on March 01, 2011, 05:30:09 PM
Does this mean that the code will be executed twice, one for each include, each time the scene is loaded?

Yes. Include files work just as an automated copy&paste feature. The content of the include file is literally inserted into the script.

The reason why meta warns about this is that some people in good faith put something like:

Code: [Select]
global myWindow = Game.LoadWindow("some.window");

to the base.inc include file. Now, since base.inc is by default inserted to every single script, their game kept loading a new window every time some script was executed, resulting in huge memory leak and extremely long loading/saving times.
Title: Re: Custom actor variables
Post by: anarchist on March 02, 2011, 12:24:20 PM
Thank you it is quite clear now.