Calling a function on a Blueprint Class from a different C++ class

Is there a way to access components and call their functions in this setup…

Blueprint (made in editor of type Actor)

  • has a widget component (this is actually what I want to access)

C++ Class PlayerController

in cpp file I spawn the Blueprint above into the world…

In Constructor…
ConstructorHelpers::FClassFinder BlueprintFinder(TEXT(“PathToBlueprint”));
BlueprintForSpawn = BlueprintFinder.Class;

In BeginPlay…
GetWorld()->SpawnActor(*BlueprintForSpawn, &Loc, &Rot, SpawnParams);

while in BeginPlay I can retrieve its name…
UE_LOG(LogTemp, Warning, TEXT(“%s”), *BlueprintForSpawn->GetName());

logs fine

Im no c++ maestro but the options Ive searched and tried in regard to gettings this Blueprint actor’s components tend to crash the engine.

Does anyone know how to do this and have tested it or is this the wrong way to go about doing what Im trying to achieve. I just figured if I can access the name that I could access its components and maybe call functions on them etc.

Thank you

This is the common way to get a component:

/// AActor* YourActor = SpawnActor....
UWidgetComponent* YourComponent = YourActor->FindComponentByClass<UWidgetComponent>();

TY, however this is not working…
I tried…
UWidgetComponent* Component = *BlueprintToSpawn->FindComponentByClass()

This gives error with no member named ‘FindComponentByClass’ in ‘UClass’

Tried casting…
UWidgetComponent* Component = Cast(*BlueprintToSpawn)->FindComponentByClass()

This crashes

NOTE: BlueprintToSpawn is a TSubclassOf , this probably makes a difference somewhere

The issue is that you are trying to call functions on a UClass instead of on a spawned actor. You get that spawned actor pointer from the return value of the SpawnActor method (my example). That is what you need to call FindComponentByClass on.

[BP] Whats the difference between variable of class and object?

Ahhh TY I misunderstood what you had written there…
Now I have …
AActor* MyActor = GetWorld()->SpawnActor(*BlueprintForSpawn, &Loc, &Rot, SpawnParams)

UWidgetComponent* Component = MyActor->FindComponentByClass();

No crashes

Thanks Alot!

1 Like