Creating a copy of a struct not a reference?

Hi Everyone,

Silly question, but I’ve got an Interactable Actor. It contains a Struct of various variables. Once I pickup the actor, I want to extract the struct and save it to an array “Player Inventory” on my player. After that, I no longer need the Interactable Actor so want to destroy it.

However, when I add the struct to my array I think it’s saving a reference to the original struct on the Interactable Actor as I get an error when I try to kill the Interactable Actor and it can no longer fetch the struct info from my array (gives a warning saying I’m trying to get it from an actor due to be destroyed). I’m just taking the struct and using “ADD” to add it to my array. I thought this would add a new struct to my Player Inventory array, not reference the original struct on my Interactable Actor.

image

So, how do I create a new struct and load the struct information into my array without it referencing the original actor I pulled it from?

I’m using blueprints, not c++.

Thanks so much!!

That bottom input instead of putting the struct in there pull off the pin and you should be able to make a struct and pass in the values from the picked up one with referencing it

1 Like

you can take the reference from the Pickup and assign by on the Add node for the Array, you could “break the Struct pin” (right click on any node that returns or receives a struct and it can be broken out into its pieces)
then you are technically adding a “new” instance of the struct.
and the Reference to the original Array is irrelevant because it will just fall out of scope when the event/function ends, or the other actor stops existing (which ever comes first)

1 Like

Thanks guys! (@Spacemarine658 , @gardian206 )

I think you might be suggesting this (see pic)…

I actually found the issue. I was passing through a static mesh component, not a static mesh which must have needed to keep a reference to the actor it was attached to (you learn something new every day!).

Turns out, I don’t actually need to break and make the struct as long as I pass the static mesh through and not the static mesh component.

Thanks for the responses!!!

1 Like