๐
2026-04-04
์ฃผ๋ง์๋ ์กฐ๊ธ์ฉ์ ๊ณต๋ถํด๋ณด๋ ค๊ณ ํ๋ค.
Delegate๋ ํจ์๋ฅผ ๋ณ์์ฒ๋ผ ์ ์ฅํ๊ณ ๋์ค์ ํธ์ถํ ์ ์๊ฒ ํ๋ ๊ฒ
Delegate = ๋จ์ ํจ์ ํฌ์ธํฐ + ๊ธฐ๋ฅ ํ์ฅ
"A์ด๋ฒคํธ ๋ฐ์ ์ Bํจ์ ์คํํด์ค"
C++์๋ Delegate ํค์๋๋ก์๊ฐ ์๋ ๊ฐ๋
์ผ๋ก ์กด์ฌ
Delegate๋ฅผ ๊ตฌํํ๋ ๋ค์ํ ๋ฐฉ๋ฒ ์กด์ฌ
void Hello()
{
std::cout << "Hello";
}
...
int main()
{
void (*func)() = Hello;
func();
}
๊ฐ์ฅ ๊ธฐ๋ณธ์ด์ง๋ง ์ ์ฐ์ฑ์ด ์์
void Hello()
{
std::cout << "Hello";
}
...
int main()
{
std::function<void()> func = Hello;
func();
}
ํจ์, ๋๋ค, ๋ฉค๋ฒ ํจ์ ๋ค ๊ฐ๋ฅ
๋งค์ฐ ์ ์ฐํจ
std::function<void()>
voidํ์์ ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ํจ์๋ฅผ ๋ด๋ ์ปจํ
์ด๋๋ฅผ ์ ์ธ
func = Hello;
func์ Hello ํจ์์ ์ฃผ์๋ฅผ ์ ์ฅ
auto func = []() {
std::cout << "Lambda";
};
func();
inline delegate ๋๋
์ฝ๋ฐฑํจ์๋ก ๋ง์ด ์ฌ์ฉ
unreal์์๋ Delegate๋ฅผ ์ด๋ฒคํธ ์์คํ ์ผ๋ก ๋ง์ด ์ฌ์ฉ
ํ๋์ ํจ์๋ง ์ฐ๊ฒฐ
์ฌ๋ฌ ํจ์ ์ฐ๊ฒฐ ๊ฐ๋ฅ (์ด๋ฒคํธ st)
๋ธ๋ฃจ ํ๋ฆฐํธ ์ฐ๋ ๊ฐ๋ฅ
DECLARE_DELEGATE(FMyDelegate);
void MyFunction()
{
UE_LOG(LogTemp, Warning, TEXT("Hello Delegate"));
}
FMyDelegate Delegate;
Delegate.BindStatic(&MyFunction);
Delegate.Execute();
DECLARE_MULTICAST_DELEGATE(FmyMultiDelegate);
FMyMultiDelegate OnSomething;
void Func1() { ... }
void Func2() { ... }
OnSomething.AddStatic(&Func1);
OnSomething.AddStatic(&Func2);
OnSomething.Broadcast();
OnDamaged.Broadcast(NewHealth);
๋ฐ๋ฏธ์ง ์ ์ ๊ฒฝ์ฐ NewHealth ํจ์ ์คํ
InputComponent->
BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
๋ด๋ถ์ ์ผ๋ก Delegate ์ฌ์ฉ
InputComponent->
InputComponent์ ๋ฉค๋ฒ ์ ๊ทผ
BindAction( ... )
ํจ์ ํธ์ถ
๋ด๋ถ์ธ์๋ค ("Jump", IE_Pressed, this, &ACharacter::Jump)
Jump
์ก์
์ด๋ฆ
IE_Pressed
์
๋ ฅ ์ด๋ฒคํธ ํ์
this
ํ์ฌ ๊ฐ์ฒด
&ACharacter::Jump();
์ด ํด๋์ค์ Jump ํจ์ ์ฃผ์
GetWorld()->GetTimerManager().SetTimer(
TimerHandle,
this,
&AMyActor::MyFunction,
1.0f,
false
);
ํน์ ์๊ฐ ํ ํจ์ ํธ์ถ