I am trying to call a function every 30 seconds. I have used this before without issues so I’m not sure why it isn’t working now. Any ideas? Has something changed in 4.24 maybe?
.h I have tried both public and private
UFUNCTION()
void Ping();
And the .cpp I see both logs in the output so I know its getting there. Also the ensure passes. I have also tried GetWorld which is null in a UObject and GWorld which also works but has the same problem of the ping function not being called at all.
UE_LOG(LogTemp, Warning, TEXT("start ping"))
if (ensure(GEngine))
{
UWorld* World = GEngine->GetWorldContexts()[0].World();
UE_LOG(LogTemp, Warning, TEXT("start ping has gworld"))
World->GetTimerManager().SetTimer(PingTimerHandle, this, &UGameJoltApi::Ping, 30, true, 30);
}
And here is the ping function. Its not called even once. I never see the log in the output.
GetWorldTimerManager()
or GetWorld()->GetTimerManager()
Could you give this a try and see if it works?
Also I’m assuming the first bit of code that’s setting up the timer is not being called in your constructor but somewhere like BeginPlay or a later point?
I have tried GetWorld()->GetTimerManager() which doesn’t work because I’m in a UObject and GetWorld() is null. I haven’t tried GetWorldTimerManager() though. I’ll try that now.
Ah, I see. Both examples are on classes that extend from AActor, which has the GetWorldTimerManager method, so it won’t work with UObject. Have you looked into using the AInfo class which extends from actor and exists in the world but without a physical representation?
Ok, if it’s being called from GameInstance then you can pass it the TimeManger of GameInstance itself which you can get by calling UGameInstance->GetTimerManager(). The function is listed here
Thats funny I just found that out and waiting for compile now after changing to pass in the FTimerManager from the GameInstance. I’ll let you know if it works.
Can you show me the code you tried with GameInstance’s TimerManager? Did you step through the code and make sure that the TimeManager you’re passing to your UObject is not null?
I havent found an answer but I found a workaround. I moved the code in the UObject that starts the timer to my GameInstance and everything works as it should. I have no idea why I couldn’t do this in the UObject but this is probably better design wise anyway as the API shouldn’t have to worry about timers and such.