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"

Is it possible to use editor value in class constuctor

Hello. Im not sure the title of the question is even 80% accurate.

Anyway I want to spawn an array of "child actor components" (no problem there ( for now ) ) depending on the size of a UProperty i have set up. But when i tried that ofc, it defaults to 0 ( as that is default ) at first. But even, if I change it in the editor it stays 0, throughout the construction and on my begin play" breakpoints - it has been changed to the value given from the editor.

So is it possible through 'some means' to be able dynamically change a exposed to editor property and for it to effect constructor or should i just do the components creation in the "begin play"

Product Version: UE 4.9
more ▼

asked Nov 18 '15 at 03:43 PM in C++ Programming

avatar image

44 10 10 14

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

2 answers: sort voted first

Yes, it's possible using the OnConstruction function. Basically, this is called each time you modify a property in the editor. So, if you want to have 2 actor component after you input 2 in the editor, Spawn those components in the OnConstruction call.

You can do it in Blueprint or C++.

The C++ function signature you need to overide is this :

     virtual void OnConstruction(const FTransform& Transform) override;

I recommend using the #if WITH_EDITOR macro here just so its explicit this is only true in the editor.

After that, you can spawn components. Here is an example:

 for (int32 i = 0; i < nNbComponentsToSpawn; i++)
     USphereComponent* spawnPoint = NewObject<USphereComponent>(this);
     spawnPoint->CreationMethod = EComponentCreationMethod::UserConstructionScript;
     spawnPoint->AttachParent = RootComponent;
     spawnPoint->bCastDynamicShadow = false;

In this scenario, nNbComponentsToSpawn is the variable you have change value of, and it's going to spawn the right number of components (sphereComponents in this case).

I think this is what you wanted?

Hope this helps,


more ▼

answered Nov 18 '15 at 09:01 PM

avatar image

2.1k 70 37 259

avatar image ExcelionSC Nov 19 '15 at 09:06 AM

Thank you very much :) this helps a lot. I can't wait to go and try it.

avatar image ExcelionSC Nov 19 '15 at 10:12 AM

a small issue arose - since NewObject - 'NewObject' : invalid template argument for 'T', type expected. And im using - static ConstructorHelpers::FObjectFinder SegmentBlueprint(....) it doesn't register it at comple-time it seems. Is there a way to 'circumvent' this

avatar image MickD777 Nov 20 '15 at 02:10 PM

You cannot use ConstructorHelpers outside of constructors. Can you copy-paste what you are trying to do?

avatar image ExcelionSC Nov 23 '15 at 10:00 AM

Im sorry for the late response ( few irl stuff got in my way ). This is in the constructor:

 static ConstructorHelpers::FObjectFinder<UBlueprint> SegmentBlueprint( TEXT( "Blueprint'/Game/Blueprints/TSwitchesPuzzle/LO_Switch.LO_Switch'" ) );
         if ( SegmentBlueprint.Object != NULL ) 
             BlueprintTemplate = (UClass*)SegmentBlueprint.Object->GeneratedClass;    

 BlueprintTemplate  is kept in header as  - TSubclassOf<ATSwitchActor>
 Then on construct i wanted something like this (few variations were left as commented - none worked ) 
 for( int i = 0; i < PuzzleDimention ; i++ )
     FSwitch new_switch;
     new_switch.id = i;
     new_switch.active = false;
     new_switch.actor = SpawnOb<BlueprintTemplate>( this );
     new_switch.actor->CreationMethod = EComponentCreationMethod::UserConstructionScript;
     new_switch.actor->AttachParent = RootComponent;
     new_switch.actor->SetActorEnableCollision( ECollisionEnabled::NoCollision );
     //new_switch.SwitchActor = NewObject<UChildActorComponent>( this ,TEXT( "PlateComponent") ); OLD
     //new_switch.SwitchActor = ConstructObject<APipeSegmentC>( APipeSegmentC::StaticClass(), GetOwner(), NAME_None, RF_Transient); OLD
     //    new_switch.SwitchActor = CreateDefaultSubobject<UChildActorComponent>( *( FString::Printf( TEXT( "WORKDAMMIT_%u") , i ) ) );
     //    new_switch.SwitchActor->SetChildActorClass( BlueprintTemplate ); // missing documentation (dull)
     //    if( new_switch.SwitchActor )
     //    {
     //        new_switch.SwitchActor->OnComponentCreated( );
     //        //new_switch.SwitchActor->RegisterComponent( ); // FOR DYNAMIC COMPONETNS ;/
     //        new_switch.SwitchActor->SetRelativeLocation( FVector( 100.0f*i , 0.0f , 0.0f ) );
     //        new_switch.SwitchActor->AttachTo( RootComponent );
     //    }
 RegisterAllComponents( );
 Super::OnConstruction( Transform );

I guess Im missing something basic from how the whole spawning/creating components and actor works from my U-knowledge.

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

Well, a constructor (as i'm sure you know :D) simply represents the 'layout' or floor plan of the class and when it becomes an object the variables are the defaults when constructed.

So if I understand your question correctly then I would say no it is not possible. After the initial construction of course it is possible to edit the values but otherwise if you want the default values to be something else then place it in the constructor.

If you want to change it at run time rather then creation then you've got the right idea. Use BeginPlay.

more ▼

answered Nov 18 '15 at 05:15 PM

avatar image

Mark James G
534 44 25 51

(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