Announcement

Collapse
No announcement yet.

How to Initialize a component in the Constructor Script?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to Initialize a component in the Constructor Script?

    So the idea is, when I check 'UseFootsteps', the class should create the AudioComponent for it.

    Code:
    .h
    UPROPERTY(EditAnywhere) bool UseFootsteps;
    UPROPERTY(VisibleDefaultsOnly) UAudioComponent* FootSound;
    Code:
    .cpp
    void ANpc::OnConstruction(const FTransform& Transform)
    {
        if (UseFootsteps && !FootSound) {
            FootSound = NewObject<UAudioComponent>(this, UAudioComponent::StaticClass());
            FootSound->RegisterComponent();
            FootSound->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
        }
    }
    With the above code, the component doesn't appear in the hierarchy.
    With 'VisibleAnywhere' all it's stats appear in a big mess in the details panel though, which isn't ideal.

    Any ideas on the proper way to do this so it appears in the component list? Thanks!
    Last edited by Juice-Tin; 10-06-2017, 12:42 PM.

    #2
    It won't appear in the Components List because it's not being created in the Constructor.

    Comment


      #3
      The answer is, not easily.

      If you want to make it appear in the hierarchy, you need to use create your own custom editor that inherits from the SCSEditor so you can grab it and add the component to the Construction Script.

      I actually just did this in my own project and it was a bit of a pain. The SCSEditor is not meant to be expanded (unless I simply missed something) and it required engine changes (mainly to the SCSEditor itself) to get it to play nice with other modules and allow custom construction script manipulation.
      Able Ability System - A high performance, robust ability system for UE4. Now Available!

      Comment

      Working...
      X