Trying to Return Structure Variable as Reference

Put simply instead of doing this:

I want a function that returns that variable for me, just like this:

The issue is, by making it a function it returns a copy and not a reference.
Any ideas on how to fix this?

  • I’ll take C++ fixes aswell

A macro would work, I expect. Otherwise the standard C++ function should work, but I’ll let someone else tell how since I haven’t done much C++ with UE.

in Unreal C++ Structs are “always” Stack Allocated references.
What Blueprints call “references” are C++ pointers (a big reason they call them references is to prevent C#/Java people from having a breakdown at the word “pointer”)

is this Player Data struct engine built or defined in your C++ or blueprints?
where is this Active Skills array defined?

my guess is that you are trying to get to a given Skill, or work with/on all active Skills; if this is the case there may be other more “safe” ways to go about what you are trying to accomplish.

as to the matter at hand there is no real “clean way” to accomplish this for USTRUCT types for a few reasons.

what you are trying to do is effectively copy the address of the struct, and put that as the address of the output struct; which is disallowed under the Unreal Header Tool particularly for UObject (the lowest level of thing that Blueprints can understand), and trying to get to the pointer of the struct is dissallowed as Unreal Header Tool error UHT001. Hence why you are getting a copy of the struct at the point of Break PlayerData and a separate copy of the array.

the core reason is that beyond not being able to check for null on structs, the other reason the engine wants to avoid having multiple things referencing the same address for Stack Allocated objects one of 2 equally valid outcomes could happen:

  • the Garbage Collector finds 2 objects pointing to the same memory address on the Stack and so as long as that linking exists neither object can have Garbage Collection performed on it
  • the Garbage Collector when told to destroy an object (or it has been flagged for some reason) just de-allocates it without checking the shared references, and now a Stack Allocated memory address is marked as “free” so it could hold relatively garbage (with a potential stealth access violation) making it untrustworthy.

which effectively breaks mechanisms and the point of garbage collection, if you want to dig in and modify the Unreal Header Tool to allow for this… good luck, as those above mentioned outcomes can “randomly” happen

it would be “safer” and more in line GC to peak at the current state, modify it, and then set the state as needed.
another option would be to make this Player Data a UObject or a Component instead of a struct, where you are freely able to have as many copies of the same pointer (blueprint reference) as you like.
if these Skills are UObject or Component then the array will be a copy, but the indices of the array will be pointers that will be the same as the original array.
Just by trying to get the array you will be getting copies of everything in it, you can’t really trust that the order of the output array will be the same as the order of the original array.