Is a return type allowed for a timer delegate?

Hello all,

I am trying to have a timer delegate set-up with a method that has one parameter and a return type: ACharacter* AEnemySpawner::SpawnEnemy(const bool IsVisible).

This is set-up as follows: const FTimerDelegate EnemySpawnDelegate = FTimerDelegate::CreateUObject(this, &AEnemySpawner::SpawnEnemy, true); GetWorldTimerManager().SetTimer(SpawnTimerHandle, EnemySpawnDelegate, SpawnDelay, false);.

However, an error occurs on the line where the FTimerDelegate is declared:

error C2665: 'TDelegate<void (void),FDefaultDelegateUserPolicy>::CreateUObject': none of the 2 overloads could convert all the argument types

note: could be 'TDelegate<void (void),FDefaultDelegateUserPolicy> TDelegate<void (void),FDefaultDelegateUserPolicy>::CreateUObject<AEnemySpawner,bool>(const UserClass *,void (__cdecl AEnemySpawner::* )(bool) const,bool)'

  DelegateSignatureImpl.inl(299): [C2665] or       'TDelegate<void (void),FDefaultDelegateUserPolicy> TDelegate<void (void),FDefaultDelegateUserPolicy>::CreateUObject<AEnemySpawner,bool>(UserClass *,void (__cdecl AEnemySpawner::* )(bool),bool)'

There is also another error brought up by the IDE I’m using (Rider):

To clarify, I would like the Character pointer returned, to be used in a method of another class. The timer is currently used in the AEnemySpawner class to handle spawning of enemies, soon after play begins.

Please let me know where I have gone wrong here, if a return type is allowed in a timer delegate, and if not, what the preferred solution would be, to refer to a pointer initialised in the method of another class.

Please also let me know if you require any further details, to help me resolve this problem.

Sounds like you are overcomplicating things. The most common way to do this is to create a “managing” class to spawn characters, which broadcasts a delegate with the character pointer as argument each time one spawns. Any spawned character could listen to the delegate of the manager to be notified when another character spawns, retrieving that pointer in the process.

There are other ways to get creative by passing a reference to a pointer into a method bound to the timer but it’s normally not the way to do it.

A timer delegate can’t have a return type because the delegate will be called somewhere in the timer manager code, which wouldn’t know what to do with it. The SpawnEnemy function will be called on your EnemySpawner object by the timer manager, inside that function you can create a new actor and do anything you want with it. Think about calling GetWorldTimerManager().SetTimer(SpawnTimerHandle, EnemySpawnDelegate, SpawnDelay, false); as the same as calling SpawnEnemy(false) directly, just that in the first case it will be delayed.

Ah, thanks for the advice* Roy. Yes, I will want to look into a way to have a class which broadcasts a delegate with the character pointer as an argument each time one spawns.

Ah, thanks for answering my question Fluppi393, the reasoning makes sense too.

I will have to refactor this class and its interactions with other classes, to get it to work the way I would like.

Considering that a timer delegate cannot have a return type (thanks ), and.suggestion for a ‘manager’ class, I have chosen that as the solution to my problem (implementing such a class, and having my code refactored appropriately)*. Thanks