We can find an example of using a UObject in several areas of the source code. My example comes from AbilityTask_Repeat.cpp in the Gameplay Abilities modules. Here we have a time set on the ability task.
Also, if you are getting access violations during runtime, your objects may be going out of scope or you may have invalid pointers. GetWorld() returns a pointer, so make sure that the GetWorld() return’s a valid pointer by checking against nullptr. Also make sure that you are not destroying the object (UGameManager) after setting the timer and make sure it is not destroyed before the timer callback is called.
Yep, when you derive from UObject, GetWorld returns nullptr by default. So you need to override it and find the world. Often, you know your object will have an actor/component as its outer and can do:
return GetOuter()->GetWorld();
On the other hand, if this object has no inherent attachment to a world, you can get the timer manager from the UGameInstance instead.
To anyone in the future: if your outer is a level, it’ll return a non-null world but it won’t work for setting timers. They exist and you can use their handle to see that they’re active and non-paused, but their time remaining never changes.
I ended up just using my UI to get the world (Which works since UUserWidgets have a working GetWorld).