Set Members in Structure Not Saving

Hey helpful forum people, I’m trying to be clever by using a single function to be able to alter my player’s stats, however it’s backfiring on me.

My PlayerCharacter blueprint has a custom actor component (StatsComponent) that holds the stats in a structure (PlayerStats). Inside the StatsComponent is this function, which (I believe) selects the correct stat to alter using the GameplayTag, uses some local variables for ease, and (should) set the correct structure member using a switch.

However, what is actually happening is that when I try to affect the stat, the number only changes based off the default value (100 + 10 = 110 every time instead of 110, 120, 130 etc.), which lead me to believe that the structure isn’t being saved for some reason.

I’ve seen plenty of questions that point to pass-by-reference and the diamond node inputs but I can’t see how that helps me here with the structure already being within the actor component, although I’m probably missing something obvious.

Any help is greatly appreciated!

(Screenshot of linked function)

I’m short on time at the moment, but I found a post about a possible bug:

Struct Data Resetting to Default Values on Editor Restart - Development / Programming & Scripting - Epic Developer Community Forums

Assuming the issue isn’t code related, perhaps this will help.

its the age old copy vs ref problem, you’re mutating a copy.

click on the in pin and change it to PassByReference but you also cant use a local variable as that’s a copy.

you also need to make sure the in connection is a ref so usually you’d just use the variable itself.

in short its a pain in BP

2 Likes

(post deleted by author)

Ah okay, so it is the usual suspect…

There’s no point me changing the input float into a reference as that’s just the modifier, good to know about local variables too though.

Is there any way for me to use the structure variables themselves as references rather than copies then? (To the beginning of the function with the select nodes)

in BP no,

you can pass a ref but you cant get a ref. Don’t ask me why, its a nightmare :sweat_smile:

the only option you have is to pass the Object and get it directly.

or just return the modified value and set it on the owning object.

I’m sure there’s a very technical reason that we can’t do it in blueprints… probably… maybe :grin:

Thanks to your information though I’ve managed a workin (but much less elegant) solution:

A pure function returns the chosen stat’s floats:

And then a modified version of the original function uses those stat values to modify.

Strangely though, the set members nodes work as expected using copied variables and local variables in this case. So it seems to have issues with referencing and setting itself within the same function because all I’ve really done is split the function in two… unless I’ve missed something (again).

1 Like

its not that it didnt work before its just you modified a different variable.

in this case you modify the copy and set the original

in the previous case you modify the copy but didnt set the original

but yes your system is the standard blueprint approach,

you can even merge the functions, have a setter/getter and modifier so it does all of that in one node.

you also dont need to set local variable just return the struct pins directly.

another option if you expand this system is have a TMap<Tag,Attribute> and can just do find attribute