动态代理FcriptDelegate
使用方法
1 FScriptDelegate start;
2
3 start.BindUFunction(this,"onOverlap");
4
5
6 myCollision->OnComponentBeginOverlap.Add(start);
7
8
9 void AMyActor::onOverlap(){
10
11 UE_LOG(LogTemp,Warning,TEXT("GOOD"));
12
13 }
标准代理
gamemodebase里
#include "GameFramework/GameModeBase.h"
DECLARE_DELEGATE(FStandardDelegateSignature)
public:
FStandardDelegateSignature myStandardDelegate; //标准代理类的对象 myStandardDelegate
FStandardDelegateSignature myStandardDelegate2;
实现方里
加入gamemodebase的头文件加入点光源头文件
public:
UFUNCTION()
30 void enableLight();
31
32 UFUNCTION()
33 void closeLight();
34
35 UPROPERTY(VisibleAnywhere,BlueprintReadWrite)
36 class UPointLightComponent* pointLight;
加入#include”Kismet/gameplayStatics.h“
构造函数里
pointlight=CreateDefaultSubobject<UPointLightComponent>(TEXT("pointlight"));
pointlight->SetVisibility(false);
pointlight->SetLightBrightness(1000000.0f);
Beginplay里
UWorld* world = GetWorld();
AGameModeBase* modebase=UGameplayStatics::GetGameMode(world);
ATestGameModeBase* mymodebase = Cast<ATestGameModeBase>(modebase);
if (mymodebase)
{
mymodebase->myStandardDelegate.BindUObject(this, &AListener::Enablelight);
mymodebase->myStandardDelegate2.BindUObject(this, &AListener::Closelight);
}
void AListener::Enablelight()
{
pointlight->SetVisibility(true);
}
void AListener::Closelight()
{
pointlight->SetVisibility(false);
}
触发方里
加入合体碰撞的头文件
public:
28 UPROPERTY()
29 class UBoxComponent* box;
30
31 UFUNCTION()
32 virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
33
34 UFUNCTION()
35 virtual void NotifyActorEndOverlap(AActor* OtherActor) override;
源文件里
加入
#include "Kismet\GameplayStatics.h"
加入游戏模式的头文件
构造函数里
box = CreateDefaultSubobject<UBoxComponent>(TEXT("box"));
再加void ATriggerActor::NotifyActorBeginOverlap(AActor* OtherActor)
32 {
33 AGameModeBase *fatherGameModeBase = UGameplayStatics::GetGameMode(GetWorld());
34 AStandardDelegateGameModeBase* myModeBase = Cast<AStandardDelegateGameModeBase>(fatherGameModeBase);
35
36
37 if (myModeBase) {
38 myModeBase->myStandardDelegate.ExecuteIfBound();
39 }
40
41
42 }
43
44 void ATriggerActor::NotifyActorEndOverlap(AActor* OtherActor)
45 {
46 AGameModeBase* fatherGameModeBase = UGameplayStatics::GetGameMode(GetWorld());
47 AStandardDelegateGameModeBase* myModeBase = Cast<AStandardDelegateGameModeBase>(fatherGameModeBase);
48
49
50 if (myModeBase) {
51 myModeBase->myStandardDelegate2.ExecuteIfBound();
52 }
53
54 }