【UE5】C++委托开灯开门

工程名为myproject
开关

MyTrigger.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyTrigger.generated.h"
UCLASS()
class MYPROJECT_API AMyTrigger : public AActor
{
	GENERATED_BODY()
public:	
	// Sets default values for this actor's properties
	AMyTrigger();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UPROPERTY()
	class UBoxComponent* MyTriggerride;
	UFUNCTION()
	void jinru(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	void chulai(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};

MyTrigger.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyTrigger.h"
#include "Components/BoxComponent.h"
#include "Kismet/GameplayStatics.h"
#include "myproject/myprojectGameMode.h"
// Sets default values
AMyTrigger::AMyTrigger()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	MyTriggerride = CreateDefaultSubobject<UBoxComponent>(TEXT("My Trigger"));
}
// Called when the game starts or when spawned
void AMyTrigger::BeginPlay()
{
	Super::BeginPlay();
	MyTriggerride->OnComponentBeginOverlap.AddDynamic(this,&AMyTrigger::jinru);
	MyTriggerride->OnComponentEndOverlap.AddDynamic(this, &AMyTrigger::chulai);
}

// Called every frame
void AMyTrigger::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}
void AMyTrigger::jinru(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Error, TEXT("biaogewo jinlai le o"));//
	UWorld* MyWorld = GetWorld();
	if (MyWorld)
	{
		AGameModeBase* GameModeBase = UGameplayStatics::GetGameMode(MyWorld);
		AmyprojectGameMode* MyGameMode = Cast<AmyprojectGameMode>(GameModeBase);
		if (MyGameMode)
		{
			MyGameMode->MyDelegate.Broadcast(true);
		}
	}
}
void AMyTrigger::chulai(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	UE_LOG(LogTemp, Error, TEXT("wo chulai le o biaoguo"));//
	UWorld* MyWorld = GetWorld();
	if (MyWorld)
	{
		AGameModeBase* GameModeBase = UGameplayStatics::GetGameMode(MyWorld);
		AmyprojectGameMode* MyGameMode = Cast<AmyprojectGameMode>(GameModeBase);
		if (MyGameMode)
		{
			//MyGameMode->MyDelegate.ExecuteIfBound(false);
			MyGameMode->MyDelegate.Broadcast(false);
		}
	}
}

灯和门:

MyLight.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyLight.generated.h"
UCLASS(Blueprintable)
class MYPROJECT_API AMyLight : public AActor
{
	GENERATED_BODY()
public:
	// Sets default values for this actor's properties
	AMyLight();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UPROPERTY(EditAnywhere)
	class USpotLightComponent* MyLight;
	UFUNCTION()
	void kaiguan(bool value);
};
MyLight.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyLight.h"
#include "Components/SpotLightComponent.h"
#include "Kismet/GameplayStatics.h"

#include "myproject/myprojectGameMode.h"
// Sets default values
AMyLight::AMyLight()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	MyLight = CreateDefaultSubobject<USpotLightComponent>(TEXT("My Light"));
}

// Called when the game starts or when spawned
void AMyLight::BeginPlay()
{
	Super::BeginPlay();
	MyLight->SetVisibility(0);
	UWorld* MyWorld = GetWorld();
	if (MyWorld)
	{
		AGameModeBase* GameModeBase = UGameplayStatics::GetGameMode(MyWorld);
		AmyprojectGameMode* MyGameMode = Cast<AmyprojectGameMode>(GameModeBase);
		if (MyGameMode)
		{
			MyGameMode->MyDelegate.AddUObject(this, &AMyLight::kaiguan);
		}
	}
}

// Called every frame
void AMyLight::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}
void AMyLight::kaiguan(bool value)
{
	MyLight->SetVisibility(value);
}
MyDoor.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyDoor.generated.h"
UCLASS()
class MYPROJECT_API AMyDoor : public AActor
{
	GENERATED_BODY()
public:	
	// Sets default values for this actor's properties
	AMyDoor();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	UPROPERTY(EditAnywhere)
	class UStaticMeshComponent* MyDoorMesh;
	UPROPERTY(EditAnywhere)
	class USceneComponent* MyDoor;
	UFUNCTION()
	void xuanzhuan(bool value);
};
MyDoor.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyDoor.h"
#include "Components/BoxComponent.h"//即便代码里没有用到,但是没有这一句,就没法在蓝图里改材质
#include "Kismet/GameplayStatics.h"
#include "myproject/myprojectGameMode.h"
// Sets default values
AMyDoor::AMyDoor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	MyDoor = CreateDefaultSubobject<USceneComponent>(TEXT("My Door"));
	MyDoorMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("My DoorMesh"));
	MyDoorMesh->SetupAttachment(MyDoor);
}
// Called when the game starts or when spawned
void AMyDoor::BeginPlay()
{
	Super::BeginPlay();
	UWorld* MyWorld = GetWorld();
	if (MyWorld)
	{
		AGameModeBase* GameModeBase = UGameplayStatics::GetGameMode(MyWorld);
		AmyprojectGameMode* MyGameMode = Cast<AmyprojectGameMode>(GameModeBase);
		if (MyGameMode)
		{
			//MyGameMode->MyDelegate.BindUObject(this, &AMyDoor::xuanzhuan);
			MyGameMode->MyDelegate.AddUObject(this, &AMyDoor::xuanzhuan);
			// BindUObject( SourceType* InUserObject, typename FGetter::template TConstMethodPtr< SourceType > InMethodPtr )
		}
	}
}

// Called every frame
void AMyDoor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}
void AMyDoor::xuanzhuan(bool value)
{
	if (value)
	{
		//MyDoorMesh->SetRelativeRotation(FRotator(0.0f, 90.0f, 0.0f));
		//MyDoorMesh->MoveComponent(FVector(0.0f, 90.f, 0.0f), FQuat(1.0f), 0, new FHitResult, MOVECOMP_IgnoreBases);
		const FLatentActionInfo LatentInfo(0, FMath::Rand(), TEXT("DelayFinish"), this);
		UKismetSystemLibrary::MoveComponentTo(MyDoor, FVector(1600.0f, 1200.0f, 240.0f), FRotator(0.0f, 0.0f, 0.0f), 0, 0, 3, 1, EMoveComponentAction::Move, LatentInfo);		
	}
	else {
		const FLatentActionInfo LatentInfo(0, FMath::Rand(), TEXT("DelayFinish"), this);
		UKismetSystemLibrary::MoveComponentTo(MyDoor, FVector(1600.0f, 1200.0f, 240.0f), FRotator(0.0f, 90.0f, 0.0f), 0, 0, 3, 1, EMoveComponentAction::Move, LatentInfo);
	}	
}

游戏模式:
myprojectGameMode.h

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "myprojectGameMode.generated.h"
DECLARE_DELEGATE_OneParam(FDelegate2, bool)

DECLARE_MULTICAST_DELEGATE_OneParam(FDelegate, bool)
UCLASS(minimalapi)
class AmyprojectGameMode : public AGameModeBase
{
	GENERATED_BODY()
	
public:
	AmyprojectGameMode();
	FDelegate MyDelegate;
};

myprojectGameMode.cpp:

// Copyright Epic Games, Inc. All Rights Reserved.

#include "myprojectGameMode.h"
#include "myprojectCharacter.h"
#include "UObject/ConstructorHelpers.h"

AmyprojectGameMode::AmyprojectGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter"));
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值