Struct being destroyed

I am writing a RPG system in which you attach components to the player (Composition based design).

I have created a few components including, level, skills and quest components.

The way I have it at the moment, the component contains a TArray of structs to hold data about each quest etc…

However the objects held in the TArray are being destroyed, which is not what I want. Normally I would use a pointer to manage the lifetime of my object however I cannot have a TArray with pointers to my structs.

What can I do?

Thanks

As far as I know, structs are not garbage collected - that’s why you generally require an implementation for “Destroy()” on them. In UE4 they are intended to mostly be used as value types (e.g. FVector).

I do recall that for some reason it’s not safe to point to a struct belonging to another object - not sure if this helps.

Do you have some more information on exactly what is going wrong?

If the structure in the array is a plain C++ structure (not inheriting from UObject), they will be destroyed when the owning class is destroyed.

If they inherit from UObject (which I guess is the case, otherwise I don’t see how it is possible they are destroyed just like that), they will be garbage collected automatically, provided you put the UPROPERTY() macro on top of the array.

It would help to see some code to better understand your problem, and try to answer it the better way.

The problem was actually to do with the for loop.

when I did for (Objecttype obj : m_objects) the destructor is called on each object. Simply changing it to a ranged for loop stopped the isaue.

is this how the c++ implemention is or what?

Thanks

That’s normal then, it’s the default C++ behavior.

Using such a loop for (Objecttype obj : m_objects), you copy the objects, thus calling copy constructor and destructor on Objecttype.

I advice you to read a good book about C++, because you will encounter a lot of issues ^^

Or use blueprints :slight_smile: