This is last resort, if you really lost contact with object and searching is only way of getting it. Generally you should keep related object in varables, as they are created or have things changed in them or when some specific event happen, this let you create network of object relations which from it you can access anything from anywhere.
For example you got Party class that manages party, party needs to hold information about there members, so when PlayerController ask to join that party via some function call, you make it so it past it self to it via argument, party class then add PlayerController to some array of members and it calls PlayerController via some function that he been added to party and includes it self as argument in that call, PlayerController acknowledges that it joined part and set party object in to pointer in itself so it know in which party it is. Parties overall could be managed in GameMode and GameMode could have array of them so you can list them and present to player and GameMode you can access from UWorld which you can access from any actor via ().
You case it’s pretty easy, as mirsabayu hints, you can use overlap events to detect what actors going in and out of box component for example. When Pawn is coming in and it a Pawn with your Pawn class which will have varable have event function OnEnteredBuilding(*ABuildingController Building)
, ABuildingController on it’s overlap event will check is it comaptible (your class with that function) pawn using IsA() function, the you cast to your class and call Other->OnEnteredBuilding(this)
(“Other” overlap actor argument varbale which you should have on overlap event, “this” is object which code is being executed right now). OnEnteredBuilding function in pawn will set variable ABuildingController* InBuilding
so pawn knows where it is and if you gonna deal with this pawn somewhere else you know in which building it is and from that variable it is.
Go explore API refrence:
Search for classes and explore there functions and varables and use them, start with AActor to see what you can do with all actors:
most importent for you will be those 2 delegates, overlap events where actor enter and leaves collision of actor:
Delegate is event in which you can bind function in to and they are being called when event happens, they usally listed as varables in API refrence. It’s hard to read them in API refrence right now because it does not tell you what kind of functions you need to create, best way right now is to search there structure type like “FActorEndOverlapSignature” in GitHub and first reult should give you decleration of delegate.
https://github.com/EpicGames/UnrealEngine/blob/311e18ff369078e192a83f27834b45bdb288168a/Engine/Source/Runtime/Engine/Classes/GameFramework/Actor.h#L26
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FActorBeginOverlapSignature, AActor*, OtherActor );
So it tells as function need to have 1 argument AActor* with the name OtherActor (but it can be any name, C++ compiler don’t check them anyway, argument types are more importent). So in your ABuildingController you declere function, in header file:
void OnActorEntersBuilding(AActor* OtherActor);
and in .cpp something like this:
void ABuildingController::OnActorEnteredBuilding(AActor* OtherActor) {
if(OtherActor->IsA(AMyAwesomePawnThatCanEnterBuilding::StaticClass()) { //we check if it our pawn
((AMyAwesomePawnThatCanEnterBuilding*)OtherActor)->OnEnteredBuilding(this);
}
}
And on BeginPlay of ABuildingController bind the event doing this:
OnActorBeginOverlap.AddDynamic(this,&ABuildingController::OnActorEnteredBuilding);
first argument is object that will be called on event and 2nd is function pointer to function we want to delegate to call
And Pawn function OnEnteredBuilding(ABuildingController*) will set varable InBuilding, on End Overlap event you set that varable to nullptr as he leaves the building
Now that simple breath example, btw it will only work if those BuildingController won’t overlap themselfs or else it might mess up that varable in pawn (it enters building, which has another building, but if it leaves one building will be null). You should figure yourself what is best for your game, so you should learn how to use assets that engine gives you to build such system. Again go explore API reference, learn about classes and functions and varbales in them, all classes in engine are made same way as your classes, so look how they constructed and try to mimic same style.
Sry that this post is kind of messy ^^’ wanted for you to show you how you should approach it