Page 1 of 1

Problem with Model-local complex variables - solved

Posted: Thu Sep 01, 2016 8:58 pm
by Rado1
Model.Definitions can contain declarations of model instance-local variables. Everything goes fine when these local variables have simple type - float, int, string, ... The problem is when they are vec* or mat4. Then, they are not treated as local, but as global variables. See this example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[model m = createModel(Model1);
m.Position.X = -5;
m.Variable1 = 1;
m.Variable2.X = 2;

m = createModel(Model1);
m.Position.X = 5;
m.Variable1 = 3;
m.Variable2.X = 4;]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Model1">
      <Definitions>
        <Variable Name="Variable1"/>
        <Variable Name="Variable2" Type="7"/>
      </Definitions>
      <OnRender>
        <RenderText TextFloatRef="Variable1" Y="0.9" Scale="10" UseModelSpace="255"/>
        <RenderText TextFloatRef="Variable2.X" Y="-0.9" Scale="10" UseModelSpace="255"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
It should show:
1 3
2 4
,but shows:
1 3
4 4

Re: Problem with Model-local complex variables

Posted: Fri Sep 02, 2016 7:48 am
by VilleK
This is because these variable types hold references. When they are cloned they will have references to the same instance.
To force a new unique instance, use vector functions.

Try:

m.Variable2=vector2(1,2);

etc.

Re: Problem with Model-local complex variables

Posted: Fri Sep 02, 2016 8:41 am
by Rado1
Thanks Ville for explanation. I did not realized "pointer nature" of var* variables.

BTW now, it necessary for each such a variable to create its own instance (i.e., to call variable2 = vector*() in OnSpawn section) when instantiating a Model. What about to do it automatically by ZGE? Do you think it would be helpful?

Re: Problem with Model-local complex variables

Posted: Fri Sep 02, 2016 11:39 am
by VilleK
It would be helpful if that was done automatically, yes. But so far I did not find a easy way to do this.