TFunction - passing reference to a function - cant get it to work

Has anyone ever gotten this to work? I’ve looked through all the available questions and examples on the internet & forum, but it stubbornly refuses

I wish to replace a long switch statement that selects functions after a timer has ended, which im doing using a hidden stored parameter

header…

UFUNCTION()
void PlayCutScene(FName TheCutSceneReference,  int32 TheResumeFunctionReference);

implementation in timer callback using stored hidden parameter…

	switch (MyFunctionResumeIndex)
	{
	case 0:
		REF_REF->GetLevelController()->ResumeAfterCutScene();
		break;

	case 100:
		break;
	}

with something like this - I Understand from research that this cannot be a UFUNCTION here ???


void PlayCutScene(FName TheCutSceneReference,  TFunction<void()> TheFunctionReference);

just so I can pass in the function as a reference - mainly for readability and as an exercise to improve my coding skills. This is easy in standard c++, but not unreal c++

Then I can call the function such as


PlayCutScene("INTRO", &LevelController::ResumeAfterCutScene);
1 Like

I am currently working on getting this too work.

I want to pass callback functions to an UAsyncLoader along with the request using a FDataObject, and in the callback I want to include the thing that was loaded. I will also be potentially having multiple of these so callbacks for after the AsyncLoad() would be beneficial.

one place I intend to use these is in my inventory system so that I don’t have dozens to hundreds of unique blueprints floating in memory

for this I have an FItemDef think of this a just a USTRUCT these work as expected

USTRUCT(BlueprintType)
struct FItemDef
{
    int32 ItemID = 0;
};

then I have an inventory UInventoryComp : public ActorComponent that is each going to be calling my UAsyncLoader : public UGameInstanceSubsystem which is responsible for the AsyncLoading calls to AssetManager, and keeping track of when these can be released. the Loaded item for this will be

UCLASS()
ASpawnableItem : public AActor`
{
// ...
public:
    FItemDef ItemDef;
// ...
};

Items will be spawned through a combinations of DataTables, and TSoftClassPtr<> these are working fine.

the Inventory has the function to be invoked after the load

UFUNCTION()
void ReceiveAsyncLoadedItem(ASpawnableItem* LoadedItem);

the AsyncLoader is supposed to have a function

// "this is where I get the error."
// UFUNCTION()
void LoadItemAsync(const FItemDef& itemToSpawn, TFunction<void(ASpawnableItem*)>& Callback);

the error VS throws is

Error : Unable to find 'class', 'delegate', 'enum', or 'struct' with name 'TFunction'

I even went so far as to try and #include "Templates/Function.h" which does not satisfy VS

1 Like