Timer.CurrentRelativeTime behavior

Found a bug? Post information about it here so we can fix it!

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 764
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Timer.CurrentRelativeTime behavior

Post by Ats »

Maybe it's not a bug and was designed like that, but I had a hard time understanding why I had some problems from time to time in my game.

Timer.CurrentRelativeTime doesn't reset to zero when the countdown hasn't been finished!
One need to manually set it to 0 before use for no bad surprises afterwards.

I made an example to illustrate this :

There are two AppState. Both have Timer that switch from one AppState to another every 5 seconds.
You can skip that timer by pressing left or right keys. The AppState backgrounds have two different colors that vary with their own Timer, so it's easy to see what's going on. You'll see that the Timer.CurrentRelativeTime continues where it was.

Is it a bug? Or is it normal? In this case, it should be explained in the help doc :wink:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="1 0 0 0" FileVersion="2">
  <OnLoaded>
    <SetAppState State="AppState1"/>
  </OnLoaded>
  <States>
    <AppState Name="AppState1">
      <OnUpdate>
        <Timer Name="Timer1" Comment="5 seconds" Interval="5">
          <OnTimer>
            <SetAppState State="AppState2"/>
          </OnTimer>
        </Timer>
        <KeyPress Keys="&lt;" CharCode="32">
          <OnPressed>
            <SetAppState State="AppState2"/>
          </OnPressed>
        </KeyPress>
        <ZExpression Expression="App.ClearColor = vector3(1 - Timer1.CurrentRelativeTime / 5,0,0);"/>
      </OnUpdate>
    </AppState>
    <AppState Name="AppState2">
      <OnUpdate>
        <Timer Name="Timer2" Comment="5 seconds" Interval="5">
          <OnTimer>
            <SetAppState State="AppState1"/>
          </OnTimer>
        </Timer>
        <KeyPress Keys="&gt;" CharCode="32">
          <OnPressed>
            <SetAppState State="AppState1"/>
          </OnPressed>
        </KeyPress>
        <ZExpression Expression="App.ClearColor = vector3(0, 1 - Timer2.CurrentRelativeTime / 5,0);"/>
      </OnUpdate>
    </AppState>
  </States>
</ZApplication>
User avatar
Kjell
Posts: 1915
Joined: Sat Feb 23, 2008 11:15 pm

Re: Timer.CurrentRelativeTime behavior

Post by Kjell »

Hi Ats,
Ats wrote: Tue Feb 08, 2022 5:02 pmIs it a bug? Or is it normal?
It's not a bug. A timer will only run as long as the node that contains it is active. Here's a similar example to yours except that you have to hold the "S" key on your keyboard to keep the timer active. Hopefully that will clear things up for you.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0 0 1 0" FileVersion="2">
  <OnUpdate>
    <KeyPress Comment="S" Keys="S">
      <OnPressed>
        <Timer Name="ColorTimer" Interval="5">
          <OnTimer>
            <ZExpression>
              <Expression>
<![CDATA[//

if(App.ClearColor.R)
{
  App.ClearColor.R = 0;
  App.ClearColor.B = 1;
}
else
{
  App.ClearColor.R = 1;
  App.ClearColor.B = 0;
}]]>
              </Expression>
            </ZExpression>
          </OnTimer>
        </Timer>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[// Print CurrentRelativeTime as value with 2 decimals

float t = ColorTimer.CurrentRelativeTime;

string f = intToStr(frac(t)*100);
if(length(f) < 2)f = "0"+f;

Print.Text = intToStr(floor(t))+"."+f;]]>
      </Expression>
    </ZExpression>
    <UseMaterial Material="DinaMaterial"/>
    <RenderText Name="Print" Text="0.00" X="-0.9" Align="1" StretchY="2"/>
  </OnRender>
  <Content>
    <Group Comment="Dina">
      <Children>
        <Font Name="DinaFont" Bitmap="DinaBitmap" FirstChar="32" CharPixelWidth="8" CharPixelHeight="16"/>
        <Bitmap Name="DinaBitmap" Width="128" Height="128" Filter="1">
          <Producers>
            <BitmapFromFile Transparency="1" DataWidth="128" DataHeight="128">
              <BitmapFile>
<![CDATA[78DAED5DDB96E3200CE3FF7F7AF66DCF9C6930B27C011AF969A64D49A2185F844DC69048241289E42DF2F34BC0837FFF810FFEF91364846628F69EF1FFBF8F57F2F9EDF282ED038EC21F57C29EC7F1792538EC20C8E7E03F9BA48FF3F76722E9F6C7981DAE93CE0E93FEBB9493B03FCBD14E7B04D583FFB9EB6AFB8F18B7D3BC7027FE5BE29FA5A1FB02908DF97E9497CFB263F6C1C1994BF8115BC93FCD4EB5E90323AB443FCED9D88DDA58743D360E2ECD37F09F6994EBABB7E5DDEF71BE1289E48BED18EE2C246D79AB2B2E92D0544C1CFF59724A8CEF4A7E0745A6E1F11B411212D444B5FED7E16F2F88C42968EF1C8FD3B915B4460A7F9BA27B89CA530DDAD5F82FCF5884FF2DFACFADBC18A11AB838FB6709A6E81164017894FE0F3FF1D5B0049912FF544FA52CFF8B78D8A0816AB0EA9D2705D785B3F0C7EB19885BDB5EDDD1A0FF5CF07C54FC7F35FE83E2759BD75FFA53E966FBCF39C7E6F517C999CCCFB1DE3CD1950BFF2B6E53F86FC71FA94B4CF4B0C2BFCDEFBBF29DD95776F18F51D7EA4AA9F038D3F87776FD447D7E29FEAEB411BC129CD705D352BBBE6579523026BC177F6F5ABA7C5E46F15204FF41D5E75F813FC7EBC6F1B727858BF6897FEEE5AB8BEC0F8DFFA3A2DAD5A45EFD373C05E1BE677E0D9C2004FECB7B8FD81F64F014FC47C6BAFFAEF86779BCD728D1F873FA734EFCF3E6DCF304FF2BFC85BFF097ECE27F24122254131AF229C23F328984E15EFD3FFF1120FC39629F91922A90325AAE47CC4E8D132048C083B73947FAEEB3F27DD7E72033839717562C5810F8D37577DCF8DE719078FB55F87BE73B81BF8B076BC0DF360EFDF877EAFF39F6E73BF097FDCFB53FCB602631FEF1AE472CEB2E1092875E2F8BE3D31936A7971B55575C77E60B445C2AFC9B33CAB6063170AAD207B80CF217F70BEC9AD4A28024128944C2C5E79DEB716FF05674F84D34A51A5BB3CEB23C246DA7F97CFC368375B606EF57877F303C23EAEA693EA701FFEA54A2223CF6F24811FCED1D6BC1FE0B425DAFC31FD7A514FC87A7FF221DFFA2B72A80C630B2FE5B8DFF58EDBF9D62C641FC830E258BCFA7F1AFE8BF08CE0BD7DD7D37FEF66DBA0C51A26B8EECB37DF27A16DD7FC1297C965FF6E2EFB2E759C7EFC29F88FF89B848E46A5628C82565C23F8BFFE1FAFD2512894422915CE4F7BDFDE3AEF8DF3829BDBF8777FF1FEF38E075D2EB2359FCB3972F0D76DAD2F59FCD75F5F8E7C1E75B5166BC0BFF7EFD8CD7F1DE823FC2639C807F90CFF1DA557C7CD77E6E2EFC233BA2180072F66724EDAB56A4FFA07921F4DF85BF519FC0BD7758F8BBECCF0CFFACBE0C6EDFA493E31F047F504F1EF704E3F607C3FB471AE2FF94B8DD7BA90D7D6DE24B2512894422919C268A4C4E7804BB9E3BD76FEE8A6F893E77049FE538B8AA2FA740B01FD64BCF8EBC82F3B1A38FC0F815817FBC453A8EFFA8E91349E7F3D30D4BD1FB37973432DDF1B4B45717E1EFEA63F2CEC4E16F61589AE2A5CD89BC6733B8EF4A9B5376191357E5B0EB2B57EF4CF0A5E134AFBBFC3C45FF6D83F3F855CA0B6B3E870A1A8A745E7DE97F73ED7F1BFF8F3B1A6E0A14EDF75514FF2C4DB177EA8171FBD2FE2F437722EFA8E0DB7BB6BB9174E6B912894422915CEDDDD2399F6552702F383DE3B7ED4E59340297F4E13C7C967635E0EF852225E08FE3FFB86F06B26340D6456665BE41FCAB8D00B29FBC412E75E24F90BD771961702B98D97E4A0DFA8F932D7B1F41B5FD6FA0C5B8BAD3E07CEFC7DFEE53B3DFE7DE89FF5EFD1F65EB9888FF9D1DD96C7F76D9FFEDF8DF1BFFEC7A04F624F56E31140FFBBFE3FDB0CDF837E4A1D76D329F32DFFBEF2E52DAF42A424682C83F99AF58DD]]>
              </BitmapFile>
            </BitmapFromFile>
          </Producers>
        </Bitmap>
        <Material Name="DinaMaterial" Shading="1" Light="0" Font="DinaFont"/>
      </Children>
    </Group>
  </Content>
</ZApplication>
K
User avatar
Ats
Posts: 764
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Timer.CurrentRelativeTime behavior

Post by Ats »

Thanks for the explanations :wink:
Post Reply