How to properly define output parameters?

Hello community!

I have been trying for days to properly define output parameters now. I am new to C++ and therefore I think I am just missing something very simple :D. This is my blueprint function, which I am now trying to rewrite in C++:

As you can see it has three input parameters and three output parameters.

Now this is the definition form my function in the .h file:

	UFUNCTION(BlueprintCallable, Category = Utilities)
	void GetClosestOverlappingActor(const UClass* ActorClassIN, const AActor* RelativeActorIN, const USphereComponent* CollisionComponentIN, class AActor* ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, UClass* ActorClassOUT);

This is the .cpp implementation:

void AActorUtilitiesCode::GetClosestOverlappingActor(const UClass* ActorClassIN, const AActor* RelativeActorIN, const USphereComponent* CollisionComponentIN, class AActor* ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, UClass* ActorClassOUT)
{
	//Local varriables:
	const AActor* LocalRelativeActor = RelativeActorIN;
	float ClosestDistanceToActor = 0;
	int32 ClosestActorIndex = 0;

	// Get all overlapping actors
	TArray<AActor*> OverlappingActors;
	CollisionComponentIN->GetOverlappingActors(OverlappingActors, ActorClassIN->GetClass());

	if (OverlappingActors.Num() > 0)
	{
		// If we have at least one appropriate actor then we have to find out which one is the closest
		for (int32 iOverlapping = 0; iOverlapping < OverlappingActors.Num(); iOverlapping++)
		{
			// When we enter the forEach Loop, we set the very first actor here 
			if (ClosestDistanceToActor == 0)
			{
				// Sets thee actor index in for loop and sets the distance to the first element found.
				ClosestActorIndex = iOverlapping;
				ClosestDistanceToActor = RelativeActorIN->GetDistanceTo(OverlappingActors[iOverlapping]);
			}
			else
			{
				if (RelativeActorIN->GetDistanceTo(OverlappingActors[iOverlapping]) < ClosestDistanceToActor)
				{
					ClosestActorIndex = iOverlapping;
					ClosestDistanceToActor = RelativeActorIN->GetDistanceTo(OverlappingActors[iOverlapping]);
				}
			}
		}
		ClosestOverlappingActorOfTypeOUT = OverlappingActors[ClosestActorIndex];
		IsAnyActorOverlappingOUT = true;
		UClass* ActorClassOutTemp = OverlappingActors[ClosestActorIndex]->GetClass();
		ActorClassOUT = ActorClassOutTemp ->GetClass();
	}
	else
	{
		IsAnyActorOverlappingOUT = false;
	}
}

Now the problem is when I am calling the function in BP the parameters “ActorClassOUT” and “ClosestOverlappingActorOfTypeOUT” are not output parameters. How can I achieve that? Here just for visualization the BP which I am calling:

Inside my code files the parameters are marked with “IN” and “OUT” to show you which ones I want to be in- and output.

BTW: I was thinking about a “Transport”-Struct to transport my parameters out of the function but that is just an idea and I believe there must be an easier way :smiley:

Thanks in advance for your help!

Greets Linus

Out parameters you define with ‘&’ placed right behing their types (you did that with bool param). Keep in that you have to put all in parameters must be placed before any out parameter.
As for variables that store class, you use TSubclassOf instead. ← It turns out that UClass* works as well, my bad! :S

So, here’s how your header should look like:

UFUNCTION(BlueprintCallable, Category = Utilities)
	virtual void GetClosestOverlappingActor(const UClass* ActorClassIN, const AActor* RelativeActorIN, const USphereComponent* CollisionComponentIN, AActor*& ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, UClass*& ActorClassOUT);

And here’s .cpp:

void AActorUtilitiesCode::GetClosestOverlappingActor(const UClass* ActorClassIN, const AActor* RelativeActorIN, const USphereComponent* CollisionComponentIN, AActor*& ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, UClass*& ActorClassOUT)
{
    // stuff
}

Wow thanks for that really fast reply! That makes sense I was trying out similar approaches before but syntax is not my strongest skill.

I replaced the .h and .cpp lines with your code. It looks like this now:
.h:

virtual void GetClosestOverlappingActor(TSubclassOf<AActor> ActorClassIN, AActor* RelativeActorIN, USphereComponent* CollisionComponentIN, AActor*& ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, TSubclassOf<AActor>& ActorClassOUT);

.cpp:

void ActorUtilitiesCode::GetClosestOverlappingActor(TSubclassOf<AActor> ActorClassIN, AActor* RelativeActorIN, USphereComponent* CollisionComponentIN, AActor*& ClosestOverlappingActorOfTypeOUT, bool& IsAnyActorOverlappingOUT, TSubclassOf<AActor>& ActorClassOUT)
{

};

Unfortunately I am now getting an error that says:
\ActorUtilitiesCode.h(11): error C2664: ‘void AActorUtilitiesCode::GetClosestOverlappingActor(TSubclassOf,AActor
*,USphereComponent *,AActor *&,bool &,TSubclassOf &)’ : cannot convert argument 6 from ‘UClass *’ to ‘TSubclassOf &’

Did I miss something?

The problem was here:

UClass* ActorClassOutTemp = OverlappingActors[ClosestActorIndex]->GetClass();
ActorClassOUT = ActorClassOutTemp ->GetClass();

You were trying to convert UClass* to TSubclassOf

Unfortunately I don’t know how to do such thing. So, instead of this I tried to use UClass* instead (as you wanted to in the first place) and it turned out that it works normally. My bad! :stuck_out_tongue:

I’ve fixed my answer above. The code should work properly now.

Thanks a lot for you help! Works perfectly!