Variable Scope / Publicity and Privacy

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:

Variable Scope / Publicity and Privacy

Post 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?
Attachments
VariablePublicity.zgeproj
Dragging the 'Signal' Variable Definition out of Data.OnSpawn into ZApplication.OnLoaded makes it work ...
(983 Bytes) Downloaded 453 times
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
User avatar
VilleK
Site Admin
Posts: 2277
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post 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).
Post Reply