Hi, I am new to programming in unreal. I have prior knowledge of c++ programming. I have a simple question, how to get reference to an actor in the world using c++. I know using
GetActorOfClass() and GetAllActorsOfClass() but i don’t know how to work with these functions to get the actor reference.
So help me to solve this problem. Example program is much appreciated.
Thanks for your response. It is placed in level. But it is helpful to me in future if you provide me both with code example. Thanks
Thanks a lot. But how do I get specific actor from the array. Also please give an example program for GetActorOfClass(). Thanks
There are many ways you can reference the actor in the world. How is this actor spawned? Is it placed in the level or do you spawn it from the code?
Let’s say the class of the actor you want to find is ADoor (this will work perfectly if you substitute it for your own class.)
First, you need to include GameplayStatics in you cpp file:
#include "Kismet/GameplayStatics.h"
##Example of GetActorOfClass
ASomeActor is just whatever actor you need it to be.
//cpp file
void ASomeActor::BeginPlay() {
Super::BeginPlay();
ADoor* Door = UGameplayStatics::GetActorOfClass((), ADoor::StaticClass());
// Door is now a pointer to one of the ADoor objects in the world
}
##Example of GetAllActorsOfClass
// header file, in class definition
UPROPERTY() // Put this in the header file so you can mark it as UPROPERTY
TArray<AActor*> DoorsAsActors; // Without UPROPERTY, strange things may happen with garbage collection on TArray
// cpp file
void ASomeActor::BeginPlay() {
Super::BeginPlay();
UGameplayStatics::GetAllActorsOfClass((), ADoor::StaticClass(), DoorsAsActors);
for (AActor* DoorAsActor : DoorsAsActors) {
ADoor* Door = Cast<ADoor>(DoorAsActor);
// Now it is looping through all ADoor actors in world, with Door as the reference.
}
}
*First, we store the pointers to the ADoors in DoorsAsActors, cast to Actors, because GetAllActorsOfClass only works with arrays of Actors. *
Additional Notes
- Don’t put GetActorOfClass or GetActorsOfClass unconditionally in Tick (don’t run them every frame) because they are expensive. Instead, just declare the pointer in the class definition in the header file, and set the pointer in BeginPlay
- Don’t put them in the Constructor, use BeginPlay instead.
Hope this helps!
GetAllActorsOfClass is a Blueprint static function, if you want to get all actors of a specific class in C++ you should use TActorIterator, you can see an example in GameplayStatics.cpp under GetAllActorsOfClass function:
TSubclassOf<AActor> ActorClass;
TArray<AActor*> OutActors;
if (ActorClass)
{
for(TActorIterator<AActor> It((), ActorClass); It; ++It)
{
AActor* Actor = *It;
OutActors.Add(Actor);
}
}
You can place ActorClass and OutActors where you want, maybe in your header file with UPROPERTY macro to have them accessible from Blueprints or inside a function, it’s your choice!
It depends, what actor do you need? If you want to filter all actors by the display name you can edit the code as follows:
TSubclassOf<AActor> ActorClass;
TArray<AActor*> OutActors;
if (ActorClass)
{
for(TActorIterator<AActor> It((), ActorClass); It; ++It)
{
AActor* Actor = *It;
if(Actor->GetDisplayName == "MyActor")
{
OutActors.Add(Actor);
}
}
}
GetActorOfClass() returns the first actor found of a specific class instead of all actors, so if you have 2 actors in the world, it will return only the first found. If you want to call directly these functions from C++ instead of using TActorIterator you can #include “Kismet/GameplayStatics.h” and then call directly them like this:
TSubclassOf<AActor> ActorClass;
TArray<AActor*> OutActors;
UGameplayStatics::GetAllActorsOfClass((), ActorClass, OutActors);
Very much thanks for your reply. I understood well : ). Thanks
My answer was held up by moderation. I posted it 6 days ago
Sorry recently we had problems with moderation, it should be better now.