传送,条件加速 Learn Unreal Engine (with C++)
生活随笔
收集整理的這篇文章主要介紹了
传送,条件加速 Learn Unreal Engine (with C++)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文以吃豆人游戲為例UE4項目: 自制UE4 小游戲 (gitee.com)
傳送
-
首先需要重疊函數與開始重疊事件綁定
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的子對象
條件加速
當吃豆人吃到特殊的豆子的時候就會加速,這使用了動態多播1主要目的是降低對象之間的耦合,代碼更加清晰簡潔
動態多播:觀察者模式, 動態即支持藍圖序列化,即可在藍圖中綁定事件,但藍圖獲取不到在C++中定義的動態多播的實例引用,即使用元數據 BlueprintReadWrite 標記也不行,但可以通過 【Assign 實例名稱】 的藍圖節點為在C++中定義的動態多播對象綁定新的委托函數
-
注冊動態多播
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FFoodieEatenEvent, EFoodieType, FoodieType); -
綁定事件
忽略下面的error,不知道為什么,重新打開的時候就這樣了,但還能正常運行…
-
廣播豆子類型,當豆子被吃掉的時候
void AFoodie::Consume() {UGameplayStatics::PlaySound2D(this, ConsumptionSound);FoodieEatenEvent.Broadcast(FoodieType); // 廣播類型Destroy(); }
https://mp.weixin.qq.com/s/Vliuv3jfUWU_1VvWSBx70w ??
總結
以上是生活随笔為你收集整理的传送,条件加速 Learn Unreal Engine (with C++)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 删库造成损失 0.87 亿,微盟程序员被
- 下一篇: 计算机基础:图形、图像相关知识笔记