Download

Add Components to C++ Class

Hello All.

At this time I am able to spawn actors from blueprints that are based off of C++ classes that I’ve written. Apparently I missed something though. The actors do have simple animations that are working when spawned, however, I have no audio. Is there a class that I can add to get a similar feel as when creating an actor in the editor? Basically I’d like to allow packaging of the actor with all the components, and spawn it complete at runtime. Is this possible to do? Do I need to traverse an array of components and instantiate each one at spawn time? For example, audio, particle effects, etc.

Thank you for the help.

Hey, i have no idea about the Audio Components, but i can show you how i create a StaticMesh Component in my C++ class. Maybe this helps you getting started (:

The component is accessible in Blueprints, so i gave it the UPROPERTY Macro with some parameters in the Header files:


**MyClass.h**

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Item Config")
class UStaticMeshComponent* ItemStaticMesh;

And this is what i have done in the Constructor of that class in the .cpp file:


**MyClass.cpp**

ItemStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("ItemStaticMesh"));

// This is Component Specific. Just wanted to show you that you can access it here too.
ItemStaticMesh->bReceivesDecals = false;
ItemStaticMesh->bCastDynamicShadow = true;
ItemStaticMesh->SetOnlyOwnerSee(false);
ItemStaticMesh->SetOwnerNoSee(false);

Tell me if anything is wrong or you need help (:

Thank you eXi.

I have a C++ class based off SkeletalMeshActor which is referenced in another class that manages the spawning of them:



    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "WaveData")
    TArray<TAssetSubclassOf<AMyPawn>> WaveData;


At some point, prior to spawning, I initialize the blueprints, but not sure what this is really doing though:



    int32 waveCount = WaveData.Num();

    for (int32 x = 0; x < waveCount; x++)
    {
        const FStringAssetReference AssetRef = WaveData[x].ToStringReference();
        gameMode->AssetLoader.SimpleAsyncLoad(AssetRef);
    }


Then at spawn time I perform the following:



    if (WaveData[p].IsValid())
    {
        FVector spawnLocation = FVector(0.0, 0.0f, 0.0f);
        FRotator spawnRotation = FRotator::ZeroRotator;
        AMyPawn* pawn = GetWorld()->SpawnActor<AMyPawn>(WaveData[p].Get(), spawnLocation, spawnRotation);
    }


This works as far as loading the asset, and it’s animations. And I suspect that I could add a Sound Cue (ASoundCue) to the C++ class that inherits from ASkeletalMeshActor. . .but I don’t think it will have the same look and feel as when adding an Actor to the scene and setting additional components as needed.

It was fairly easy adding the initial audio to the actors but I’m not certain how flexible the approach will be for other use cases such as explosions, and other sound effects.

In the AMyPawn class I added an asset pointer to the sound cue:



    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Sound)
    TAssetPtr<USoundCue> sound;


Then in the Pawn Manager class, the class responsible for spawning the actors, I load the sound cue and add it to the AActor::PlaySoundOnActor



    FStringAssetReference& AssetRef = pawn->sound.ToStringReference();
    pawn->sound = Cast<USoundCue>(gameMode->AssetLoader.SynchronousLoad(AssetRef));
    if (pawn->sound.IsValid())
        pawn->PlaySoundOnActor(pawn->sound.Get());


You may notice that I am accessing gameMode->AssetLoader in several places, namely in an effort to keep performance high by having the asset loader initialized and ready at all times. It’s in my GameMode class, and it may not be the best location for it, but it appears to be working for the moment:



    .h public access
    --------------------------------------------
    FStreamableManager AssetLoader;


Does anyone know how to add different types of Components to a class via TArray<>?

I tried TArray<TAssetPtr<UActorComponent>> but that wouldn’t let me assign anything in the Graph Editor. I also tried TArray<TAssetPtr<UChildActorComponent>> but still no luck. Does anyone know how to do this?

I’m trying to make the class work with the Blueprint Graph Editor. Is it possible to have those components that are static meshes be visible in the Viewport too?

Thank you in advance for the help.

Thank you, thank you, thank you!!!
I was struggling with adding a custom scene component for hours until I found this thread.

Maybe (cannot try at the moment) to get other types



TArray<TSubClassOf<UActorComponent>> Array


or



TArray<UActorComponent*> Array


to get references