Hello,
How can i add a static mesh within a loop with c++
I keep getting crashes when i try to loop & add a mesh at runtime. Here is the code im using
Hello,
How can i add a static mesh within a loop with c++
I keep getting crashes when i try to loop & add a mesh at runtime. Here is the code im using
Are you doing this in constructor or at run time? Its super stupid but unreal has different calls to create and attach components depending on that.
In constructor, create, attach, using:
newComponent = CreateDefaultSubobject<T>(FName(*unrealUniqueName));
newSceneComponent->SetupAttachment(attachTarget, FName(*attachSocketName));
Not in constructor, create, attach, using:
newComponent = NewObject<T>(actor, FName(*unrealUniqueName));
newSceneComponent->AttachToComponent(attachTarget, FAttachmentTransformRules::SnapToTargetIncludingScale, FName(*attachSocketName));
BTW, you can detect if “in constructor” or “in runtime” like this:
bool GetThreadContextIsContructor()
{
FUObjectThreadContext& ThreadContext = FUObjectThreadContext::Get();
if (ThreadContext.IsInConstructor > 0)
{
return true; // is in constructor
}
else
{
return false;
}
}
bool GetThreadContextIsRuntime()
{
FUObjectThreadContext& ThreadContext = FUObjectThreadContext::Get();
if (ThreadContext.IsInConstructor > 0)
{
return false; // is not in constructor
}
else
{
return true;
}
}
So then you could make a wrapper function that automatically uses the correct Create or Attach functions.