[Unreal] UE4에서의 원시 포인터가 UE5에서 TObjectPtr 바뀐 경우 사용방법
글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자입니다
UE4에서의 원시 포인터가 UE5에서 TObjectPtr 바뀐 경우 사용방법
문제 상황
ue4버전
UBlacboardComponent* Blackboard;

ue5버전
TObjectPtr<UBlackboardComponent> Blackboard;
로 변경

UseBlackboard 함수를 사용해야 하는 경우

UBlackBoardComponent*& 을 받아와야하는 경우 문제가 생긴다.
코드 사용
UE4
#include "Characters/CAIController.h" #include "Global.h" #include "GameFramework/Character.h" #include "BehaviorTree/BehaviorTree.h" #include "Characters/CEnemy_AI.h" #include "Components/CAIBehaviorComponent.h" #include "Perception/AIPerceptionComponent.h" #include "BehaviorTree/BlackboardComponent.h" #include "Perception/AISenseConfig_Sight.h" ACAIController::ACAIController() { CHelpers::CreateActorComponent<UBlackboardComponent>(this, &Blackboard, "Blackboard");//UE4방식. UBlackboardComponent* Blackboard CHelpers::CreateActorComponent<UAIPerceptionComponent>(this, &Perception, "Perception"); Sight = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight");//CreateDefaultSubobject는 생성자 동적할당, UObject는 런타임 동적할당. Sight->SightRadius = 600;//시야 반경 Sight->LoseSightRadius = 800;//시야를 잃는 범위 Sight->PeripheralVisionAngleDegrees = 45;//시야각 Sight->SetMaxAge(2); Sight->DetectionByAffiliation.bDetectEnemies = true;//적들을 감지o Sight->DetectionByAffiliation.bDetectNeutrals = false;//중립 감지x Sight->DetectionByAffiliation.bDetectFriendlies = false;//아군 감지x Perception->ConfigureSense(*Sight);//감지 객체 지정. 시야감지로 지정. Perception->SetDominantSense(*Sight->GetSenseImplementation());//시야감지를 최우선 감지타입으로 설정. } void ACAIController::OnPossess(APawn* InPawn) { Super::OnPossess(InPawn); Enemy = Cast<ACEnemy_AI>(InPawn);//Enemy에 possess되는 Pawn을 넣어준다. SetGenericTeamId(Enemy->GetTeamID());//TeamID 지정 CheckNull(Enemy->GetBehaviorTree());//Enemy가 BehaviorTree를 가지고 있는지 체크 UseBlackboard(Enemy->GetBehaviorTree()->BlackboardAsset, Blackboard); Behavior = CHelpers::GetComponent<UCAIBehaviorComponent>(Enemy);//Enemy내의 BehaviorComponent를 가져온다. Behavior->SetBlackboard(Blackboard); RunBehaviorTree(Enemy->GetBehaviorTree());//BehaviorTree 실행 }
AAIController에서 상속받은 Blackboard 변수 그대로 사용.
ACAIController::ACAIController()
- CHelpers::CreateActorComponent<UBlackboardComponent>(this, &Blackboard, "Blackboard");
void ACAIController::OnPossess(APawn* InPawn)
- UseBlackboard(Enemy->GetBehaviorTree()->BlackboardAsset, Blackboard);
UE5
#include "Characters/CAIController.h" #include "Global.h" #include "GameFramework/Character.h" #include "BehaviorTree/BehaviorTree.h" #include "Characters/CEnemy_AI.h" #include "Components/CAIBehaviorComponent.h" #include "Perception/AIPerceptionComponent.h" #include "BehaviorTree/BlackboardComponent.h" #include "Perception/AISenseConfig_Sight.h" ACAIController::ACAIController() { Blackboard = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard"));//UE5 방식. TObjectPtr<UBlackboardComponent> Blackboard CHelpers::CreateActorComponent<UAIPerceptionComponent>(this, &Perception, "Perception"); Sight = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight");//CreateDefaultSubobject는 생성자 동적할당, UObject는 런타임 동적할당. Sight->SightRadius = 600;//시야 반경 Sight->LoseSightRadius = 800;//시야를 잃는 범위 Sight->PeripheralVisionAngleDegrees = 45;//시야각 Sight->SetMaxAge(2); Sight->DetectionByAffiliation.bDetectEnemies = true;//적들을 감지o Sight->DetectionByAffiliation.bDetectNeutrals = false;//중립 감지x Sight->DetectionByAffiliation.bDetectFriendlies = false;//아군 감지x Perception->ConfigureSense(*Sight);//감지 객체 지정. 시야감지로 지정. Perception->SetDominantSense(*Sight->GetSenseImplementation());//시야감지를 최우선 감지타입으로 설정. } void ACAIController::OnPossess(APawn* InPawn) { Super::OnPossess(InPawn); Enemy = Cast<ACEnemy_AI>(InPawn);//Enemy에 possess되는 Pawn을 넣어준다. SetGenericTeamId(Enemy->GetTeamID());//TeamID 지정 CheckNull(Enemy->GetBehaviorTree());//Enemy가 BehaviorTree를 가지고 있는지 체크 //UE5 방식. UBlackboardComponent*변수를 만들어 Blackboard.Get()을 담아준 후 사용해야 한다. UBlackboardComponent* BlackboardTemp = Blackboard.Get(); UseBlackboard(Enemy->GetBehaviorTree()->BlackboardAsset, BlackboardTemp);//Blackboard 사용을 위해 BlackboardData인 BlackboardAsset과 BlackboardComonent인 Blackboard를 할당한다. this->Blackboard = BlackboardTemp; Behavior = CHelpers::GetComponent<UCAIBehaviorComponent>(Enemy);//Enemy내의 BehaviorComponent를 가져온다. Behavior->SetBlackboard(Blackboard); RunBehaviorTree(Enemy->GetBehaviorTree());//BehaviorTree 실행 }
ACAIController::ACAIController()
- Blackboard = CreateDefaultSubobject(TEXT("Blackboard"));
UE5 방식. UBlackboardComponent*변수를 만들어 Blackboard.Get()을 담아준 후 사용해야 한다.
void ACAIController::OnPossess(APawn* InPawn)
- UBlackboardComponent* BlackboardTemp = Blackboard.Get();
UseBlackboard(Enemy->GetBehaviorTree()->BlackboardAsset, BlackboardTemp);
this->Blackboard = BlackboardTemp;
사용 예시2
USTRUCT(BlueprintType) struct FDA_Ability { GENERATED_BODY() UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) TObjectPtr<const UTexture2D> Icon = nullptr; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) TObjectPtr<const UMaterialInterface> BackgroundMaterial = nullptr; };
void UTDUW_SkillIcon::ReceiveDA_Ability(FDA_Ability InDA_Ability) { if (InDA_Ability.InputTag.MatchesTagExact(InputTag)) { if (InDA_Ability.AbilityTag.MatchesTagExact(FTDGameplayTags::GetTDGameplayTags().Abilities_None)) { // TObjectPtr에서 원시포인터 형태로 변환. FDA_Ability에서 const로 선언해서 const를 없애는 대신 const_cast 사용. UMaterialInterface* Material_Background = const_cast<UMaterialInterface*>(InDA_Ability.BackgroundMaterial.Get()); UTexture2D* Texture_Icon = const_cast<UTexture2D*>(InDA_Ability.Icon.Get()); Image_Background->SetBrush(UWidgetBlueprintLibrary::MakeBrushFromMaterial(Material_Background)); Image_SkillIcon->SetBrush(UWidgetBlueprintLibrary::MakeBrushFromTexture(Texture_Icon)); } // ... } // ... }
참고 자료
https://uecasts.com/courses/unreal-engine-5-introduction/episodes/ue4-to-ue5-tobjectptr
UE Casts - Premium Unreal Engine 4 Tutorial Screencasts and Unreal Engine 4 Resources
Premium Unreal Engine 4 Tutorial Screencasts and Unreal Engine 4 Resources
uecasts.com
https://www.youtube.com/watch?v=uv8hyf3AB-I
'⭐ Unreal Engine > UE Debugging Log' 카테고리의 다른 글
[UE5] Root Motion이 제대로 작동하지 않을때. Retargeting 제대로 하는 방법 (0) | 2023.08.13 |
---|---|
[Unreal] Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000000c8 문제해결 (0) | 2023.08.03 |
[Unreal] 화살이 포물선을 그리며 떨어지게 하기 (0) | 2023.07.21 |
[Unreal] SDK Not Setup 문제(Project Package 문제) 해결하기 (0) | 2023.07.15 |
[UE5] Enhanced Input이 적용되지 않는 문제 해결방안 (0) | 2023.06.23 |
댓글
이 글 공유하기
다른 글
-
[UE5] Root Motion이 제대로 작동하지 않을때. Retargeting 제대로 하는 방법
[UE5] Root Motion이 제대로 작동하지 않을때. Retargeting 제대로 하는 방법
2023.08.13 -
[Unreal] Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000000c8 문제해결
[Unreal] Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000000c8 문제해결
2023.08.03코드가 정상적으로 빌드되고 Unreal Editor도 정상적으로 작동한다. 하지만 Game Play를 하면 실행되지 않고 크러쉬가 난다. Crash Reporter에서는 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000000c8 라는 문구를 띄운다. 목차 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000000000c8 문제해결 문제상황 코드가 정상적으로 빌드되고 Unreal Editor도 정상적으로 작동한다. 하지만 Game Play를 하면 실행되지 않고 크러쉬가 난다. Crash Reporter에서는 Unhandled Ex… -
[Unreal] 화살이 포물선을 그리며 떨어지게 하기
[Unreal] 화살이 포물선을 그리며 떨어지게 하기
2023.07.21화살 같이 날라가는 물체는 대개 포물선을 그린다. 원형으로 된 물체가 아닌 이상 날아가는 물체의 시작 부분이 서서히 바닥을 바라보며 추락해야 한다. 이것을 가능하게 하려면 Projectile Movement의 세부항목에 들어가 Rotation Follows Velocity를 체크해주어야 한다. 목차 화살이 포물선을 그리며 떨어지게 하기 ProjectileMovement에서 Rotation Follows Velocity 체크 날아가는 화살의 Actor 내의 ProjectileMovement 세부항목을 확인한다. Projectile Rotation Follows Velocity를 체크한다. Projectile Gravity Scale 0이면 중력 적용x 1이면 일반적인 중력이 적용된다. 해당 항목을 체크하면… -
[Unreal] SDK Not Setup 문제(Project Package 문제) 해결하기
[Unreal] SDK Not Setup 문제(Project Package 문제) 해결하기
2023.07.15SDK Not Setup 문제 해결하기 (the sdk for windows is not installed properly, which is needed to generate date. check the sdk section of the launch on menu in the main toolbar to update sdk. would you like to attempt to continue anyway?). 다음과 같은 문구가 뜨며 프로젝트가 Package 되지 않는 문제 해결하기. 목차 SDK Not Setup 문제 해결하기 the sdk for windows is not installed properly, which is needed to generate date. check the sdk sect…
댓글을 사용할 수 없습니다.