How do i use a timer in a UObject ?

Hi all,

I am using this code to define a timer:

FTimerHandle ThisTimerHandle;
ThisPlayerController->GetWorld()->GetTimerManager().SetTimer(ThisTimerHandle, this, &UYagFileServer::Test, 1.3f, true);

void UYagFileServer::Test()
{
	GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, FString::Printf(TEXT("Test OK")));
}

It works in a APlayerController derived class (with the right delegate and GetWorld() called directly), but not in a UObject derived class.

The timer is activated in both cases (IsActive() function returns true in both cases), but the “Test OK” message is not shown in the UObject.

Any reason for that ? Am i doing anything wrong ?

Thanks

Cedric

Hi,

This doesn’t work either:

void UYagFileServer::Start()
{
	FTimerHandle UniqueHandle;
	FTimerDelegate ListenerDelegate = FTimerDelegate::CreateUObject(this, &UYagFileServer::TCPConnectionListener);
	FileServerWorld->GetTimerManager().SetTimer(UniqueHandle, ListenerDelegate, 1.3f, true);

	if (FileServerWorld->GetTimerManager().IsTimerActive(UniqueHandle)) GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, FString::Printf(TEXT("UniqueHandle active")));
}

void UYagFileServer::TCPConnectionListener()
{
	GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Red, FString::Printf(TEXT("TCPConnectionListener")));
}

The “UniqueHandle active” message is displayed, but not the one in the delegate. So the timer is active but the delegate does not run.

The code doesn’t look too fancy, any idea of what i am missing ?

Thanks

Cedric

Problem solved in the forum: