Create a Timer for Plugin without reference to UWorld or AActor.

I’m currently putting together a plugin that needs to poll a service for updates to some connected device’s state, and serve those updates to actors as requested. To do so, I think I need to create a timer from within the plugin, rather than indirectly by referencing an Actor (AActor::GetWorldTimerManager()) / World (UWorld::GetWorldTimerManager()). Is there some way for me to do this?

Failing this, perhaps I could create a persistent AActor / UWorld when attempting to access the plugin when it’s not initialised. The created object should then hopefully have a reference to the FTimerManager instance to permit using SetTimer(), or alternatively hooking into the created AActor’s Tick() with an appropriate ETickingGroup.

The basic pattern is:

  1. Some class instance / blueprint requests the service’s functionality via my plugin.

  2. If not initialised, initialise my plugin’s connection to the service.

  3. Data is regularly updated in an instance of a class in my plugin (the service’s update rate is independent of any in-engine framerate, but, if new data is received, reception should ideally occur somewhere between after rendering the previous frame, and before the current frame’s physics updates), and other class instances can now call getters for it.

would implementing your own timer class not work ?
I am doing this in my own Framework Library that I include with my unreal project. You could either do the same or just write a small timer function - its really not that difficult to do :slight_smile:

here some links (didnt thorougly read them but it gives you the idea)

C++11 chrono timer

some stack overflow question

in particular this part from that thread

class Timer
{
  LARGE_INTEGER startTime ;
  double fFreq ;

public:
  Timer() {
    LARGE_INTEGER freq ;
    QueryPerformanceFrequency( &freq ) ;
    fFreq = (double)freq.QuadPart ;
    reset();
  }

  void reset() {   QueryPerformanceCounter( &startTime ) ;  }

  // Gets the most up to date time.
  double getTime() const {
    LARGE_INTEGER endTime ;
    QueryPerformanceCounter( &endTime ) ;
    return ( endTime.QuadPart - startTime.QuadPart ) / fFreq ; // as double
  }
} ;

and you use it like that

int main()
{
  Timer t ;

  // do some things..
  printf( "time: %f\n", t.getTime() );

  t.reset() ;
  // do some more things
  printf( "time: %f\n", t.getTime() );
}

just search for C++ Game Timer or C++ precise timer and you will find plrenty of results, pick one :slight_smile:

hope that helped,
chrys

Thanks for the suggestion Chrys.

In the mean time, I’ve had another idea for how my plugin system should behave, though it’s esoteric to the service I’m connecting with. Given my question, your comment’s probably the most sensible way of doing what I was asking, if there’s no other way of hooking into UE4’s timer system (and it seems that way!).

Take a look at the FTicker class in CoreMisc.cpp. This class allows you to register a callback function that will be called periodically (you can specify the delay).

For high precision timing take a look at the FPlatformTime class instead. It has platform specific implementations for all supported platforms, including a QueryPerformanceCounter version for Windows.