x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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;
 
 }

Product Version: UE 4.18
Tags:
more ▼

asked Jun 14 '18 at 07:36 PM in C++ Programming

avatar image

Rynold
56 8 9 15

avatar image AZTECCO Jun 14 '18 at 09:52 PM

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?

avatar image Rynold Jun 14 '18 at 10:02 PM

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.

avatar image AZTECCO Jun 14 '18 at 10:31 PM

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.. https://forums.unrealengine.com/development-discussion/c-gameplay-programming/30940-4-7-how-do-i-actually-pass-parameter-s-in-the-new-constructor-format

avatar image Rynold Jun 14 '18 at 10:42 PM

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.

avatar image siditious Jun 15 '18 at 01:33 PM

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.

avatar image AZTECCO Jun 15 '18 at 09:02 PM

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".

https://heapcleaner.wordpress.com/2016/06/11/uobject-constructor-postinitproperties-and-postload/amp/

Sorry.. I still feel uncomfortable too with this kind of c++ but the engine it's not so bad!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Jun 15 '18 at 09:55 AM

avatar image

HarryHighDef
1.4k 2 14 17

avatar image siditious Jun 15 '18 at 01:34 PM

This is called after the member constructor though right?

avatar image HarryHighDef Jun 15 '18 at 01:46 PM

Yes I believe it is

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question