Bind lambda in struct

Hi all, so problem statement; I have a struct A, which contains a TArray of struct B. Struct B has a multicast delegate OnStatusChanged, and in struct A’s constructor, I’m attempting to bind a lambda to the OnStatusChanged delegate belonging to each struct B in the TArray.
There are no errors or anything thrown when compiling or running the bind, but when it’s called, the lambda doesn’t appear to be executing.
I suppose the question becomes - can structs bind to and execute lambdas? Is there something I’m perhaps missing?

explicit FMM_Mission()
	{
		FTimerDelegate TimerDelegate;
		FTimerHandle TimerHandle;
		TimerDelegate.BindLambda([&]
		{
			for (FMM_Objective Objective: Objectives)
			{
				Objective.OnStatusChanged.AddLambda([&]
				{
					GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Yellow, TEXT("Lambda running"));
				});
			}
		});
		if (GEngine && GEngine->GetWorld())
			GEngine->GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 1.f, false);
	}

Is this a constructor? Are you sure the engine and world are present. Try putting a UE_LOG (or set a breakpoint) under the if to see if SetTimer is actually getting called.

Actually the FTimerHandle and FTimerDelegate are local variables, that doesn’t look right either. I think they should be data members.

Yeah I ended up finding that GEngine was never valid, so I created a function on the struct that would be called by an actor which holds the mission struct sometime after the game started, which would solely attempt to bind a lambda with:

void FMM_Mission::StartMission()
{
	for (FMM_Objective Objective: Objectives)
	{
		Objective.OnStatusChanged.AddLambda([&]
		{
			UE_LOG(LogTemp, Warning, TEXT("Hello lambda"));
		});
	}
}

Though the lambda still does not execute on the struct…

You work on copies. Add & to modify original object instead.

for (FMM_Objective& Objective : Objectives)
1 Like