Hi, is there anyway to have a c++ function return multiple values just like blueprint functions(also I’m not calling this function in blueprints)? Thanks in advance.
Hi ShadowSprite123,
No - just the one return type - just use Parameters with references, eg:
int32 MyFunction(int32& outRet2,int32& outRet3) {
outRet2=42;
outRe3=109;
return 1;
}
int32 ret1,ret2,ret3;
ret1=MyFunction(ret2,ret3);
Thank you so much! Just one more question one of the values I’m returning is a AActor* how would I set this one up??
There’s a couple of ways - seeing as you’re not using the method as a UFUNCTION, it’s proabably easiest to use a “pointer to pointer”:
int32 MyFunction(int32& outRet2, AActor** outActor) {
outRet2=32;
*outActor=MyActor;
return 1;
}
You can also wrap/group them in a dedicated struct/class and return that instead. That can typically produce code that’s cleaner and easier to read. Take a look at this for example would you prefer
bool DoSomething(int32& outResult) { ... }
or
TOptional<int32> DoSomething() { ... }
The TOptional
version states pretty clearly that it may return an int32 but its up to the caller to check and handle if it actually did. Internally TOptional
automatically tracks the bool whether it currently contains a value of the given type (int32 in this case). Whereas with the other version there is nothing that prevents you from using outResult
without first checking the return value itself.
You can also use TTuple
TTuple<int32, float, AActor*> GetData();
Ty for all the replies I’ll experiment with them all and see what fits. I appreciate the help:)
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.