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 ( )
{
}
}
What is ActorRef and why ever you cast it to AActor? Name ActorRef suggest that this is already instance of Actor class.
Why did you name the array designed to hold the components RadarActors?
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.
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.
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.
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.
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.