How do i pass parameters into FTimerDelegate ?

I have this piece of code:



	bool b_rached_target = false;

	FTimerHandle timer_handle;
	FTimerDelegate timer_delegate;

	timer_delegate.([this]()
	{
		if (b_rached_target)
		{
			GetWorld()->GetTimerManager().ClearTimer(timer_handle);
			print("Reached target!");
		}
		else
		{
			animInstance->current_animation = EEnemy_Animations::WalkCycle;
			enemy_controller->MoveToActor(target);
			print("Moving...");
		}
	});

And it says:

Error 4 error C3493: ‘b_rached_target’ cannot be implicitly captured because no default capture mode has been specified C:\OldSchoolNightmare\Source\OldSchoolNightmare\Enemy_Character.cpp 37 1 OldSchoolNightmare

And:

Error 5 error C3493: ‘timer_handle’ cannot be implicitly captured because no default capture mode has been specified C:\OldSchoolNightmare\Source\OldSchoolNightmare\Enemy_Character.cpp 39 1 OldSchoolNightmare

I have no experience with , but here are some examples of how to set a timer with parameters by putting a member function plus arguments into a FTimerDelegate:

Thanks, it seems like i will have to use global variables which is pretty sux… :\

You don’t need global variables. If you move the lambda function you have right now to a member function and use the examples in the thread I linked, you can wrap the function call + arguments in a timer delegate and call that function with arguments after a delay. No globals needed.

So then i need to make a new function in the header no?..

In your lambda, replace [this] with =]. This will capture (by copying) all local variables into the lambda. Note that this is just c++ lambda syntax and not specific to UE4 delegates.

It’s not clear what you’re doing with your bool variable here, but it seems like it should probably be a class member rather than a local, since you want to be able to test its value at a later time, when the timer is triggered, I assume.

Thanks! That solved it! but by copying it and not reference it, i guess i will have to declare the bool as global, but will it clear the handle to stop the timer even if it copying it ? because i can’t make the handle as global, it says there is no such a type, even when i include the TimerManager.h in the top of the header ( Why is it doing that ? )

&b_reached_target]

Would that do what you want?

Something else, though, has anyone gotten [this] to work? I was having compiler issues when trying to access my object’s variables in the lambda using [this].

EDIT:
Hold up, if b_reached_target is a variable within this function, it will be destroyed when scope is left. So you wouldn’t want that passed by reference, else it will be a reference to garbage.

After managing my code some, the final thing i need is to know how to make a global variable of type FTimerHandle so i can clear the timer from within the timer’s loops, is it possible ?

Btw adding the “this” worked for me, it just let’s you access global variables normally, you don’t have to write “this->var”, just “var”.

You didn’t get this error?

> error C3490: ‘var’ cannot be modified because it is being accessed through a const object

Nope, i don’t remember if i tried to modify something though, maybe i was just reading some value…