- 在场景中获取某一种类型的Actor,并且调用Actor中的成员函数,使用函数:UGameplayStatics::GetAllActorsOfClass
TSubclassOf<AActor> worldClassObject = ARotatingActor::StaticClass();
TArray<AActor*>actorOfClass;
UGameplayStatics::GetAllActorsOfClass(this, worldClassObject, actorOfClass);
for (AActor* actor : actorOfClass){
ARotatingActor* rotatingActor = Cast<ARotatingActor>(actor);
if (rotatingActor){
rotatingActor->ToggleRotate();
}
}
- 委托
//静态绑定
DECLARE_DELEGATE(FRotateDelegate);
//动态绑定
/**
* _RetVal :表示绑定的函数具有返回值
* _OneParam: 表示绑定的函数有一个参数
* 第一个惨数float: 函数返回值的类型
* 第二个参数FDynamicRotateDelegate :委托的名称
* 第三个参数:函数的参数
* 第四个参数:函数参数的名称
*/
DECLARE_DYNAMIC_DELEGATE_RetVal_OneParam(float, FDynamicRotateDelegate, float, RotationSpeed);
//声明代理
FRotateDelegate RotateDelegate;
FDynamicRotateDelegate DynamicRotatedelegate;
//调用
RotateDelegate.ExecuteIfBound();
float PreviousRotationRate = DynamicRotatedelegate.Execute(Rate);
//委托调用接口
APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
AFirstPawn* firstPawn = Cast<AFirstPawn>(Pawn);
if (firstPawn) {
firstPawn->RotateDelegate.BindUObject(this, &ARotatingActor::ToggleRotate);
firstPawn->DynamicRotatedelegate.BindDynamic(this, &ARotatingActor::SetRotationRate);
}
完整代码:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "FirstPawn.generated.h"
//静态绑定
DECLARE_DELEGATE(FRotateDelegate);
//动态绑定
/**
* _RetVal :表示绑定的函数具有返回值
* _OneParam: 表示绑定的函数有一个参数
* 第一个惨数float: 函数返回值的类型
* 第二个参数FDynamicRotateDelegate :委托的名称
* 第三个参数:函数的参数
* 第四个参数:函数参数的名称
*/
DECLARE_DYNAMIC_DELEGATE_RetVal_OneParam(float, FDynamicRotateDelegate, float, RotationSpeed);
UCLASS()
class TRACELENGTH_API AFirstPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AFirstPawn();
/**Mesh component to give the Pawn a visual representation in the world*/
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* MeshComponent;
/** Camera boom positioning the camera behind the Pawn */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
//移动速度
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Movement")
float moveSpeed = 20.0;
//移动方向
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement")
FVector direction = FVector(0.0);
UFUNCTION(BlueprintCallable)
void ToggleAllRotators();
//声明代理
FRotateDelegate RotateDelegate;
FDynamicRotateDelegate DynamicRotatedelegate;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float RotatingActorRotate = 180.f;
UFUNCTION(BlueprintCallable)
void SetRotatingActorRates(float Rate);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
void moveForward(float value);
void moveRight(float value);
void moveUp(float value);
void lineTraceSingleByChannel();
void lineTraceMultiByChannel();
void boxTraceSingleForObjects();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "FirstPawn.h"
#include "GameFramework\SpringArmComponent.h"
#include "DrawDebugHelpers.h"
#include "Camera\CameraComponent.h"
#include "TraceLength.h"
#include "Kismet\KismetSystemLibrary.h"
#include "RotatingActor.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AFirstPawn::AFirstPawn() {
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
SetRootComponent(Cast<USceneComponent>(MeshComponent));
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
}
void AFirstPawn::ToggleAllRotators() {
RotateDelegate.ExecuteIfBound();
}
void AFirstPawn::SetRotatingActorRates(float Rate) {
float PreviousRotationRate = DynamicRotatedelegate.Execute(Rate);
printf("Previous Rotation Rate : %f", PreviousRotationRate);
}
// Called when the game starts or when spawned
void AFirstPawn::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void AFirstPawn::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
FVector location = GetActorLocation();
FVector detailLocation = direction;
detailLocation.Normalize();
detailLocation *= moveSpeed;
location += detailLocation;
SetActorLocation(location);
lineTraceSingleByChannel();
}
// Called to bind functionality to input
void AFirstPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AFirstPawn::moveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AFirstPawn::moveRight);
PlayerInputComponent->BindAxis("MoveUp", this, &AFirstPawn::moveUp);
}
void AFirstPawn::moveForward(float value) {
//UE_LOG(LogTemp, Warning, TEXT("moveForward:%f"), value);
direction.X = value;
}
void AFirstPawn::moveRight(float value) {
//UE_LOG(LogTemp, Warning, TEXT("moveRight: %f"), value);
direction.Y = value;
}
void AFirstPawn::moveUp(float value) {
direction.Z = value;
}
void AFirstPawn::lineTraceSingleByChannel() {
FHitResult HitResult;
FVector Start = GetActorLocation() /*+ FVector(0.f, 0.f, 75.f)*/;
FVector End = Start + GetActorForwardVector() * 500.f;
FCollisionQueryParams CollisionQueryParams;
CollisionQueryParams.AddIgnoredActor(this);
GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Visibility, CollisionQueryParams);
if (HitResult.bBlockingHit) {
//DrawDebugSphere(GetWorld(), HitResult.Location, 15.f, 12, FColor::Red, false, 5.f);
//DrawDebugLine(GetWorld(), Start, HitResult.Location, FColor::Red, true, 3.f);
DrawDebugPoint(GetWorld(), HitResult.Location, 5.f, FColor::Blue, false, 5.f);
}
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "RotatingActor.generated.h"
UCLASS()
class TRACELENGTH_API ARotatingActor : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARotatingActor();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Item|Mesh") class
UStaticMeshComponent* Mesh;
//控制旋转
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Item|ItemProperties")
bool bRotate = false;
//旋转角度
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|ItemProperties")
float RotationRate = 45.0;
UFUNCTION()
float SetRotationRate(float Rate);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void ToggleRotate();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "RotatingActor.h"
#include "FirstPawn.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ARotatingActor::ARotatingActor() {
// 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;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
RootComponent = Cast<USceneComponent>(Mesh);
}
float ARotatingActor::SetRotationRate(float Rate) {
float Temp = RotationRate;
RotationRate = Rate;
return Temp;
}
// Called when the game starts or when spawned
void ARotatingActor::BeginPlay() {
Super::BeginPlay();
APawn* Pawn = UGameplayStatics::GetPlayerPawn(this, 0);
AFirstPawn* firstPawn = Cast<AFirstPawn>(Pawn);
if (firstPawn) {
firstPawn->RotateDelegate.BindUObject(this, &ARotatingActor::ToggleRotate);
firstPawn->DynamicRotatedelegate.BindDynamic(this, &ARotatingActor::SetRotationRate);
}
}
// Called every frame
void ARotatingActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
if (bRotate) {
FRotator Rotation = GetActorRotation();
Rotation.Yaw += DeltaTime * RotationRate;
SetActorRotation(Rotation);
}
}
void ARotatingActor::ToggleRotate() {
bRotate = !bRotate;
}
BP_FirstPawn
aaa