# Setting Timer in ExecuteTask()

**URL:** <https://forums.unrealengine.com/t/setting-timer-in-executetask/417062>\
**Category:** C++\
**Tags:** task, timer, unreal-engine, UE4, CPP, question, set-timer\
**Created:** [February 12, 2018, 4:07am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062 "2018-02-12T04:07:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jr557](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jr557](https://forums.unrealengine.com/u/jr557)\
**Post date:** [February 12, 2018, 4:07am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062/1 "2018-02-12T04:07:44Z")

</div>

I’m writing a task in C++ and I need to set a timer in `ExecuteTask()` which calls `Beefy()` after 4 seconds:

```
EBTNodeResult::Type UMyTask::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
  ...

MyPawn->StartWork();

// TODO:
// Set timer to call UMyTask::Beefy() after 4 seconds
// UMyTask::Beefy() needs OwnerComp to call FinishLatentTask() and MyPawn* to call MyPawn->FinishWork()

// Task not done yet!
return EBTNodeResult::InProgress;

}

void UMyTask::Beefy(UBehaviorTreeComponent& OwnerComp, APawn* MyPawn)
{
  MyPawn->FinishWork();

  // Task is done now. Return success.
  FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
}

```

Could anyone give a working code example that shows how this is usually done?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [February 12, 2018, 4:21am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062/2 "2018-02-12T04:21:29Z")

</div>

First you need to have world instance to access timer manager. You can get it from MyPawn.

Next you need to delere timer handle (FTimerHandle), without it you won’t be bale to control the timer after you start it. You can declere it in class, but if you don’t plan to control timer you can declere it locally.

Then you call SetTimer from Timer Manager, first parameter is handle you gonna use, 2nd is object on which function will be called, 3rd is a function pointer to function you want to call when time will be up, 4th amount time, 5th if timer should loop.

So it should like this:

```
FTimerHandle Handle;
MyPawn->()->GetTimerManager().SetTimer(Handle,this,&UMyTask::Beefy,4.0,false);

```

Study API refrence opf timer manager to see other function which you can use to control timer:

> **[FTimerManager](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/FTimerManager/)**
>
> Class to globally manage timers.

---

<div class="post-metadata">

**Author:** ![jr557](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jr557](https://forums.unrealengine.com/u/jr557)\
**Post date:** [February 12, 2018, 4:40am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062/3 "2018-02-12T04:40:37Z")

</div>

My trouble seems to be getting the 2 arguments into Beefy, particularly the OwnerComp. I have tried something like this as suggested by this answer:

> **[Using SetTimer() on a Function with Parameters](https://forums.unrealengine.com/questions/165678/using-settimer-on-a-function-with-parameters.html)**
>
> I am currently trying to implement a method that calls 2 functions. The catch is I don’t want the second one to run until the animation of the first has finished playing. I am trying to unequip a weapon then equip a new one. Each weapon in the...

```
FTimerHandle TimerHandle;
FTimerDelegate TimerDel;
TimerDel.BindUFunction(this, FName("Beefy"), OwnerComp, MyPawn);
()->GetTimerManager().SetTimer(TimerHandle, TimerDel, 4.0f, false);

```

But I get an `error C2248: 'UBehaviorTreeComponent::UBehaviorTreeComponent': cannot access private member declared in class 'UBehaviorTreeComponent'.` and OwnerComp has the red squiggly lines under it.

Could you show an example of calling FinishLatentTask() in Beefy so I could see the setup?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [February 13, 2018, 1:21am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062/4 "2018-02-13T01:21:53Z")

</div>

Thats because because it’s a reference, try sending pointer with `&OwnerComp` if it still complasins it’s private then you need public varable in class (which need to be pointer too)

Make sure that OwnerComp whatever it is is not gets deleted in 4.0s, otherwise you will end up with invalid pointer and sure crash. in worse scenerion you will need to use IsValidLowLevel

---

<div class="post-metadata">

**Author:** ![SuryaPrakashModi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuryaPrakashModi](https://forums.unrealengine.com/u/SuryaPrakashModi)\
**Post date:** [May 21, 2021, 8:02am UTC](https://forums.unrealengine.com/t/setting-timer-in-executetask/417062/5 "2021-05-21T08:02:42Z")

</div>

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/b/3/ab3b08d0b3e483c5f9c2cfb15b43ac9f7173a94a.jpeg "How to use Set Timer And Clear Timer in C++ Unreal Engine") ](https://www.youtube.com/watch?v=Asw98YKAOtg)

watch this
