본문 바로가기
프로그래밍/Unreal Engine4

현재 레벨에 있는 다른 Actor의 C++클래스 연결하기.

by 눈야옹 2016. 4. 20.

기본 적인 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//받아올 클래스
    static AStageFloor* stagefloor = nullptr;
    //현재 래벨에 존재하는 모든 액터를 가져온다.
    TTransArray<AActor*> actors = GetWorld()->GetCurrentLevel()->Actors;
    
    for (int i = 0; i < actors.Num(); ++i)
    {        
        USceneComponent* root = actors[i]->FindComponentByClass<USceneComponent>();
        //만약 USceneComponent를 RootComponent 검색해서 있다면 연결하고.
        if (root)
        {
            //열결한 Root(USceneComponent)가 StageFloor를 Tag로 가지고 있다면 
            if (root->ComponentHasTag("StageFloor"))
            {
                //현재 i번쨰 액터를 AStageFloor로 Cast해서 받아온다.
                stagefloor = Cast<AStageFloor>(actors[i]);
            }
        }    
    }
 
    FVector pos = FVector::ZeroVector;
 
    if (stagefloor)
    {
        pos = stagefloor->GetRandomPositionInCollisionBox();
        GEngine->AddOnScreenDebugMessage(-1100.0f, FColor::Blue, pos.ToString());
    }
cs


tamplate함수로 변경  범용적으로 쓰기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//기본적으로 root는 USceneComponent으로 하고있다.
    template<class ReturnClassType, class RootClassType = USceneComponent>
    FORCEINLINE ReturnClassType* FindClassWithRootTag(const FName tag)
    {
        TTransArray<AActor*> actors = GetWorld()->GetCurrentLevel()->Actors;
        for (int i = 0; i < actors.Num(); ++i)
        {
            RootClassType* root = actors[i]->FindComponentByClass<RootClassType>();
            if (root)
            {
                if (root->ComponentHasTag(tag))
                {
                    return Cast<ReturnClassType>(actors[i]);
                }
            }
        }
        return nullptr;
    }
cs