Difference between revisions of "IC Python API:RLPy RPyTimer"

From Reallusion Wiki!
Jump to: navigation, search
m
m
 
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
== Detailed Description ==
+
{{last_modified}}
This class provides repetitive and single-shot timers.
+
 
 +
== Description ==
 +
 
 +
This class is used to setup a timed event.  Use [[#SetInterval ( self, nMSec )|SetInterval]] and [[#GetInterval ( self )|GetInterval]] to access and set the time interval.  Use [[#SetSingleShot ( self, bSingleShot )|SetSingleShot]] and [[#IsSingleShot ( self )|IsSingleShot]] to access and set the trigger state (single shot or repeatly trigger). Use [[#RPyTimer.Start ( self )|Start]], [[#Stop ( self )|Stop]], and [[#IsRunning ( self )|IsRunning]] to control the execution of the Timer.
 +
 
 +
In order to receive trigger events, first create a new class and inherit from '''RLPyTimerCallback''' and register with '''RegisterPyTimerCallback'''.  When not in use, you can unregister the timer event with '''UnRegisterPyTimerCallback'''.
 +
 
 +
See Also: [[#IC_Python_API:RLPy_RPyTimerCallback|RPyTimerCallback]]
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
 
class MyPyTimerCallback(RLPy.RPyTimerCallback):
 
class MyPyTimerCallback(RLPy.RPyTimerCallback):
def __init__(self):
+
  def __init__(self):
   RLPy.RPyTimerCallback.__init__(self)
+
      RLPy.RPyTimerCallback.__init__(self)
 
+
  def Timeout(self):
def Timeout(self):
+
      print ("timeout")
   print ("timeout")
+
Using Try catch??
+
 
# set timer
 
# set timer
 
timer = RLPy.RPyTimer()
 
timer = RLPy.RPyTimer()
 
timer.SetInterval(100)
 
timer.SetInterval(100)
 
timer.SetSingleShot(True)
 
timer.SetSingleShot(True)
print(timer.GetInterval()) # 100
+
print(timer.GetInterval()) # 100
 
# register callback
 
# register callback
timer_callback = MyPyTimerCallback()
+
timer_callback = MyPyTimerCallback() timer_callback crash issue??
 
timer.RegisterPyTimerCallback(timer_callback)
 
timer.RegisterPyTimerCallback(timer_callback)
 
# start timer
 
# start timer
Line 24: Line 31:
 
# unregister callback
 
# unregister callback
 
timer.UnregisterPyTimerCallback()
 
timer.UnregisterPyTimerCallback()
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
== Member Functions ==
 
== Member Functions ==
=== GetInterval ===
+
 
<syntaxhighlight lang="Python">
+
=== GetInterval ( self ) ===
RLPy.RPyTimer.GetInterval ( self )
+
 
</syntaxhighlight>
+
Get the time interval for the event triggered by the timer in milliseconds. The default interval value is 0, which will cause the timer to start triggering immediately after '''Start()'''.
Get timer interval value.
+
 
 +
See Also: [[#SetInterval ( self, nMSec )|SetInterval]]
 +
 
 
==== Returns ====
 
==== Returns ====
<div style="margin-left: 2em;">
+
:Timer interval ( milisecond ) - int
Timer interval in milliseconds - int
+
 
</div>
+
-----
+
=== IsRunning ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.IsRunning ( self )
+
timer = RLPy.RPyTimer()
 +
timer.SetInterval(1000)
 +
print (timer.GetInterval()) # 1000
 
</syntaxhighlight>
 
</syntaxhighlight>
Check if the timer is running.
+
 
 +
=== IsRunning ( self )  ===
 +
 
 +
Check if the Timer is currently running.
 +
 
 +
See Also: [[#Start ( self )|Start]], [[#Stop ( self )|Stop]]
 +
 
 
==== Returns ====
 
==== Returns ====
<div style="margin-left: 2em;">
+
:'''True''' if the timer is currenlty running, else '''False''' - bool
True if the timer is running; otherwise returns false - bool
+
 
</div>
+
-----
+
=== IsSingleShot ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.IsSingleShot ( self )
+
timer = RLPy.RPyTimer()
 +
timer.Start()
 +
print (timer.IsRunning()) # True
 
</syntaxhighlight>
 
</syntaxhighlight>
Get timer single shot status.
+
 
 +
=== IsSingleShot ( self ) ===
 +
 
 +
Get the Timer trigger settings.  Return '''True''' if the trigger is single-shot and '''False''' for repeat trigger.
 +
 
 +
See Also: [[#SetSingleShot ( self, bSingleShot )|SetSingleShot()]]
 +
 
 
==== Returns ====
 
==== Returns ====
<div style="margin-left: 2em;">
+
:'''True''' if trigger is single-shot, else '''False''' - bool
Is timer single shot or not - bool
+
 
</div>
+
-----
+
=== RegisterPyTimerCallback ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.RegisterPyTimerCallback ( self, pCallback )
+
timer = RLPy.RPyTimer()
 +
timer.SetSingleShot(True)
 +
print (timer.IsSingleShot()) # True
 
</syntaxhighlight>
 
</syntaxhighlight>
Register timer event callback.
+
 
 +
=== RegisterPyTimerCallback ( self, pCallback ) ===
 +
 
 +
Register a Callback object with the Timer.
 +
 
 +
See Also: [[#UnregisterPyTimerCallback ( self, pCallback )|UnregisterPyTimerCallback()]]
 +
 
 
==== Parameters ====
 
==== Parameters ====
<div style="margin-left: 2em;">
+
:pCallback [IN] Callback object inherited from '''RPyTimerCallback''' - RLPy.RPyTimerCallback
'''pCallback''' [IN] pointer of timer callback object - RLPy.RPyTimerCallback
+
 
</div>
+
-----
+
=== SetInterval ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.SetInterval ( self, nMSec )
+
class MyPyTimerCallback(RLPy.RPyTimerCallback):
 +
  def __init__(self):
 +
      RLPy.RPyTimerCallback.__init__(self)
 +
  def Timeout(self):
 +
      print ("timeout")
 +
 +
timer = RLPy.RPyTimer()
 +
timer_callback = MyPyTimerCallback()
 +
timer.RegisterPyTimerCallback(timer_callback)
 
</syntaxhighlight>
 
</syntaxhighlight>
Set timer interval value.
+
 
 +
=== UnregisterPyTimerCallback ( self, pCallback ) ===
 +
 
 +
Unregister the Callback object for the Timer.
 +
 
 +
See Also: [[#RegisterPyTimerCallback ( self, pCallback )|RegisterPyTimerCallback()]]
 +
 
 +
<syntaxhighlight lang="Python">
 +
class MyPyTimerCallback(RLPy.RPyTimerCallback):
 +
  def __init__(self):
 +
      RLPy.RPyTimerCallback.__init__(self)
 +
  def Timeout(self):
 +
      print ("timeout")
 +
 +
timer = RLPy.RPyTimer()
 +
timer_callback = MyPyTimerCallback()
 +
timer.RegisterPyTimerCallback(timer_callback)
 +
...
 +
# unregister callback
 +
timer.UnregisterPyTimerCallback()
 +
</syntaxhighlight>
 +
 
 +
=== SetInterval ( self, nMSec ) ===
 +
 
 +
Set the Timer trigger interval in milliseconds.  The default interval value is 0, which will cause the timer to start triggering immediately after '''Start()'''.
 +
 
 +
See Also: [[#GetInterval ( self )|GetInterval]]
 +
 
 
==== Parameters ====
 
==== Parameters ====
<div style="margin-left: 2em;">
+
:nMSec [IN] Timer interval in milliseconds - int
'''nMSec''' [IN] timer interval in milliseconds - int
+
 
</div>
+
-----
+
=== SetSingleShot ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.SetSingleShot ( self, bSingleShot )
+
timer = RLPy.RPyTimer()
 +
timer.SetInterval(1000)
 +
print (timer.GetInterval()) # 1000
 
</syntaxhighlight>
 
</syntaxhighlight>
Set timer single shot status.
+
 
 +
=== SetSingleShot ( self, bSingleShot ) ===
 +
 
 +
Set the Timer trigger state.  Single-shot will only fire off the trigger event once, otherwise the event will fire off according to the Timer interval.  The default setting is '''False'''.
 +
 
 +
See Also: [[#IsSingleShot ( self )|IsSingleShot]]
 +
 
 
==== Parameters ====
 
==== Parameters ====
<div style="margin-left: 2em;">
+
:bSingleShot [IN] Set to single-shot triggering - bool
'''bSingleShot''' [IN] timer single shot status - bool
+
 
</div>
+
-----
+
=== Start ===
+
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.Start ( self )
+
timer = RLPy.RPyTimer()
 +
timer.SetSingleShot(True)
 +
print (timer.IsSingleShot()) # True
 
</syntaxhighlight>
 
</syntaxhighlight>
Start the timer.
+
 
Starts or restarts the timer. If the timer is already running, it will be stopped and restarted.
+
=== Start ( self ) ===
-----
+
 
=== Stop ===
+
Start or restart the Timer. If the Timer is already running, then this operation will stop the Timer first then start it again.  If the trigger is set to single-shot then the Timer event will activate one more time.
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.Stop ( self )
+
timer = RLPy.RPyTimer()
 +
timer.SetInterval(100)
 +
timer.Start()
 
</syntaxhighlight>
 
</syntaxhighlight>
Stop the timer.
+
 
-----
+
=== Stop ( self ) ===
=== UnregisterPyTimerCallback ===
+
 
 +
Stop the Timer.
 +
 
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
RLPy.RPyTimer.UnregisterPyTimerCallback ( self )
+
timer = RLPy.RPyTimer()
 +
timer.SetInterval(100)
 +
timer.Start()
 +
# do something
 +
timer.Stop()
 
</syntaxhighlight>
 
</syntaxhighlight>
Unregister timer event callback.
 

Latest revision as of 20:58, 26 March 2020

Main article: Modules. Last modified: 03/26/2020

Description

This class is used to setup a timed event. Use SetInterval and GetInterval to access and set the time interval. Use SetSingleShot and IsSingleShot to access and set the trigger state (single shot or repeatly trigger). Use Start, Stop, and IsRunning to control the execution of the Timer.

In order to receive trigger events, first create a new class and inherit from RLPyTimerCallback and register with RegisterPyTimerCallback. When not in use, you can unregister the timer event with UnRegisterPyTimerCallback.

See Also: RPyTimerCallback

class MyPyTimerCallback(RLPy.RPyTimerCallback):
   def __init__(self):
       RLPy.RPyTimerCallback.__init__(self)
   def Timeout(self):
       print ("timeout")
 Using Try catch??
# set timer
timer = RLPy.RPyTimer()
timer.SetInterval(100)
timer.SetSingleShot(True)
print(timer.GetInterval())  # 100
# register callback
timer_callback = MyPyTimerCallback() timer_callback crash issue??
timer.RegisterPyTimerCallback(timer_callback)
# start timer
timer.Start()
...
# unregister callback
timer.UnregisterPyTimerCallback()

Member Functions

GetInterval ( self )

Get the time interval for the event triggered by the timer in milliseconds. The default interval value is 0, which will cause the timer to start triggering immediately after Start().

See Also: SetInterval

Returns

Timer interval ( milisecond ) - int
timer = RLPy.RPyTimer()
timer.SetInterval(1000)
print (timer.GetInterval())	# 1000

IsRunning ( self )

Check if the Timer is currently running.

See Also: Start, Stop

Returns

True if the timer is currenlty running, else False - bool
timer = RLPy.RPyTimer()
timer.Start()
print (timer.IsRunning())	# True

IsSingleShot ( self )

Get the Timer trigger settings. Return True if the trigger is single-shot and False for repeat trigger.

See Also: SetSingleShot()

Returns

True if trigger is single-shot, else False - bool
timer = RLPy.RPyTimer()
timer.SetSingleShot(True)
print (timer.IsSingleShot())	# True

RegisterPyTimerCallback ( self, pCallback )

Register a Callback object with the Timer.

See Also: UnregisterPyTimerCallback()

Parameters

pCallback [IN] Callback object inherited from RPyTimerCallback - RLPy.RPyTimerCallback
class MyPyTimerCallback(RLPy.RPyTimerCallback):
   def __init__(self):
       RLPy.RPyTimerCallback.__init__(self)
   def Timeout(self):
       print ("timeout")
 
timer = RLPy.RPyTimer()
timer_callback = MyPyTimerCallback()
timer.RegisterPyTimerCallback(timer_callback)

UnregisterPyTimerCallback ( self, pCallback )

Unregister the Callback object for the Timer.

See Also: RegisterPyTimerCallback()

class MyPyTimerCallback(RLPy.RPyTimerCallback):
   def __init__(self):
       RLPy.RPyTimerCallback.__init__(self)
   def Timeout(self):
       print ("timeout")
 
timer = RLPy.RPyTimer()
timer_callback = MyPyTimerCallback()
timer.RegisterPyTimerCallback(timer_callback)
...
# unregister callback
timer.UnregisterPyTimerCallback()

SetInterval ( self, nMSec )

Set the Timer trigger interval in milliseconds. The default interval value is 0, which will cause the timer to start triggering immediately after Start().

See Also: GetInterval

Parameters

nMSec [IN] Timer interval in milliseconds - int
timer = RLPy.RPyTimer()
timer.SetInterval(1000)
print (timer.GetInterval())	# 1000

SetSingleShot ( self, bSingleShot )

Set the Timer trigger state. Single-shot will only fire off the trigger event once, otherwise the event will fire off according to the Timer interval. The default setting is False.

See Also: IsSingleShot

Parameters

bSingleShot [IN] Set to single-shot triggering - bool
timer = RLPy.RPyTimer()
timer.SetSingleShot(True)
print (timer.IsSingleShot())	# True

Start ( self )

Start or restart the Timer. If the Timer is already running, then this operation will stop the Timer first then start it again. If the trigger is set to single-shot then the Timer event will activate one more time.

timer = RLPy.RPyTimer()
timer.SetInterval(100)
timer.Start()

Stop ( self )

Stop the Timer.

timer = RLPy.RPyTimer()
timer.SetInterval(100)
timer.Start()
# do something
timer.Stop()