Sending delegates as parameter

Hi! I’m pretty new in using delegates in Unreal and I’m kind of stuck. So I have a class where I defined a delegate and its constructor takes it in as a parameter: WorldGeneration.h

DECLARE_DELEGATE_TwoParams(FWorldGenerationCallback, TArray<Block*>&, MeshData*);

class PROCEDURALWORLD_API WorldGeneration: public FRunnable
	WorldGeneration(WorldStats* stats, FWorldGenerationCallback& callback);
//some other functions
	TArray<Block*> mGrid;
	WorldStats* mStats;
	FWorldGenerationCallback mFinishCallback;
	MeshData* mMeshData;


WorldGeneration::WorldGeneration(WorldStats* stats, FWorldGenerationCallback& callback)
	mStats = stats;

//delegate is used in some other function inside WorldGeneration class with .Execute
mFinishCallback = callback;

Now inside the other class I’m trying to create the WorldGeneration instance and send the function I want to be using as delegate GeneratedWorld.cpp

void AGeneratedWorld::LoadMeshData(TArray<Block*>& createdGrid, MeshData* meshData)
//function I want to send as delegate

void AGeneratedWorld::RequestWorldGeneration()
	WorldStats* stats = new WorldStats();
//more code

//I don't know how to send the LoadMeshData function
	WorldGeneration* worldGeneration = new WorldGeneration(stats, &AGeneratedWorld::LoadMeshData);

I know that in Unity you can just pass in the name of the function and it works normally… What am I doing wrong?

The way UE4 delegates work is that delegate is a variable type that holds function pointer that bind to it and then you can execute at any moment, so you need to something like this:

FWorldGenerationCallback Callback;
Callback.BindUObject(this, &AGeneratedWorld::LoadMeshData); //"this" is object that it will be called on
WorldGeneration* worldGeneration = new WorldGeneration(stats, Callback);

And then you execute callback, but keep in mind that delegate need to be in memory in order for it to be executed, local variable will be destroyed. So either declare delegate variable in class or instead of reference do the copy of it.

Read docs about delegates:

Asside of that… are you sure you want WorldGeneration to be outside of UObject class hirarchy? That class wont be visible by the enigne and won’t be GCed. Also by UE4 naming convention you should atleast prefix it by F so FWorldGeneration