本文内容
[Lua]-Using-Tick-Bus
Polling
When you need to do something every frame, the TickBus
is what you will need to use. The TickBus
will notify every component that is subscribed to it when a new frame has occured. The component will also receive information such as deltaTime
: the time in seconds since the last frame.
local Ticking = {}
function Ticking:OnActivate()
self.tickHandler = TickBus.Connect(self)
end
function Ticking:OnDeactivate()
self.tickHandler:Disconnect()
self.tickHandler = nil
end
function Ticking:OnTick(deltaTime, timePoint)
Debug.Log("I am printing every frame! Time since last frame in seconds: " .. deltaTime)
end
return Ticking
Breakdown
Connecting to TickBus
When the component activates, we use Connect
to subscribe to TickBus
events. We need a variable self.tickHandler
to store a reference so that we can unsubscribe from the TickBus
. You can name this variable whatever you like.
function Ticking:OnActivate()
self.tickHandler = TickBus.Connect(self)
end
Receiving Tick Events
To receive TickBus
events you simply need to have correctly named TickBus
event functions, which will be called automatically as long as your component is subscribed to the TickBus
. Any parameters you include will also be populated when the function gets called. Use the Classes Reference
in LuaIDE
to find the available events and parameters.
function Ticking:OnTick(deltaTime, timePoint)
Debug.Log("I am printing every frame! Time since last frame in seconds: " .. deltaTime)
end
Disconnecting from TickBus
To unsubscribe from TickBus
events, use Disconnect
on the reference stored during the connection process. It is not strictly necessary to Disconnect
or set variables to nil
when a component deactivates, but it is considered best practice. It is safe to use Disconnect
on a handler that is already disconnected but not when it is nil
.
function Ticking:OnDeactivate()
self.tickHandler:Disconnect()
self.tickHandler = nil
end
Additional Resources
TickBus.h
source code link