Passing Values into AActor Constructor

Hey,

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;

}

Hello!
We need more details…

At first sight it could be something like:

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?

1 Like

Resource is a custom UCLASS that is created from a factory. Inheriting directly from UObject.

I don’t think you can change the function signature of that constructor because it is generated by UE4.

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.

Lol right! I’m just a noob!

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!

This can be interesting for you… 4.7 How do I actually pass parameter's in the new constructor format? - C++ - Epic Developer Community Forums

You can use the function

virtual void OnConstruction(const FTransform& Transform) OVERRIDE;

It should work like Bp construction scripts and allow you to edit variables and place components that will be visible in the editor.

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.

This is called after the member constructor though right?

Yes I believe it is

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!