Gah - a solution with more questions. – EntropicLqd

Difference between revisions of "Timing"

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
(described some global variables)
 
(14 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
'''Timing''' is a family of functions using which you can schedule other functions to be called asynchronously
 
'''Timing''' is a family of functions using which you can schedule other functions to be called asynchronously
at a interval. Only <code>SetTimer</code> exists in UT2004.
+
at a interval. Only <code>SetTimer</code> exists in UT2004.
 +
Like in JavaScript, despite asynchronous nature of SetTimer, UnrealScript is still
 +
'''single-threaded'''. It means that you don't have to worry about thread-safety. Race conditions are still possible though.
  
 
== Functions ==
 
== Functions ==
Line 8: Line 10:
 
=== SetTimer ===
 
=== SetTimer ===
 
<uscript>
 
<uscript>
 +
// UT2004 and earlier:
 
native final function SetTimer(
 
native final function SetTimer(
 
     float inRate,  
 
     float inRate,  
     optional bool inbLoop, // not optional in UT2004
+
     bool inbLoop,
     optional Name inTimerFunc='Timer', // doesn't exist in UT2004
+
);
     optional Object inObj // doesn't exist in UT2004
+
 
);</uscript>
+
// UT3 and later:
 +
native final function SetTimer(
 +
    float inRate,
 +
    optional bool inbLoop,
 +
     optional Name inTimerFunc='Timer',  
 +
     optional Object inObj
 +
);
 +
</uscript>
 
Sets a timer to call the given function at a set interval.  Defaults to calling the 'Timer' event if no function is specified.  If inRate is set to 0.f it will effectively disable the previous timer.
 
Sets a timer to call the given function at a set interval.  Defaults to calling the 'Timer' event if no function is specified.  If inRate is set to 0.f it will effectively disable the previous timer.
* <code>inRate</code> - time interval, in gameplay seconds. If <code>inRate <= 0.0</code>, it'll reset the timer. <!-- will calling SetTimer override previous timer? or it'll create another one? -->
+
 
* <code>inbLoop<code> - if false, it'll fire only once. Default to false.
+
<ul>
* <code>inTimerFunc</code> - function name.
+
<li><code>inRate</code> - time interval, in '''gameplay''' seconds. If <code>inRate <= 0.0</code>, it'll reset the timer.</li>
* <code>inObj</code> - unknown.
+
<li><code>inbLoop<code> - if true, timer will be executed repeatedly every <code>inRate</code> seconds. If false, which is default, the timer will be executed only once.</li>
 +
<li><code>inTimerFunc</code> - function name.</li>
 +
<li><code>inObj</code> - object whose function will called. Defaults to <code>self</code>. Note that timer is still stored in the timer array of <code>self</code> object. E.g. destroying <code>self</code> object will remove all the timers, including the ones that are set to call function on some other object.
 +
</ul>
 +
 
 +
Note that it's very significant that timers use gameplay seconds (<code>WorldInfo.TimeSeconds</code>). They are stopped when the game is paused (unless <code>bAlwaysTick</code> is set), and are affected by time dilation (see below). If you need to set up a timer that uses real time, and does not depend on gameplay speed or game being paused, you have to emulate timer using <code>bAlwaysTick=true</code>, <code>event Tick</code> and <code>WorldInfo.RealTimeSeconds</code> variable.
  
 
=== ClearTimer ===
 
=== ClearTimer ===
 
<uscript>
 
<uscript>
 +
// available only in UT3 and later
 
native final function ClearTimer(
 
native final function ClearTimer(
 
     optional Name inTimerFunc='Timer',  
 
     optional Name inTimerFunc='Timer',  
Line 28: Line 44:
 
Clears a previously set timer.
 
Clears a previously set timer.
 
* <code>inTimerFunc</code> - function name.
 
* <code>inTimerFunc</code> - function name.
* <code>inObj</code> - unknown.
+
* <code>inObj</code> - object whose timers will be cleared (i.e. whose function would be called at timeout - as in <code>SetTimer()</code>).
 +
 
 +
It's equivalent to calling <code>SetTimer(0.0f, false, inTimerFunc, inObj)</code>.
 +
 
 +
=== ClearAllTimers ===
 +
<uscript>
 +
// available in UT3 and later
 +
native final function ClearAllTimers(
 +
    optional Object inObj
 +
);</uscript>
 +
Clears all timers for the given object.
 +
* <code>inObj</code> - object whose timers will be cleared.
 +
 
 +
=== PauseTimer ===
 +
<uscript>
 +
// available in UDK and later
 +
native final function PauseTimer(
 +
    bool bPause,
 +
    optional Name inTimerFunc='Timer',
 +
    optional Object inObj
 +
);</uscript>
 +
* <code>param</code>
 +
* <code>inTimerFunc</code> - function name.
 +
* <code>inObj</code> - object whose timer will be paused.
  
 
=== IsTimerActive ===
 
=== IsTimerActive ===
 
<uscript>
 
<uscript>
 +
// available in UT3 and later
 
native final function bool IsTimerActive(
 
native final function bool IsTimerActive(
 
     optional Name inTimerFunc='Timer',  
 
     optional Name inTimerFunc='Timer',  
     optional Object inObj);
+
     optional Object inObj
 +
);
 
</uscript>
 
</uscript>
 
Checks is specified timer is active.
 
Checks is specified timer is active.
 
* <code>inTimerFunc</code> - function name.
 
* <code>inTimerFunc</code> - function name.
* <code>inObj</code> - unknown.
+
* <code>inObj</code> - object whose function will be called.
  
 
=== GetTimerCount ===
 
=== GetTimerCount ===
 
<uscript>
 
<uscript>
 +
// available in UT3 and later
 
native final function float GetTimerCount(
 
native final function float GetTimerCount(
 
     optional Name inTimerFunc='Timer',
 
     optional Name inTimerFunc='Timer',
Line 50: Line 92:
 
Returns -1.f if the timer is not currently active. See <code>GetTimerRate</code> below for explanation.
 
Returns -1.f if the timer is not currently active. See <code>GetTimerRate</code> below for explanation.
 
* <code>inTimerFunc</code> - function name.
 
* <code>inTimerFunc</code> - function name.
* <code>inObj</code> - unknown.
+
* <code>inObj</code> - object whose function will be called.
  
 
=== GetTimerRate ===
 
=== GetTimerRate ===
 
<uscript>
 
<uscript>
 +
// available in UT3 and later
 
native final function float GetTimerRate(
 
native final function float GetTimerRate(
 
     optional name TimerFuncName = 'Timer',  
 
     optional name TimerFuncName = 'Timer',  
 
     optional Object inObj
 
     optional Object inObj
 
);
 
);
* <code>inTimerFunc</code> - function name.
 
* <code>inObj</code> - unknown.
 
 
</uscript>
 
</uscript>
 
Gets the current rate for the specified timer.  
 
Gets the current rate for the specified timer.  
 
<code>GetTimerRate('SomeTimer') - GetTimerCount('SomeTimer')</code> is the time remaining before 'SomeTimer' is called.
 
<code>GetTimerRate('SomeTimer') - GetTimerCount('SomeTimer')</code> is the time remaining before 'SomeTimer' is called.
 
In other words, "count" starts at 0, increases, and when it reaches "rate", timer is fired.
 
In other words, "count" starts at 0, increases, and when it reaches "rate", timer is fired.
 +
* <code>inTimerFunc</code> - function name.
 +
* <code>inObj</code> - object whose function will be called.
 +
 +
=== ModifyTimerTimeDilation ===
 +
<uscript>
 +
// available in UDK and later
 +
native final function ModifyTimerTimeDilation(
 +
  const name TimerName,
 +
  const float InTimerTimeDilation,
 +
  optional Object inObj
 +
);</uscript>
 +
This will search the Timers on this actor and set the passed in TimerTimeDilation
 +
* <code>inTimerFunc</code> - function name.
 +
* <code>InTimerTimeDilation</code> - new time dilation for this timer function.
 +
* <code>inObj</code> - object whose function will be called.
 +
 +
=== ResetTimerTimeDilation ===
 +
<uscript>
 +
// available in UDK and later
 +
native final function ResetTimerTimeDilation(
 +
    const name TimerName,
 +
    optional Object inObj
 +
);</uscript>
 +
This will search the Timers on this actor and reset the TimerTimeDilation to 1.0f
 +
* <code>inTimerFunc</code> - function name.
 +
* <code>inObj</code> - object for accessing function name.
 +
 +
== Variables ==
 +
* <code>WorldInfo.TimeSeconds</code> - time since level began play, affected by pause and time dilation
 +
* <code>WorldInfo.RealTimeSeconds</code> - time since level began play, is not affected by pause and time dilation
 +
* <code>WorldInfo.AudioTimeSeconds</code> - time since level began play, affected by pause only
 +
 +
Note that in online play, these variables are not synchronized over network. That is, <code>TimeSeconds</code> on the client
 +
signifies time since it has joined the game.
 +
 +
Moreover, seamsless travel (introduced in UT3), carries over <code>TimeSeconds</code> from previous level.
 +
 +
== Time Dilation ==
 +
WorldInfo object includes a <code>TimeDilation</code> property. The correct way to access it from UnrealScript is to use <code>SetGameSpeed</code> function defined in <code>GameInfo</code>. This property can also be manipulated with <code>slomo</code> cheat command.
  
== How to make seconds real ==
+
The more dilation is, the faster game is. For instance, if <code>TimeDilation</code> was 2.0, the game would be twice as fast.  It means that each real world second would correspond to two in-game ones. A timer with rate 1.0 would be called each gameplay second. But a timer with rate <code>WorldInfo.TimeDilation</code> would be called once per '''real''' second.
Multiply rate by <code>WorldInfo.TimeDilation</code>. Of course, this method isn't absolutely reliable,
+
as TimeDilation may change before timer fires.
+
  
 +
In PC version of UT3 there's additional multiplier equal to 1.1. That is, <code>WorldInfo.SetGameSpeed(x)</code> will set time dilation to <code>x * 1.1</code>.
 
== See also ==
 
== See also ==
 
* [[UE3:Actor native functions (UT3)#SetTimer | SetTimer in UT3]]
 
* [[UE3:Actor native functions (UT3)#SetTimer | SetTimer in UT3]]
 
* [[UE3:Actor native functions (UDK)#SetTimer | SetTimer in UDK]]
 
* [[UE3:Actor native functions (UDK)#SetTimer | SetTimer in UDK]]

Latest revision as of 09:57, 22 February 2014


Timing is a family of functions using which you can schedule other functions to be called asynchronously at a interval. Only SetTimer exists in UT2004. Like in JavaScript, despite asynchronous nature of SetTimer, UnrealScript is still single-threaded. It means that you don't have to worry about thread-safety. Race conditions are still possible though.

Functions[edit]

SetTimer[edit]

// UT2004 and earlier:
native final function SetTimer(
    float inRate, 
    bool inbLoop,
);
 
// UT3 and later:
native final function SetTimer(
    float inRate, 
    optional bool inbLoop, 
    optional Name inTimerFunc='Timer', 
    optional Object inObj
);

Sets a timer to call the given function at a set interval. Defaults to calling the 'Timer' event if no function is specified. If inRate is set to 0.f it will effectively disable the previous timer.

  • inRate - time interval, in gameplay seconds. If inRate <= 0.0, it'll reset the timer.
  • inbLoop - if true, timer will be executed repeatedly every inRate seconds. If false, which is default, the timer will be executed only once.
  • inTimerFunc - function name.
  • inObj - object whose function will called. Defaults to self. Note that timer is still stored in the timer array of self object. E.g. destroying self object will remove all the timers, including the ones that are set to call function on some other object.

Note that it's very significant that timers use gameplay seconds (WorldInfo.TimeSeconds). They are stopped when the game is paused (unless bAlwaysTick is set), and are affected by time dilation (see below). If you need to set up a timer that uses real time, and does not depend on gameplay speed or game being paused, you have to emulate timer using bAlwaysTick=true, event Tick and WorldInfo.RealTimeSeconds variable.

ClearTimer[edit]

// available only in UT3 and later
native final function ClearTimer(
    optional Name inTimerFunc='Timer', 
    optional Object inObj 
);

Clears a previously set timer.

  • inTimerFunc - function name.
  • inObj - object whose timers will be cleared (i.e. whose function would be called at timeout - as in SetTimer()).

It's equivalent to calling SetTimer(0.0f, false, inTimerFunc, inObj).

ClearAllTimers[edit]

// available in UT3 and later
native final function ClearAllTimers(
    optional Object inObj
);

Clears all timers for the given object.

  • inObj - object whose timers will be cleared.

PauseTimer[edit]

// available in UDK and later
native final function PauseTimer(
    bool bPause, 
    optional Name inTimerFunc='Timer', 
    optional Object inObj
);
  • param
  • inTimerFunc - function name.
  • inObj - object whose timer will be paused.

IsTimerActive[edit]

// available in UT3 and later
native final function bool IsTimerActive(
    optional Name inTimerFunc='Timer', 
    optional Object inObj
);

Checks is specified timer is active.

  • inTimerFunc - function name.
  • inObj - object whose function will be called.

GetTimerCount[edit]

// available in UT3 and later
native final function float GetTimerCount(
    optional Name inTimerFunc='Timer',
    optional Object inObj
);

Gets the current count for the specified timer, defaults to 'Timer' if no function is specified. Returns -1.f if the timer is not currently active. See GetTimerRate below for explanation.

  • inTimerFunc - function name.
  • inObj - object whose function will be called.

GetTimerRate[edit]

// available in UT3 and later
native final function float GetTimerRate(
    optional name TimerFuncName = 'Timer', 
    optional Object inObj
);

Gets the current rate for the specified timer. GetTimerRate('SomeTimer') - GetTimerCount('SomeTimer') is the time remaining before 'SomeTimer' is called. In other words, "count" starts at 0, increases, and when it reaches "rate", timer is fired.

  • inTimerFunc - function name.
  • inObj - object whose function will be called.

ModifyTimerTimeDilation[edit]

// available in UDK and later
native final function ModifyTimerTimeDilation(
   const name TimerName, 
   const float InTimerTimeDilation,
   optional Object inObj
);

This will search the Timers on this actor and set the passed in TimerTimeDilation

  • inTimerFunc - function name.
  • InTimerTimeDilation - new time dilation for this timer function.
  • inObj - object whose function will be called.

ResetTimerTimeDilation[edit]

// available in UDK and later
native final function ResetTimerTimeDilation(
    const name TimerName,
    optional Object inObj
);

This will search the Timers on this actor and reset the TimerTimeDilation to 1.0f

  • inTimerFunc - function name.
  • inObj - object for accessing function name.

Variables[edit]

  • WorldInfo.TimeSeconds - time since level began play, affected by pause and time dilation
  • WorldInfo.RealTimeSeconds - time since level began play, is not affected by pause and time dilation
  • WorldInfo.AudioTimeSeconds - time since level began play, affected by pause only

Note that in online play, these variables are not synchronized over network. That is, TimeSeconds on the client signifies time since it has joined the game.

Moreover, seamsless travel (introduced in UT3), carries over TimeSeconds from previous level.

Time Dilation[edit]

WorldInfo object includes a TimeDilation property. The correct way to access it from UnrealScript is to use SetGameSpeed function defined in GameInfo. This property can also be manipulated with slomo cheat command.

The more dilation is, the faster game is. For instance, if TimeDilation was 2.0, the game would be twice as fast. It means that each real world second would correspond to two in-game ones. A timer with rate 1.0 would be called each gameplay second. But a timer with rate WorldInfo.TimeDilation would be called once per real second.

In PC version of UT3 there's additional multiplier equal to 1.1. That is, WorldInfo.SetGameSpeed(x) will set time dilation to x * 1.1.

See also[edit]