Cannot Cast to Actor Component from HUD Class in C++

I am trying to create a radar in c++ using an actor component and a HUD class. In blueprints you can get all components by class from the actor component but in c++ for some reason I cannot cast to the actor component from the hud. I also don’t know how to cast to the AActor from the hud as well the object parameter is a mystery to me if I am casting from the hud class. Any help would be much appreciated and thank you for your time.

void ACppRadarHUD::ReceiveDrawHUD(int32 SizeX, int32 SizeY)
{
	AActor* Actor = Cast<AActor>(ActorRef);
         //Here I don't how to cast here in this instance. ^
          //Below I cannot cast to the actor component in order to get the components by  class.
        //RadarActors is a TArray comprised of AActors within the level.
	Actor->GetComponents<"ActorComponent should go here.">(RadarActors);
	ScreenSize.X = SizeX;
	ScreenSize.Y = SizeY;
        //Using a for loop in order to draw the hud eventually.
	for (  )
	{

	}
}
  1. What is ActorRef and why ever you cast it to AActor? Name ActorRef suggest that this is already instance of Actor class.
  2. Why did you name the array designed to hold the components RadarActors?
  3. Unreal C++ is little different than vanilla C++, but if you don’t know how to use simple function like AActor::GetComponents, then go back to vanilla C++ tutorials/books to learn language basics.

From AHUD, to get components from the owned pawn you can do:

GetOwningPlayerController()->GetPawn()->FindComponentByClass<UAC_RadarComponent>();

I assume that is what you are trying to do.

1 Like

Thanks man for insulting my intelligence you are cool. How about you just don’t comment if you don’t have anything constructive to say other than go back to language basics. You’re unprofessional. Better yet keep asking “why” questions they really help a lot.

1 Like

Thank you I appreciate it.

I am trying to cast to the actor component indicated but it says the actor component is undefined. I do have the proper includes to support the cast.

Where it says UAC_RadarComponent it says that UAC_RadarComponent (or ActorComponent) is undefined yet I have the proper includes to support the cast within the HUD Class. I know when communicating from HUD to actor classes it is fine, but when casting to an actor component it is giving me an error.

It is not clear where ActorRef comes from. Is it the HUD? is it an Actor on the level? Is it the pawn using the HUD?

In UAC_RadarComponent I am using a prefix AC_ which might differ from your actual class implementation. Insert the actual type name of the radar component as declared in the header.

I should probably remove that cast in the question because it is irrelevant.

Made me curious though as Emaer said casting to an actor is not required if the class or its parent class is already an actor.

It is an actor in the level I am trying to create a Radar that gets all of the actors inside of the level and returns their locations to the radar hud. RadarActors is a TArray comprised of AActors. That is partially why I was attempting to cast to a class of AActor* Actors.

Alright, then this is a bit more complex. You could use the GetAllActorsOfClass method (UGameplayStatics library) and filter to the actors you are giving a radar component but this is really dirty. there is not really a clean way to directly pull actors from a level other then that.

What I like to do is to register a GameInstanceSubsystem which will serve as a managing class, in this case a RadarManager. a GameInstanceSubsystem can be pulled in 1 line in BP and in 2 lines in c++ (through the gameinstance). On this subsystem you can implement a method to register and unregister radarcomponents. On the radarcomponent you should implement (OnBeginPlay) a call to this subsystem to register the component. Now your subsystem gets all the radarcomponents automatically. Then, From the HUD you can just call the subsystem to get all the registered components. This is a lot cleaner than trying to filter all actors on an entire level.

Programming Subsystems | Unreal Engine 4.27 Documentation

1 Like

But to give more context I am trying to do a cpp conversion of this tutorial here. Here so that you may understand exactly what I am trying to do rather then me just spouting snippets.

I see. He / she uses the HUD itself as a manager where I’d use a subsystem because it is less dirty. This is possible however.

I lost it here. If your HUD holds an array of actors that should look like this:

UPROPERTY()
TArray<AActor*> RadarActors;

and to add an actor or anything deriving from actor just do

RadarActors.Add(SomeActor);

I did that in the HUD Class itself.

void ACppRadarHUD::AddToRadar(AActor* Actor)
{
	RadarActors.Add(Actor);
}

void ACppRadarHUD::ClearFromRadar(AActor* Actors)
{
	RadarActors.Remove(Actors);
}

Can you post all the code here? It might be easier for me just do write it into working code

1 Like

Want me to edit the question or just post in a reply?

post it as a reply

Ok will do.