工程名为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;
}
}