Now, we derive two classes from Character class: EternalPlayer and EternalBot. Somewhere after the StatComponent creation in Char’s constructor, the StatComponent gets destroyed (presumably by Garbage Collector). Below are two screenshots from debugging, each is in several milliseconds after another, theoretically.
I’m experiencing troubles with the GC too.
It collects my object (saved via a UPROPERTY), as well as the MovementComponent (the default one, not a custom one).
To prevent component from being garbage collected in this situation, you can use RegisterComponent() function:
StatsComponent->RegisterComponent();
Please note, that it should be called in the appropriate context, which means that “this” points to the owning Actor, that creates the component.
Thus, RegisterComponent() provides correct performance only when “this” pointer is valid and has a valid world associated with itself, which works for most Actors.