首页 > 编程语言 >传送,条件加速 Learn Unreal Engine (with C++)

传送,条件加速 Learn Unreal Engine (with C++)

时间:2023-04-16 21:31:46浏览次数:31  
标签:Engine 多播 绑定 void OtherActor AActor Unreal C++ ATeleporterActor

传送,条件加速 Learn Unreal Engine (with C++)_c++

传送

  1. pawn进入box触发OnActorBeginOverlap
  2. 获取目标位置,下一帧将pawn坐标更改为目标位置

传送,条件加速 Learn Unreal Engine (with C++)_ue4_02

  • 首先需要重叠函数与开始重叠事件绑定
OnActorBeginOverlap.AddDynamic(this, &ATeleporterActor::OnOverlapBegin);
  • 头文件声明
UPROPERTY(EditAnywhere)
		ATeleporterActor* Target = nullptr; //目标位置,在蓝图中设置比较方便
	UPROPERTY(EditAnywhere)
		USoundCue* TeleportSound;//声音
	UFUNCTION()
		void OnOverlapBegin(AActor* TeleporterActor, AActor* OtherActor);//触发重叠后执行的操作
  • 实现
void ATeleporterActor::TeleportToTarget(AActor * Actor)
{
	//获取传送目标名为"Spawn"的场景组件
	USceneComponent* TargetSpawn = Cast<USceneComponent>(Target->GetDefaultSubobjectByName("Spawn"));
	UGameplayStatics::PlaySound2D(this, TeleportSound);
	Actor->SetActorLocation(TargetSpawn->GetComponentLocation());//更改坐标
}

void ATeleporterActor::OnOverlapBegin(AActor * TeleporterActor, AActor * OtherActor)
{
	if (OtherActor->ActorHasTag("Pacman")) {
		//下一帧,调用传送函数
		GetWorldTimerManager().SetTimerForNextTick([OtherActor, this]() { TeleportToTarget(OtherActor); });
	}
}

Target->GetDefaultSubobjectByName获取名为xxx的子对象,例如本游戏中就是获取ATeleporterActor名为spawn的子对象


传送,条件加速 Learn Unreal Engine (with C++)_虚幻_03

条件加速

当吃豆人吃到特殊的豆子的时候就会加速,这使用了动态多播1主要目的是降低对象之间的耦合,代码更加清晰简洁

动态多播:观察者模式, 动态即支持蓝图序列化,即可在蓝图中绑定事件,但蓝图获取不到在C++中定义的动态多播的实例引用,即使用元数据 BlueprintReadWrite 标记也不行,但可以通过 【Assign 实例名称】 的蓝图节点为在C++中定义的动态多播对象绑定新的委托函数

  1. 加速豆被吃操作绑定到豆被吃事件上
  2. 每次有豆被吃事件发生的时候,都会广播当前豆子的类型
  3. 普通豆被吃无反应
  4. 加速豆被吃就会执行加速豆被吃事件
  • 注册动态多播
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FFoodieEatenEvent, EFoodieType, FoodieType);
  • 绑定事件

忽略下面的error,不知道为什么,重新打开的时候就这样了,但还能正常运行…

  • 广播豆子类型,当豆子被吃掉的时候
void AFoodie::Consume()
{
	UGameplayStatics::PlaySound2D(this, ConsumptionSound);
	FoodieEatenEvent.Broadcast(FoodieType); // 广播类型
	Destroy();
}

传送,条件加速 Learn Unreal Engine (with C++)_正常运行_04


  1. https://mp.weixin.qq.com/s/Vliuv3jfUWU_1VvWSBx70w ↩︎


标签:Engine,多播,绑定,void,OtherActor,AActor,Unreal,C++,ATeleporterActor
From: https://blog.51cto.com/u_16062556/6193589

相关文章

  • c++训练打卡(8)
    冒泡排序流程图:伪代码:源代码:#include<stdio.h>intmain(){ intN,i,j,Max; inta[100]; printf("请输入要比较的数据的个数:"); scanf("%d",&N); printf("请输入所要比较的数据:"); for(i=0;i<N;i++){ scanf("%d",&a[i]); } for(i=0;i<N;i......
  • C++ —— 重载、重写和重定义
    1重载一般是类内部方法的关系classMyClass{public:voidMyPrint();voidMyPrint(intcnt);voidMyPrint(intcnt,conststring&msg);};2重写一般父子类中方法的关系对父类虚函数进行重载classMyClass{public:virtualvoidMyPrint();};c......
  • c++文件操作
    include<iostream>#include<fstream>usingnamespacestd;#include<string>voidtest01(){stringl;ofstreama;a.open("test.txt",ios::out);/*getline(cin,l);*///可以正常写入空格a<<"你好!!!"<......
  • C++实现多线程
    #include<iostream>#include<chrono>#include<thread>voidprintNumbers1(){for(inti=1;i<=10000;i++){std::cout<<"Thread1:"<<i<<std::endl;}}voidprintNumbers2(){for......
  • C++动态数组(vector.h)
    #include<iostream>#include<vector>intmain(){std::vector<std::string>con;con.push_back("9999");std::cout<<con[0];return0;}vector搞了一个多态,你可以随便赋值和数组一样,不过是动态的,读取的话vector有自带的比for更优雅的方式......
  • C++ auto关键字
    auto 是C++11中新增的一种类型推导关键字,可以根据变量的初始化表达式,自动推导出相应的类型。使用 auto 可以简化代码,减少类型错误的发生,提高代码的可读性和可维护性。下面是 auto 的使用示例,假设我们有一个整数变量 x,可以这样使用 auto 进行类型推导:autox=10;/......
  • C++的namespace
    这个也是和Java不同的地方,作用是为了防止类的名字冲突#include<iostream>namespacemyspace{classA{public:std::stringhead;private:std::stringbody;};}namespacemyspace2{classA{public:......
  • c++打卡第六天
    Ⅰ一、问题描述定义一个函数判断一个数是否为质数。二、设计思路①输入一个数,同时进入函数判断。②进入函数可以通过for循环判断n是否为质数③当这个数为1时,不是质数,而当这个数是23时,是质数。④可以通过循环质因数,其范围到n的开平方。三、流程图四、代码实现 #in......
  • C++中的继承
    #include<iostream>classA{public:std::stringhead;voidhello(std::stringstr){std::cout<<str<<head<<std::endl;}};intmain(){Aa;std::stringstr="hello";a.head......
  • C++访问控制public private
    #include<iostream>classA{public:std::stringhead;private:std::stringbody;};intmain(){Aa;a.head="888";a.body="999";return0;}报错结果main.cpp:Infunction‘intmain()’:main.c......