Increment of 0 by 5 equals 5 sometimes 10. Is this a race condition problem? How can I solve this?

Hey, glad you got it working.

As for the question about initialization: it’s rather complicated, but let me explain a couple of things that might help.

The first thing is that compile time initialization requires that all data is known at compile time; this is rarely feasible in a majority of situations. Even if you put default values in a default constructor in C++, that still is going to require runtime initialization, and this initialization would occur when the constructor is invoked. In most scenarios, some runtime initialization cost is going to exist. Whether or not that cost is acceptable is going to depend on your particular context and will require measurements and analysis via profilers.

Secondly, because USlotStorage is a UObject, it must be dynamically allocated on the heap since it is going to be traced in Unreal’s garbage collection system. This is why you can’t, for instance, have a member field of type USlotStorage (instead of USlotStorage*), because that would embed it directly in the owning class’s memory and would escape the call to NewObject that properly registers it with garbage collection system. In any situation, if you are using a UObject, it is going to need to be initialized via NewObject at runtime.

Now, in your situation you won’t have lost any performance; CreateDefaultSubobject is also going to end up calling NewObject (or an equivalent) for the target as well. But it’s not appropriate for things that are not serialized (transient); it’s really just meant for components and subobjects that are integral to the “structure” of the UObject; things that are meant to be visible in the Blueprint visual component hierarchy, and thus must be serialized.

1 Like