Using a timer in UObject

Hello people, well I already searched the forums and answerhub about this problem and could not find a solution.
I am trying to use a timer inside a UObject. So what I got so far, here is an example:

FTimerHandle ThisTimerHandle;
()->GetTimerManager().SetTimer(ThisTimerHandle, this, &UMyUObject::Test, 1.0f, true);
 void UMyUObject::Test()
     GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::, FString::Printf(TEXT("Works")));

This gives an Access Violation (Nullpointer insider ()). I also can check what () returns and this is NULL. So I took a look into the () Funktion in UObject and I noticed that it always returns NULL. This makes the () useless for me, because I need a timer inside the UObject. Sure I could try to get the UObject over another actor which is the problem behind. I am creating the UObject on an empty map which has no actors inside(Main Menu). Just the BP Widget what is getting spawned inside the Map’s Begin Play.
Is there a possibility to use other timer methodes instead of this one I posted here ?

Why don’t you use an Actor instead?

Because I prefer objects which are more easy to implement for me. Just object oriented. Also I have to spawn and get the actor everytime and the object makes handling easier than the actor.

An Actor is any object that can be
placed into a level. Actors are a
generic Class that supports 3D
transformations such as translation,
rotation, and scale.

If you want your object to be in your scene you should use an Actor.

I think I gonna swap over to an actor instead of an UObject… thank you for your time

Hi ,

I see that you are having an issue with UObjects but it seems like you’ve changed course to use an Actor instead. I hope this fixed your issue. If that is not the case, please let us know and we’ll be happy to look into it further.

In the meantime, I’ll be resolving this question for tracking purposes.

Have a nice day,

Yeah, I completely switched to AActor instead of UObject because I couldn’t handle the error itself. Inside Actor it is working fine…

Timer in UObjects… I need it :slight_smile:

You could also pass UWorld* to UObject somehow, if you initiate this object in Actor, set UWorld* varbale using (). Or if you set timer in specific function which is called in actor, make UWorld* argument (UE4 UObject functions in engine do that frequently), you can use FTimerHandle as a return value for that function

There also GWorld, but in editor GWorld might be something else then game world because editor uses multiple UWorld instances, so it’s not recomand to use it or you might lose control of timers or they might continue to run when you turn off PIE.

UObject classes doesnot access to world in default.
You need implement this function.

UWorld* () override 
  return GetOuter()->();

Or just use simply use:


GetOuter()->()->SetTimer(TimerHandle, this, &UMyObject::Tick, 0.1f, true, 0.1f);