当前位置:   article > 正文

UE5 C++ UGameInstance 功能、作用及应用_ue5 gameinstance

ue5 gameinstance

# UE5 C++ UGameInstance  功能及作用
 


网上有很多文章介绍,例如在游戏中只有一个实例,换关卡不会丢失等。暂时省略。

#  UE5 C++ UGameInstance  应用


## 应用一,UE5 C++ UGameInstance  里监听player创建事件


UWebSocketGameInstance.h里的定义

  1. UCLASS()
  2. class MYUE521_API UUWebSocketGameInstance : public UGameInstance
  3. {
  4. GENERATED_BODY()
  5. public:
  6. virtual void Init() override;
  7. virtual void Shutdown() override;
  8. void OnPlayerCreated(ULocalPlayer* player);
  9. }

说明:

  1.  Init()方法是在UGameInstance.h里定义的。源码里的注解是允许在这里自定义一些实现
  2. Shutdown()方法是在UGameInstance.h里定义的。源码里的注解是允许在这里自定义一些清理工作
  3. void OnPlayerCreated(ULocalPlayer* player) 方法是我自己定义的方法,用来监听创建player以后执行的方法

下面来看一下实现UWebSocketGameInstance.cpp

  1. void UUWebSocketGameInstance::Init() {
  2. Super::Init();
  3. OnLocalPlayerAddedEvent.AddUObject(this,&UUWebSocketGameInstance::OnPlayerCreated);
  4. }
  5. void UUWebSocketGameInstance::Shutdown() {
  6. //先写自定义的清理工作
  7. Super::Shutdown();
  8. }
  9. void UUWebSocketGameInstance::OnPlayerCreated(ULocalPlayer* player) {
  10. UE_LOG(LogTemp, Warning, TEXT("%s plaer is created"), *FString(__FUNCTION__));
  11. }

说明:

  1.  UE5里的事件绑定,这里绑定到自定义的OnPlayerCreated 方法里
  2. 触发事件OnLocalPlayerAddedEvent的位置在GameInstance.cpp里有AddLocalPlayer(ULocalPlayer* NewLocalPlayer, FPlatformUserId UserId)方法里有OnLocalPlayerAddedEvent.Broadcast(NewLocalPlayer); 通过广播通知


## 应用二,UE5 C++ UGameInstance  里player转成目标Characher的方法


  1. ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
  2. Amyue521Character* character2 = Cast<Amyue521Character>(character);
  3. if (character2 == nullptr) {
  4. UE_LOG(LogTemp, Warning, TEXT("%s 1强制类型转换成Amyue521Character失败"), *FString(__FUNCTION__));
  5. }
  6. else {
  7. UE_LOG(LogTemp, Warning, TEXT("%s 1强制类型转换成Amyue521Character成功"), *FString(__FUNCTION__));
  8. }

## 应用三,UE5 C++ UGameInstance  里一些方法罗列

  1. /** Returns number of fully registered local players */
  2. int32 GetNumLocalPlayers() const;
  3. /** Returns the local player at a certain index, or null if not found */
  4. ULocalPlayer* GetLocalPlayerByIndex(const int32 Index) const;
  5. /** Returns the first local player, will not be null during normal gameplay */
  6. ULocalPlayer* GetFirstGamePlayer() const;
  7. /** Returns the player controller assigned to the first local player. If World is specified it will search within that specific world */
  8. APlayerController* GetFirstLocalPlayerController(const UWorld* World = nullptr) const;
  9. /** Returns the local player assigned to a physical Controller Id, or null if not found */
  10. ULocalPlayer* FindLocalPlayerFromControllerId(const int32 ControllerId) const;
  11. /** Returns the local player assigned to this platform user id, or null if not found */
  12. ULocalPlayer* FindLocalPlayerFromPlatformUserId(const FPlatformUserId UserId) const;
  13. /** Returns the local player that has been assigned the specific unique net id */
  14. ULocalPlayer* FindLocalPlayerFromUniqueNetId(FUniqueNetIdPtr UniqueNetId) const;
  15. ULocalPlayer* FindLocalPlayerFromUniqueNetId(const FUniqueNetId& UniqueNetId) const;
  16. ULocalPlayer* FindLocalPlayerFromUniqueNetId(const FUniqueNetIdRepl& UniqueNetId) const;
  17. /** Returns const iterator for searching list of local players */
  18. TArray<ULocalPlayer*>::TConstIterator GetLocalPlayerIterator() const;
  19. /** Returns reference to entire local player list */
  20. const TArray<ULocalPlayer*> & GetLocalPlayers() const;

#  UE5 C++ UGameInstance  其他

待补充

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/735676
推荐阅读
相关标签
  

闽ICP备14008679号