ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 블루프린트 엑터를 부모에 붙이기
    Game Programming/언리얼 2023. 10. 6. 14:51
    에셋에 FlashLight가 있어서 한번 써보기로 했다.

    두번째 에셋이 FlashLight인데 
    코드를 먼저 보여준다면
    UObject* SpawnActor = Cast<UObject>(StaticLoadObject(UObject::StaticClass(), NULL, TEXT("/Game/Voyager/Blueprints/BP_FlashLightWeapon.BP_FlashLightWeapon")));
    
    UBlueprint* GeneratedBP = Cast<UBlueprint>(SpawnActor);
    if (!SpawnActor)
    {
        GEngine->AddOnScreenDebugMessage(2, 5, FColor::Red, FString::Printf(TEXT("CANT FIND OBJECT TO SPAWN")));
        return;
    }
    UClass* SpawnClass = SpawnActor->StaticClass();
    if (SpawnClass == NULL)
    {
        GEngine->AddOnScreenDebugMessage(2, 5, FColor::Red, FString::Printf(TEXT("CLASS == NULL")));
        return;
    }
    
    UWorld* World = GetWorld();
    FActorSpawnParameters SpawnParams;
    SpawnParams.Owner = this;
    SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
    const USkeletalMeshSocket* BarrelSocket = GetMesh()->GetSocketByName("barrel_socket");
    AActor* actor = World->SpawnActor<AActor>(GeneratedBP->GeneratedClass, GetActorLocation(), GetActorRotation(), SpawnParams);
    actor->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, "barrel_socket");

     

    위와 같이 짯다...

    뭐 구글링을 통해서 가져온 것인데 (코드 설명을 위해 접은 글 생략)

    UObject* SpawnActor = Cast<UObject>(StaticLoadObject(UObject::StaticClass(), NULL, TEXT("/Game/Voyager/Blueprints/BP_FlashLightWeapon.BP_FlashLightWeapon")))

    StaticLoadObject 함수를 사용해서 블루프린트 객체를 가져와야하는 것으로 파악된다.

    언리얼 문서를 보면 "오브젝트를 문자열로 가져온다"라고 되어있다.

    UBlueprint* GeneratedBP = Cast<UBlueprint>(SpawnActor);

    블루프린트로 캐스팅 후 

    FActorSpawnParameters SpawnParams;
    SpawnParams.Owner = this;
    SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

    FActorSpawnParameters를 위와 같이 설정해야 했다.

    ESpawnActorCollisionHandlingMethod::AlwaysSpawn 이 부분이 헷갈렸는데

    /** Fall back to default settings. */
    Undefined								UMETA(DisplayName = "Default"),
    /** Actor will spawn in desired location, regardless of collisions. */
    AlwaysSpawn								UMETA(DisplayName = "Always Spawn, Ignore Collisions"),
    /** Actor will try to find a nearby non-colliding location (based on shape components), but will always spawn even if one cannot be found. */
    AdjustIfPossibleButAlwaysSpawn			UMETA(DisplayName = "Try To Adjust Location, But Always Spawn"),
    /** Actor will try to find a nearby non-colliding location (based on shape components), but will NOT spawn unless one is found. */
    AdjustIfPossibleButDontSpawnIfColliding	UMETA(DisplayName = "Try To Adjust Location, Don't Spawn If Still Colliding"),
    /** Actor will fail to spawn. */
    DontSpawnIfColliding					UMETA(DisplayName = "Do Not Spawn"),

     

    충돌과 스폰이 관련 있는 설정으로 이해했다.

    AActor* actor = World->SpawnActor<AActor>(GeneratedBP->GeneratedClass, GetActorLocation(), GetActorRotation(), SpawnParams);
    actor->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, "barrel_socket");

    SpawnActor를 통해 엑터를 스폰시키고, barrel_socket에 붙였다.

     

    완성 ~!

Designed by Tistory.