Page 1 of 1

Variable Scope / Publicity and Privacy

Posted: Fri Sep 12, 2008 6:56 pm
by diki
hi,

i notice the following behavior: when a Variable is defined in OnSpawn of any Model other than ZApplication, i can use this variable in a ZExpression in a different model without any displayed errors - but it is always treated like 0, indifferent of the actual value. if i then try to address it like 'Model.Variable.Value', there is an error saying that the variable cannot be found. if the variable definition is located in ZApplication\OnLoaded, everything works just fine.

what is the policy/scope of variables defined in models other than ZApp.? are they always only visible inside their own model? what would be the smartest way of 'communicating' between models other than 'global' variables?

Posted: Fri Sep 12, 2008 10:15 pm
by Kjell
Hi diki,

The easiest solution for your problem would be to change the SpawnStyle of the two SpawnModel components from Clone into Reference.

However, when you'd insist on using Clones, there are a couple of things that need to be changed. Variables that belong to individual models need to be defined under Definitions instead of OnSpawn. Also, when addressing these variables from the Clone itself using a ZExpression, you need to use the syntax CurrentModel.VariableName instead of the ModelName. And finally it's worth noting that currently there is no easy way to communicate between Clones directly, so you'd probably want to use a global Array / Variable for those situations.

Hope this helps,
K

Posted: Sat Sep 13, 2008 5:02 pm
by VilleK
The scoping rules for variables in ZGE needs to be more clearly defined. I'm working on the scripting-language right now and hopefully this will lead to an improvement in this area.

The best practice right now is to keep variables that you want to be "per instance" (a separate value for each clone) in the Model.Defintions-list. You always address your own variables with the name alone, "LivesLeft" not "PlayerModel.LivesLeft". When you write a script somewhere inside your model and reference a variable that is defined inside Definitions, it will address the value for the current clone. If you address that variable from somewhere else (a script in App, or another model) it will address the variable in the original model only (not clones). So this only works with reference-instances with models you only have a single instance of (such as a PlayerModel in a single player game).