Download

Can structs be passed by reference?

By default, it seems like structs in ue are meant to be passed by value, effectively copying the entire class and all of its data. In my case, I have a data struct that contains everything an NPC needs to function like HP, statistics, inventory, and so forth. I have a centralized array of every CharacterData struct in the game, and when a character is spawned into the world I attach a copy of their data struct to their ACharacter subclass, so the game can do things like process damage or skill checks against them by casting to AMyCharacter and looking for that specific NPC’s CharacterData.

Right now I handle this by passing a copy of the CharacterData struct from the central array to the instance of AMyCharacter created when the NPC spawns, applying any changes to the local version of the struct, then passing it back to the central array when the character despawns. Is there any simple way I can skip this, and simply keep a single, central array of structs that each ACharacter contains a pointer to?

You should be able to treat them like any other Variable AFAIK, no reason why not.

Obviously there are no restrictions if you’re working in C++. Are you talking entirely blueprints? Or you need it exposed to some degree to blueprints?

Edit: Okay so just realised this is in the C++ forum. :wink: So why do you say structs are meant to be passed by value? If you want to just hold a pointer, then hold a pointer. Alternatively, you could use some kind of ID to reference entries in your centralized array. That way you could also expose blueprint access if needed.

Hmm, okay… why is it then that this is okay:



FMyStruct someStruct;

But this produces an error, “Inappropriate * on variable of type FMyStruct”?



FMyStruct* someStruct;

This is why

Thank you :slight_smile: