How to add Tick to an Unreal Editor Module
January 3, 2022
Recently I got bored and decided to add Discord Rich Presence to the Unreal Editor. As part of this, I needed a way to fire off a callback as often as possible, this is usually done every frame. For games, you'd place the code within an Actor and use its built-in
Tick method. Unfortunately, Editor Modules, being Editor Modules, don't have Tick events as they aren't actual objects, they're just extensions of the Editor.
So how do we achieve this?
It's pretty simple, actually. It relies on the
Tickerclass. To use is we just need to include a single header in your .cpp file:
Ticker is built into Unreal, you shouldn't need any other modules or dependencies.
Then we want to set up the
Handle by adding:
Ticker Delegate and Handle
Delegate work's how
Delegates usually work with any language, you bind it to a function and it runs the bound function whenever the delegate is called.
Handles tend to act as extra configuration for the
Delegate. If you've used a
Timer in Blueprints you should have an understanding of how
Handles work in a more visual way.
Creating and Registering the Ticker Delegate and Handle
So this is pretty self-explanatory. First, we are using
CreateRaw to create the delegate binding. This passes in our
Tick function, which we will create in a moment. Or you could create it now, it's not that difficult. You then set up the
Handle by directly calling the
FTicker class and adding the
Delegate as a
Ticker. This creates the
Discord Module Tick Function
This is pretty self-explanatory too. We get
DeltaTime from the
Tick. I haven't done rigorous testing but, from what I can tell, it must return true. I assume this tells the Ticker that the tick was successful and allows it to run again.
And, finally, this is an important one. When your module shuts down, we want to run
RemoveTicker on the
Handle. As my Discord plugin runs as an Editor module, I want to make sure everything has cleared and shutdown cleanly. Unreal can sometimes leave a background process running so it's better to clean house as much as possible.
I will be releasing a guide on creating the Discord Editor plugin soon™.
In the meantime, I have a guide on How To Make Custom Unreal Engine Nodes Easily or why not Controlling what happens on Fell Out Of World in UE4, it's very quick and simple to do!