hey,
I’ve created a Actor with a USphereComponent and a int variable, used it in the ue4 editor as Blueprint, placed it somewhere on the map and set the int variable to a specific value(in blueprint).
Now I’m trying to display this specific int value of the BlueprintActor in a HUD, but i don’t know how to create a object of this Actor.
example:
AMyActor *myActor = getActor on the Map ;
I know that it’s possible to get the Character with GetPlayerPawn, but i have no idea how it’s possible to get other actors.
~Slei
Hey, Sleicreider
You can use iterators to find other objects/actors!
Take a look at: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine ForumsObject%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search
so, with these examples in the link I get a list of all Actors on the map, but i still have no idea how to get a particular Actor and use its variables/print its values on HUD. (I don’t get it)
Let’s say i have “AObjekt” (random Actor name) actor with a variable num. (might change in runtime)
It’s possible to have more than one AObject on the map.
so, how would i get the num value of the 3rd AObject on the Map?.
Well, you’ll have to decide which one to read the variable from, and that depends on the objective of your HUD.
You can read from the closest “AObject” to the player’s location… for example:
...
ACharacter *character = nullptr; // Use your character type instead!
float distance, nearest = TNumericLimits<float>::Max();
for (TObjectIterator<ACharacter> it; it; ++it)
{
distance = FVector::Dist(it->GetActorLocation(), playerPawn->GetActorLocation());
if (distance < nearest)
{
character = *it;
nearest = distance;
}
}
return character;
}
But I don’t know which metric do you want to use to determine which object to read from…
Hope that can help you!
Isn’t it possible to give each Actor a name or index on the map and get it by this name/index?
edit: I’ve found a workaround for my problem, but i’m still curious, if it’s possible.
Well, you can add an instance of TMap, I recomend do it in a singleton class, and there store the references to your actors using names. TMap | Unreal Engine Documentation
I didn’t play much or look at the engine code, but if it’s like a std::map from regular C++ then its find() procedure may be O(log(n)) complexity… good to search in a scene with lots of characters!
One of the introductory videos actually takes you through the actor loop… The lady presenting it casts the actor to a type. If the type casting is successful, it picked that actor… I’m not sure how great that method is, I haven’t had time to think about it. “Introduction to programming - Unreal Engine”
I’ve already watched the videos, I tried to do the same thing like she did in the video, but it only worked for the ACharacter class