게임 개발 메모장
[ UE5 ] Unreal Assert 본문
참고 : C++ Assert - https://yooyoungjae0627.tistory.com/6
assert 는 일반적으로 참조하고자 하는 변수가 null 인지 아닌지를 체크하는 용도로
자주 사용된다. 실제 개발을 할 때는 프로젝트명으로 된 헤더 파일에 assert 구문을 이용한
커스텀 로그를 만들어 놓는 게 간편하다.
언리얼에서의 assert 종류는 크게 check, verify, ensure 이 있다.
// assert 는 DO_CHECK = 1 일때만 실행
// check 는 false 값이면 실행을 중지
check(Mesh != nullptr);
checkf(MyPtr, TEXT("MyPtr is nullptr")); // false 이면 로그 출력
checkcodeMyPtr, TEXT("MyPtr is nullptr")); // checkf 와 비슷
checkNoEntry(); // 절대 실행될 리 없는 경로 표시
checkNoReentry(); // 호출 완료 전까지 다시 호출되면 안되는 경우 체크
checkNoRecursion(); // checkNoReentry() 와 동일
unimplemented(); // 반드시 재정의해야 하는 가상 함수를 표시하기 위해 사용
// verify 는 DO_CHECK = 1 이 아니어도 실행
verify(Mesh != nullptr); // 이하 다른 method 는 assert 와 유사
// ensure 은 콜스택을 생성한다
ensure(MyPtr != nullptr); // 이 지점까지의 콜스택 생성
ensureMsg(MyPtr, TEXT("log")); // 콜스택 생성 + 로그 출력
ensureMsgf(Success, TEXT("log")); // if 문의 조건문 안에 넣어 사용 가능
Assert
assert는 코드의 검증 도구이다. 언리얼 엔진에서는 검증을 위한 매크로 시리즈가 제공된다.
메크로를 확인하려면 아래의 파일에서 찾아볼 수 있다.
/UE4/Engine/Source/Runtime/Core/Public/Misc/AssertionMacros.h
런타임 어서트 매크로는 다음과 같은 3가지 종류가 있다.
- 실행 중지 (DO_CHECK)
- 디버그 빌드에서 실행 중지 (DO_GUARD_SLOW)
- 실행 중지하지 않고 오류 보고 (DO_CHECK)
check(표현식)
표현식의 결과가 false인 경우 실행을 중지한다. 가장 간단한 형태의 매크로
check(Mesh != nullptr)
check(bInitialized);
checkf(표현식, ...)
check와 동일하지만 표현식의 결과가 false인 경우 추가 정보를 출력할 수 있다.
checkf(WasDestroyed, TEXT( "Failed to destroy Actor %s (%s)"), *Actor->GetClass()->GetName(), *Actor->GetActorLabel());
verify(표현식)
DO_CHECK가 켜져있으면 check와 동일하고, DO_CHECK가 꺼져있어도 실행된다. 검증하는데 사용 된다.
verify((Mesh = GetRenderMesh()) != nullptr);
verifyf(표현식, ...)
checkf처럼 실행을 중지하면서 디버그 메시지를 출력한다.
verifyf(Module_libeay32, TEXT("Failed to load DLL %s"), *DLLToLoad);
checkCode(표현식)
중괄호 범위안의 표현식을 실행할수 있다. (checkf의 확장버전같은 느낌)
DO_CHECK가 꺼져 있으면 컴파일에서 제외된다.
checkCode( if( Object->HasAnyFlags( RF_PendingKill ) ) { UE_LOG(LogUObjectGlobals, Fatal, TEXT("Object %s is part of root set though has been marked RF_PendingKill!"), *Object->GetFullName() ); } );
checkNoEntry()
절때 실행될 일 없는 코드 경로에 표시
switch (MyEnum)
{
case MyEnumValue:
break;
default:
checkNoEntry();
break;
}
checkNoReentry(), checkNoRecursion()
함수의 재진입을 방지하기 위한 용도
void NoReentry()
{
checkNoReentry();
}
int32 Recurse(int32 A, int32 B)
{
checkNoRecursion();
return Recurse(A - 1, B - 1);
}
unimplemented()
함수내 구현이 없어서 특정 클래스에서 호출이 안되거나 오버라이딩 해야하는 함수를 표시하는데 사용
class FNoImpl
{
virtual void DoStuff()
{
// You must override this
unimplemented();
}
};
ensure(표현식)
표현식 검증에 실패하면 콜스택을 생성
if (ensure( InObject != NULL ))
{
InObject->Modify();
}
ensureMsg(표현식, 메시지)
ensure에 메시지를 추가한 버전
ensureMsg(Node != nullptr, TEXT("Node is invalid"));
ensureMsgf(표현식, 메시지, ...)
checkf나 verifyf처럼 컨텍스트 정보를 포함해서 메시지를 출력할 수 있도록 함
if (ensureMsgf(!bModal, TEXT("Could not create dialog because modal is set to (%d)"), int32(bModal)))
{
...
}
출처 : https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/Assertions/
Asserts
Reference to Unreal Engine 4's Assert Functionality
docs.unrealengine.com
'언리얼 엔진 > 기능' 카테고리의 다른 글
[ C++ ] 정적/동적 라이브러리 (0) | 2023.08.27 |
---|---|
[ UE5 ] Build Process (0) | 2023.08.27 |
[ UE5 ] GetClass vs StaticClass (0) | 2023.08.17 |
[ UE5 ] Garbage Collection (G.C) (0) | 2023.08.13 |
[ UE5 ] 게임플레이 디버거 (0) | 2023.08.06 |