1. 在场景中获取某一种类型的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();}}
  1. 委托
//静态绑定
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 propertiesAFirstPawn();/**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 spawnedvirtual void BeginPlay() override;public:  // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual 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 propertiesARotatingActor();UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category    = "Item|Mesh") classUStaticMeshComponent* 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 spawnedvirtual void BeginPlay() override;public:// Called every framevirtual 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

UE4使用委托实现Actor之间的通信相关推荐

  1. 【完整代码】Scala AKKA实现两个Actor之间的通信代码示例

    启动程序: package com.zxl.akka.two_actorsimport akka.actor.{ActorRef, ActorSystem, Props}//extends App 可 ...

  2. UE4 UI和UI之间的通信(浅谈测试)

    1.实现效果就是在一个主UI中,点击新建按钮弹出一个新建名称的UI,然后输入自己的想要输入的名称,然后点击确认,将输入的名称放到主UI的框中,这里新建3个UI 2.CreatePlayer的设置如下: ...

  3. Scal:Master和worker之间的通信

    ** Scala编程实战 ** 1. 课程目标 1.1. 目标:熟练使用Scala编写程序 2. 项目概述 2.1. 需求 目前大多数的分布式架构底层通信都是通过RPC实现的,RPC框架非常多,比如前 ...

  4. UE4-(蓝图)第二十课蓝图之间的通信(类型转换、公开变量)

    一.使用转换类型节点,将获取到的物体转换到该类型,进行获取对应类型上的变量或者事件.(蓝图类与关卡蓝图通信) 1.使用自定义事件 2.使用类型转换Cast To..节点:想和谁通信就要类型转换到谁,这 ...

  5. Linux 进程及进程之间的通信机制——管道

    参考: LInux C编程从初学到精通 电子工业出版社 Linux 进程 Linux 进程简介 Linux是一个多用户多任务的操作系统,多用户是指多个用户可以在同一时间使用同一台计算机系统:多用户是指 ...

  6. vue中子组件和子组件之间怎么通信_vue.js组件之间如何通信?

    vue.js组件之间如何通信?下面本篇文章就来给大家介绍一下Vue.js组件间通信方式.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 平时在使用Vue框架的业务开发中,组件不仅仅要 ...

  7. JS每日一题: 小程序页面之间如何通信?

    20190227 小程序页面之间如何通信? 首先将通信的模型列举出来, 分为以下几种 兄弟页面间通信 父路径页面向子路径页面通信 子路径页面向父路径页面通信 通信的方式 localStorage 本地 ...

  8. java 本地通信_java – 本地JVM之间的通信

    我的问题:我可以/应该采用什么方法在本地运行的两个或多个JVM实例之间进行通信? 问题的一些描述: 我正在为一个项目开发一个系统,该系统需要单独的JVM实例来完全隔离某些任务. 在它运行时,'父'JV ...

  9. QT小例子GUI(主)线程与子线程之间的通信

    QT小例子GUI(主)线程与子线程之间的通信 在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. #ifndef TQT_H_ ...

最新文章

  1. seci-log 1.11 发布 增加了ftpserver,远程ftp,sftp采集简化配置等功能
  2. 抓包工具tcpdump及分析工具wireshark
  3. 这家刚拿了1亿美元的基金会,要证明“21世纪是生物的世纪”
  4. python导入excel数据-python + Excel数据读取(更新)
  5. 数据库笔记——数据模型
  6. 运维组如何管理服务器资源,运维服务管理体系方案全套.doc
  7. Hibernate,JPA注解@Entity
  8. 英特尔发布至强E-2300服务器处理器,比上一代性能提高17%
  9. java做登录时要加锁吗_你用对锁了吗?谈谈 Java “锁” 事
  10. php对接xenserver,XenServer虚拟机管理工具XenCenter安装配置图文教程
  11. 电商后台管理项目的步骤分析
  12. PLC与工业DTU接线快速入门
  13. OKR成功落地的13条箴言
  14. Java使用itext 生成PDF,以生成个人简历为例
  15. 加密的压缩包文件如何解压
  16. 二十一世纪大学英语读写教程(第二册)学习笔记(原文)——7 - Thinking: A Neglected Art(思考——被忽视的艺术)
  17. LeetCode单词规律解法
  18. DIJ(单源次短路) - Two Paths - HDU 6181
  19. 亚马逊欧洲站点遇见kyc问题审核了怎么办?
  20. windows 网管总结

热门文章

  1. 在线教育的鲶鱼“肥瘦不均”
  2. 阿里对高管的要求,不得不服
  3. 服务器网站权限,在服务器上设置网站权限
  4. 计算机视觉中的数学方法——1平面射影几何——1射影平面+2二次曲线
  5. 最新淘宝商品销量接口API(精准总销月销)
  6. 简单的一道 SQL 题,谈如何提高编程水平
  7. 【报告分享】2021年小红书kol营销白皮-千瓜数据(附下载)
  8. 数商云食品行业数字化供应链转型解决方案
  9. 超频技术之内存“时序”重要参数设置解说
  10. matlab之simulink仿真入门