I’m trying to create a dynamic amount of components in my constructor based off of a resource. The issue is that there seems to be no way for me to set that resource value by passing it into the constructor.
It’s important for me to have these components created in the constructor because I would like their values editable in the editor and to be visible in the viewport.
If anybody knows how to properly achieve this functionality I would be very appreciative.
Thanks
AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
if (IsValid(Resource))
{
for (int i = 0; i < Resource->ComponentResources.Num(); ++i)
{
MeshComponents.Add(CreateDefaultSubobject<UMyMeshComponent>(*Resource->ComponentResources[i].Name));
MeshComponents[i]->bEditableWhenInherited = true;
}
}
SetActorTickEnabled(true);
PrimaryActorTick.bCanEverTick = true;
}
AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer , const type& Resource/==> you no more need to check for is valid… Compiler will do it for you/)
: Super(ObjectInitializer)
what is exactly Resource? A TArray? And where it comes from?
The initialize function wont work for my purposes. I need “Resource” to not be null at the time of construction. Before construction the object doesn’t exist so I can’t assign the variable or call a setter. And so the only time I could call a function, like an init, is after the constructor, which would be too late.
I found you need an init() call inside your constructor… It’s not a good practice but seems to be the only way… I’m really impressed, I never liked macros!
I spent a lot of time looking into this, I truly think the only way to do this is with init(), I don’t think there’s any way to pass parameters through the constructor unfortunately. If you find otherwise I would love to know how.
If all you’re trying to do is have those components available in the viewport I believe you can initialize them in BeginPlay and they should be available in the viewport. That’s been my experience anyways.
Yes… No way… ue4 c++ is not really standard c++
In fact they don’t use STL, and ue4 add garbage collection, hashing and some features not really typical of standard C++
And it seems like they taken constructors for internal use of the engine… But this is reasonable, it’s not a good design to let clients put their hands on the core logics, but they provide clients all the methods,functionalities to add logic and integrate to the engine,editor.
So we better agree to what said in this post: " we need to change paradigm".
Sorry… I still feel uncomfortable too with this kind of c++ but the engine it’s not so bad!