Pause/Clear/Destroy timer by event?

I have a UObject that’s being treated a a buff with its own life span timer and etc. It makes a timer if its a ticking buff and it should destroy or pause the timer once its life span has been reached. I have no clue how to destroy timers. everyone says the GC does that when the owning object is no longer in use, but I’m having issues destroying my UObject as described here, so I want to manually halt the timer. I see commands for pausing by function, but how do I pause by event?

You can set the timer to a 0 time and its the same as ClearTimer. Im not sure if that will help with the GC though as thats handled internally, I roughly know theres a grey list before an object gets blacklisted which is where it is ready for cleaning.

How do I get a currently active timer though. I see a node for set timer, but am I setting a currently active timer or a new one? Is there even a timer object to get?

I tried that but I can’t seem to access anything on the timer handle variable. There’s no context from it that can pause or clear the timer. I’m so confused at how this is designed.

So I tried just writing code to deal with this because blueprints are weird with timers. But In my UObject here I simply can’t get it working.

// Buff.cpp

void UBuff::OnCreation_Implementation(){

   FTimerHandle buffTimer;

   if(tickRate > 0.f){
      GetWorldTimerManager().SetTimer(buffTimer, this, &UBuff::OnTick_Implementation, tickRate, true);
   }

   owningTank->activeBuffs.Add(this);

   GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Blue, "Buff Created.");
}

void UBuff::OnTick_Implementation(){
   GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, "Buff Ticking.");
   timeActive += tickRate;
   if(timeActive > lifeSpan){
      this->OnDestroy();
   }
}

void UBuff::OnDestroy_Implementation(){
   GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Destroy Called.");
   GetWorldTimerManager().ClearTimer(buffTimer);
   owningTank->activeBuffs.Remove(this);
   Destroy();
}

I get compiler errors saying that ‘GetWorldTimerManager’: identifier not found. I looked at the doc and this function is defined in AActor. So are UObjects not able to deal with timers? How do I build a data only object that has a tick function?

Since youre using an Event delegate instead of a function you may need to set a TimerHandle reference from the output of your SetTimer to be able to clear it. Generally speaking setting a timer to 0 time and not looping will also clear it but this takes the extra step of invalidating the handle.