Is returning a struct worse than returning many parameters? Why?

Official Doc Said, : https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/Blueprints/TechnicalGuide/Guidelines/

“Favor functions with lots of return parameters over functions that return structs.”

I prefer to use struct to manage many parameters.
But after I read that document, I got worry without reason.
(I just guess that it is about performance, maybe?)
Is there someone would explain to me about this, please?

1 Like

I’m guessing just as you are but I would be surprised if performance is the primary reason. Sure, there’s memory overhead not just because you’re using an additional struct to carry the variables you’d have anyway, but also because a struct has to marked as Blueprint type in order to work with Blueprints, right? It can’t be a plain old struct.

My guess would be ease of use. You don’t want users of your code to have to pull out the struct pin, break on it and get the variable instead of just accessing the variable directly on the BP node. Other than that I’m clueless. When working in C++, I do it just like you do, using structs to act as “payload” parameter when there are a large number of variables to carry around.

1 Like

I would not expect a significant performance difference. The memory usage by struct metadata is negligible.

This page mostly discusses API design. When you return many variables, BP users can immediately hook up wires to go where ever, while a struct would need to be split first.

1 Like