Download

Is it possible to use a ufunction with return value as FTimerDelegate?

Header:



	DECLARE_DELEGATE_RetVal(bool, MyDelegateType)
	FTimerHandle MyTimerDelegate;
	UFUNCTION()
	bool MyFunction();


Code:



	MyDelegateType MyDelegate;
	MyDelegate.BindUFunction(this, FName("MyFunction"));
	GetWorld()->GetTimerManager().SetTimer(MyTimerDelegate, MyDelegate, 10f, false);


Causes a compiler error: C2664: Converting of “MyDelegate” in const “const FTimerDelegate &” not possible.

Is there another possibility to add a delegate with retval to a timer?

FTimerDelegate is declared in TimerManager.h with


DECLARE_DELEGATE(FTimerDelegate);

so it doesn’t seem it supports return values. The other overloads of


SetTimer()

do not support retValue delegates as well or did I miss something?

It’s not possible. To whom would the function return the value?
When you set a timer and execute a function depending on elapsed time, the program will continue to execute it’s code. And by the time the time elapsed, the context of the code might be anything. That’s why you can’t have a return value for timer functions (and the same goes for events by the way). If you need to “return” a value, you should rather call another function that takes your return value as an argument.

Thanks! I came to the same conclusion and wanted to verify if my assumption is correct (I’m just working with UE4 for a couple of month in my freetime). I’ve already changed the design to work w/o return values for times (which made sense anyway).

Thanks also for the hint regarding events! :slight_smile:

You can create your own latent function instead of using a timer;
But I don’t know if there’s documentation on them and 'away from PC.

Ah ok, didn’t know latent functions/delays exist. There is a 3rd party tutorial and several topics at Q&A covering this, e.g. one about performance of delays vs timers (however opinions only w/o measurements).

No official documentation it seems and answers are mostly related to Blueprints instead of C++.