U++1.代理

动态代理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   }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值