[C++]How to return more than two variables from a function?

I have a function that should take two arguments and return the other two (different types). How can I return them so that I can use them in other C++ functions?

Make a return type of TTuple<var1,var2,ā€¦>
Where vars can any exposed unreal variable types

for example

TTuple<float,bool> AMyCharacter::DoSomething()
{
  float myfloat_value = 1.0f;
  bool mybool_value = false;
  return MakeTuple(myfloat_value,mybool_value);
}

Thank you so much!!!

A more ā€œtraditionalā€ way of doing that is using a regular return value, and an Out param, although frankly, for exactly two values, I feel like using a Tuple is far more readable with the intent.

bool AMyCharacter::DoSomething(float& OutFloat)
{
    OutFloat = 1.0f;
    return false;
}

Youā€™ll see that pattern all over Unreal, bringing it up so you recognize it. IMO the Tuple is much cleaner, though. Some might argue that creating a new data structure is unnecessary, so donā€™t, though. But unless itā€™s a critical performance area, Iā€™d go with the Tuple.

[Error]Unrecognized type ā€˜TTupleā€™ - type must be a UCLASS, USTRUCT or UENUM
Why I Cant UFUNCTION?

UFUNCTION(BlueprintCallable)
TTuple<bool, int32> AddToInvenoty(FName ItemID, int Quantity);

TTuple is not exposed to blueprints

.h

USTRUCT(Blueprintable)
struct FInventorytStruct
{
	GENERATED_BODY();

	UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
	bool Added;

	UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
	int32 index;
};



	UFUNCTION(BlueprintCallable)
	void AddToInvenoty(FName ItemID, int Quantity, FInventorytStruct& outValue);

.cpp

void AMyCharacter::AddToInvenoty(FName ItemID, int Quantity, FInventorytStruct& outValue) {

	outValue.Added = true;
	outValue.index = 22;

}

add to inv
(after right clicking spit struct for output)

1 Like

And how i can use this is struct on my cpp funcs

Either put it in a separate header and include it or put it at the top of your header file if itā€™s just used with 1 class (has to be above the uclass definition)

I mean how to use the value that the function will return if I pass it to &FStructName?

inside the function when you set the properties of the struct, they will be returned by reference.

Use these return values.

Thank you very much, I just realized that Iā€™m very stupid

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.