Why is this laggy in C++ but not in BP?

Hi there,

I am wondering about the performance of a code snipped that I have in C++ vs BP. I use it to change the location of an actor when the mouse moves along the y axis.

In my Level BP this looks like the following:

and in C++ the code in my PlayerController class is:

void AConfiguratorPlayerController::MouseY(float val)
	if (abs(val) > 0.f)
		TArray<AActor*> OutActors;
		UGameplayStatics::GetAllActorsOfClass(this, ABodyActorModel::StaticClass(), OutActors);

		FVector currentLocation = OutActors[0]->GetActorLocation();
		FVector newLocation = currentLocation;
		newLocation.Z = FMath::Clamp((currentLocation.Z + val), 100.f, 140.f);

I know that the function GetAllActorsOfClass ([defined here][2]) is rather slow but besides that, the outcome is different in BP and C++.
When I use the BP implementation, setting the actor location is very smooth and the actor moves with the mouse up and down. When using the C++ implementation, changing the location is not smooth but lags a bit and sometimes stops.

Am I missing something here? I wanted to implement everything in C++ so that I do not have to expose variables and stuff.

Well the big difference between the two is that the C++ is looping over every actor returned by GetAllActorsOfClass and the BP version only does the first element. So perhaps there are more being returned on the C++ side than you think there should be. I’d change either the BP to have the same forloop or remove the loop from the C++ side and see if you don’t get the same behavior both places after that.

The function which is called on the BP node is defined here. Using exactly this code in C++ (updated my question) does not result in any improvements.

And what about the for loop? Did you take that out of the C++ version?

Updated my question/code again. Now I am completely using the same BP node code in C++ but still BP is smooth and C++ not.

In the previous version I did not take out the foor loop in C++ because the foor loop is present in the BP version.

Technically the abs() call in your C++ code should be fabs() but I can’t see that making any diff in performance. I would guess that perhaps you have some other differences between the two environments outside of this one particular function.