🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

delegate wrapper 사용 이유

24.05.26 17:41 작성 24.05.27 21:58 수정 조회수 119

0

안녕하세요 좋은 강의 정말 감사드립니다.

다름이 아니라 delegate wrapper 사용 이유가 궁금하여 질문 드립니다.

선생님께서 delegate를 다수 배열로 관리하기 해야 하는데 이 자체를 인자로 쓸 수 없다고 하신 부분이 잘 이해가 되지가 않습니다. 어느 부분을 인자로 쓸 수 없는지에 대해 구체적으로 말씀해주실 수 있나요?

FTakeItemDelegateWrapper 랩퍼 구조체가 아닌 FOnTakeItemDelegate 델리게이트를 TArray 배열에 넣을 수도 있고

이런 식으로 바인드도 되고, 실행도 되는데 왜 델리게이트 랩퍼 구조체를 따로 만들어야하는지가 궁금합니다.

답변 2

·

답변을 작성해보세요.

0

안녕하세요.

FOnTakeItemDelegate 선언은 아래 그대로 사용하셨는지요?

DECLARE_DELEGATE_OneParam(FOnTakeItemDelegate, class UABItemData* /*InItemData*/);

그리고 헤더에서 TakeItemActions2를 어떻게 선언했는지 볼 수 있을까요?

 

답변 감사드립니다.

image제가 실수로 이렇게 선언해서 TakeItemActions2에 UPROPERTY를 적용하지 않아 생긴 문제였습니다. TakeItemActions2에 UPROPERTY를 붙이니 바로 오류가 뜨네요!

 

그렇다면 UPROPERTY 배열에 델리게이트를 넣을 수 없어 랩퍼 구조체에 델리게이트를 넣고 해당 구조체를 UPROPERTY 배열에 넣어주는 것은 이해했는데, 델리게이트 배열에 UPROPERTY를 붙인 이유를 잘 모르겠습니다. 블루 프린트에서 사용하기 위한 목적인가요?

말씀하신대로 Part2 예제처럼 로컬의 C++에서만 사용한다면 상관 없겠지만, 이런 저런 확장까지 감안했을 때 아무래도 UPROPERTY화 시키는게 좋겠지요.

명확하게 이해했습니다. 답변 감사합니다!

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.05.28

안녕하세요, 인프런 AI 인턴입니다.

님의 질문에 대해 답변드리겠습니다.

Delegate wrapper를 사용하는 주된 이유 중 하나는 delegate의 직렬화와 관련된 제약 때문일 수 있습니다. Unreal Engine에서는 게임의 상태를 저장하거나 네트워크를 통해 데이터를 전송할 때 객체의 상태를 직렬화하는 과정이 필요합니다. 그런데, 일반적인 delegate는 직렬화를 지원하지 않아, 게임의 상태 저장이나 네트워크를 통한 상태 공유시에 delegate에 바인딩된 정보를 유지하기 어려울 수 있습니다. 이런 문제를 해결하기 위해 delegate를 감싸는 wrapper 구조체를 사용하면, 구조체를 통해 직렬화를 지원하게 만들어, delegate의 상태를 보다 쉽게 관리할 수 있습니다.

또한, 다수의 delegate를 배열로 관리할 때, 각기 다른 타입의 delegate를 하나의 배열에 저장하고자 할 때 type safety와 관련된 문제를 해결하는 데에도 wrapper 구조체가 유용할 수 있습니다. 각 delegate에 대한 wrapper 구조체를 정의하면, 다양한 형태의 delegate를 안전하게 관리하고, 실행 시 타입 체크를 통해 오류를 줄일 수 있습니다.

사진의 코드를 보면, TakeItemActions 객체에 여러 종류의 액션을 추가하는 과정에서 delegate를 사용하고 있습니다. 여기서 FTakeItemDelegateWrapper 랩퍼 구조체를 사용하는 경우, 이러한 delegate의 집합을 더 유연하게 관리할 수 있으며, 추가적으로 직렬화 지원이나 타입 안정성을 강화하는 역할을 할 수 있습니다. 따라서 이러한 이유로 wrapper 구조체를 별도로 만들어 사용하는 것은 delegate를 폭넓게 활용하기 위한 Unreal Engine 프로그래밍의 한 방식이 될 수 있습니다.

The image shows lines of code, specifically from a programming language that appears to be C++ based on the syntax. The code snippet involves operations on a `TakeItemActions` object, with the `Add()` method being called multiple times to add different action delegates. These delegates seem to be specifying actions such as `EquipWeapon`, `DrinkPotion`, and `ReadScroll` for a character class named `AABCharacterBase`. The syntax indicates that the actions are related to a gameplay mechanics, likely for a video game where a character can perform various actions like equipping a weapon, drinking a potion, or reading a scroll. Each line is creating a delegate that binds a function to an action that can be taken by the character.
The image shows a snippet of source code, possibly from a computer program written in C++. Here is a description of the contents:- The snippet defines a function named `TakeItem` which is a member of a class with the name `AABCharacterBase`. This function takes a single parameter, a pointer to `UABItemData` named `InItemData`.
- Inside the `TakeItem` function, there is an `if` statement that checks if `InItemData` is not null (i.e., it contains a valid address).
- Within the `if` block, there are two function calls:
  - The first function call `TakeItemActions1` casts an integer value (`uint8`, which is an unsigned 8-bit integer) obtained by accessing the `Type` member of the `InItemData` object, then calls `ItemDelegate.ExecuteIfBound` with `InItemData` as an argument.
  - The second function call `TakeItemActions2` does basically the same thing but presumably performs a different set of actions as suggested by the “2” in its name.This code is utilizing some features of C++ like classes, pointers, type casting (to `uint8`), member access (`->`), and possibly delegation patterns (`ItemDelegate.ExecuteIfBound`). However, the code snippet is lacking context, such as the include directives, the definition of `UABItemData`, and what `TakeItemActions1` and `TakeItemActions2` functions actually do, so the exact behavior and purpose of the function in the broader application is not clear.

채널톡 아이콘