Hi, this is sillly, but I think that something is odd with how pass by reference is treated in latent commands.
Consider this test:
DEFINE_LATENT_AUTOMATION_COMMAND_ONE_PARAMETER(FLatentCommandCounterCommand,int&, latentCounter);
bool FLatentCommandCounterCommand::Update()
{
latentCounter += 1;
GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Orange, FString::Printf(TEXT("latentCounter: %d"), latentCounter));
if(latentCounter > 5)
{
GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Green, FString::Printf(TEXT("latentCounter reached: %d"), latentCounter));
return true;
}
return false;
}
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FLatentCounterTest, "tests.LatentCounterTest", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)
bool FLatentCounterTest::RunTest(const FString& Parameters)
{
int latentCounter = 0;
ADD_LATENT_AUTOMATION_COMMAND(FLatentCommandCounterCommand(latentCounter));
return true;
}
I pass an int as reference and the latent command should modify that variable each frame until it reaches 6, because we are modifying the real variable, not a copy.
The problem is that it doesn’t. It looks like it treats as if we were working with addresses when we run it:
But it should behave as if we were working with pointers:
DEFINE_LATENT_AUTOMATION_COMMAND_ONE_PARAMETER(FLatentCommandCounterCommand, int*, latentCounter);
bool FLatentCommandCounterCommand::Update()
{
*latentCounter = *latentCounter + 1;
GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Orange, FString::Printf(TEXT("latentCounter: %d"), *latentCounter));
if(*latentCounter > 5)
{
GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Green, FString::Printf(TEXT("latentCounter reached: %d"), *latentCounter));
return true;
}
return false;
}
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FLatentCounterTest, "tests.LatentCounterTest", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter)
bool FLatentCounterTest::RunTest(const FString& Parameters)
{
int* latentCounter = new int{0};
ADD_LATENT_AUTOMATION_COMMAND(FLatentCommandCounterCommand(latentCounter));
return true;
}
Which results in the desired behaviour:
Passing by reference would be nicer because I shouldn’t have to use ‘*’ and it doesn’t have a memory leak like the pointer implementation.
I don’t know, maybe I’m tired, but shouldn’t these two tests reach the same result?
Thanks!