Page 1 of 1

Timer component strange reset behaviour

Posted: Tue Mar 12, 2019 8:20 am
by Ats
Hi,

I'm having a proglem with the Timer component. Is it normal that the Timer.RepeatCount stays the same between AppStates, but in fact, it isn't... ?
I made a quick example to show you my problem. The trace shows that the TimerCube and TimerBlue stays the same, but they are only working once. Aren't they supposed to reset between states so they can continue to work as expected? I think this is really weird...

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0 0 0.3 0" FileVersion="2">
  <OnLoaded>
    <SetAppState State="AppState1"/>
  </OnLoaded>
  <States>
    <AppState Name="AppState1">
      <OnStart>
        <ZExpression>
          <Expression>
<![CDATA[App.ClearColor.R = 0.3;
App.ClearColor.G = 0.0;
App.ClearColor.B = 0.0;
trace(intToStr(TimerCube.RepeatCount));]]>
          </Expression>
        </ZExpression>
      </OnStart>
      <OnUpdate>
        <Timer Name="TimerCube" Interval="1" RepeatCount="2">
          <OnTimer>
            <SpawnModel Model="Model1"/>
          </OnTimer>
        </Timer>
        <Timer Interval="4">
          <OnTimer>
            <SetAppState State="AppState2"/>
          </OnTimer>
        </Timer>
      </OnUpdate>
    </AppState>
    <AppState Name="AppState2">
      <OnStart>
        <ZExpression>
          <Expression>
<![CDATA[App.ClearColor.R = 0.0;
App.ClearColor.G = 0.0;
App.ClearColor.B = 0.3;
trace(intToStr(TimerBlue.RepeatCount));]]>
          </Expression>
        </ZExpression>
      </OnStart>
      <OnUpdate>
        <Timer Name="TimerBlue" Interval="1" RepeatCount="0">
          <OnTimer>
            <SetAppState State="AppState1"/>
          </OnTimer>
        </Timer>
      </OnUpdate>
    </AppState>
  </States>
  <Content>
    <Mesh Name="Mesh1">
      <Producers>
        <MeshBox Scale="0.2 0.2 0.2"/>
      </Producers>
    </Mesh>
    <Model Name="Model1">
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[CurrentModel.Position.X = random(0,5);
CurrentModel.Position.Y = random(0,5);
CurrentModel.Position.Z = random(-5,5);
CurrentModel.Rotation = rnd();]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnUpdate>
        <Timer/>
      </OnUpdate>
      <OnRender>
        <RenderMesh Mesh="Mesh1"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>

Re: Timer component strange reset behaviour

Posted: Wed Mar 13, 2019 2:45 pm
by Kjell
Hi Ats,
Ats wrote: Tue Mar 12, 2019 8:20 amThe trace shows that the TimerCube and TimerBlue stays the same, but they are only working once. Aren't they supposed to reset between states so they can continue to work as expected?
No, AppState is basically a ModelState for the App component. Nothing gets reset automatically when switching to a AppState. Unfortunately, there's no way to actually reset a Timer component .. so the easiest way to go about this is to wrap the Timer in a Model. Below is a example.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SetAppState State="RedAppState"/>
  </OnLoaded>
  <States>
    <AppState Name="RedAppState">
      <OnStart>
        <ZExpression>
          <Expression>
<![CDATA[//

App.ClearColor = vector3(0.5, 0, 0);]]>
          </Expression>
        </ZExpression>
        <SpawnModel Model="Scene"/>
      </OnStart>
      <OnUpdate>
        <Timer Interval="4">
          <OnTimer>
            <SetAppState State="BlueAppState"/>
          </OnTimer>
        </Timer>
      </OnUpdate>
      <OnLeave>
        <RemoveAllModels/>
      </OnLeave>
    </AppState>
    <AppState Name="BlueAppState">
      <OnStart>
        <ZExpression>
          <Expression>
<![CDATA[//

App.ClearColor = vector3(0, 0, 0.5);]]>
          </Expression>
        </ZExpression>
      </OnStart>
      <OnUpdate>
        <Timer Interval="2">
          <OnTimer>
            <SetAppState State="RedAppState"/>
          </OnTimer>
        </Timer>
      </OnUpdate>
    </AppState>
  </States>
  <Content>
    <Model Name="Cube">
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[//

Cube.Position = random(0, 4);
Cube.Rotation = rnd();]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnRender>
        <RenderMesh Mesh="CubeMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="CubeMesh">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
    <Model Name="Scene">
      <OnUpdate>
        <Timer Interval="1" RepeatCount="2">
          <OnTimer>
            <SpawnModel Model="Cube"/>
          </OnTimer>
        </Timer>
      </OnUpdate>
    </Model>
  </Content>
</ZApplication>
K

Re: Timer component strange reset behaviour

Posted: Fri Mar 15, 2019 7:28 am
by Ats
All right, thanks for the explanations :wink:
Maybe this should be explained in the ComponentRef page to avoid new users searching why it's not working as expected:
http://www.zgameeditor.org/index.php/ComponentRef/Timer