当前位置:   article > 正文

UE5- c++ websocket里实现调用player里的方法_ue5怎么使用webrtc

ue5怎么使用webrtc

# UGameInstance里直接调用


获取到引用了,就可以自然的调用。忽略

# UGameInstance里间接调用,通过代理调用


  1. 前置已经添加了websocket,具体步骤参考,链接
  2. 在UWebSocketGameInstance.h里新增代理,并在链接成功后进行绑定。
    1. #pragma once
    2. #include "myue521Character.h"
    3. #include "CoreMinimal.h"
    4. #include "Engine/GameInstance.h"
    5. #include "WebSockets/Public/IWebSocket.h"
    6. #include "UWebSocketGameInstance.generated.h"
    7. DECLARE_DELEGATE_OneParam(JSONMessageTrigger, FString)
    8. /**
    9. *
    10. */
    11. UCLASS()
    12. class MYUE521_API UUWebSocketGameInstance : public UGameInstance
    13. {
    14. GENERATED_BODY()
    15. public:
    16. JSONMessageTrigger JSONMessageTrigger;
    17. public:
    18. virtual void Init() override;
    19. virtual void Shutdown() override;
    20. TSharedPtr<IWebSocket> WebSocket;
    21. void OnConnected();
    22. void OnConnectionError(const FString& Error);
    23. void OnClosed(int32 StatusCode, const FString& Reason, bool bWasClean);
    24. void OnMessage(const FString& Message); // 接收消息时
    25. void OnMessageSent(const FString& MessageString); // 发送消息时
    26. private:
    27. Amyue521Character* myCharacher;
    28. public:
    29. Amyue521Character* getMyCharacher();
    30. };

    说明,

    1. 声明一个代理。DECLARE_DELEGATE_OneParam(JSONMessageTrigger, FString)

    2. 声明触发器。public:
          JSONMessageTrigger JSONMessageTrigger;

    3. 下面到实现类里进行绑定,我这里是在UWebSocketGameInstance.cpp里,socket链接成功后进行绑定。

  3. 绑定,并调用。这里尝试着获取到目标characher.
    1. Amyue521Character* UUWebSocketGameInstance::getMyCharacher() {
    2. if (this->myCharacher != nullptr) {
    3. return this->myCharacher;
    4. }
    5. //获取player,并转化为Amyue521Character
    6. ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
    7. this->myCharacher = Cast<Amyue521Character>(character);
    8. if (this->myCharacher != nullptr) {
    9. //转化成功后则进行绑定
    10. JSONMessageTrigger.BindUObject(this->myCharacher, &Amyue521Character::CallBackFunMul);
    11. //模拟触发,这个触发实际应该放到收到message方法里执行
    12. FString TestHUDString = FString(TEXT("my json msg"));
    13. JSONMessageTrigger.ExecuteIfBound(TestHUDString);
    14. }
    15. return this->myCharacher;
    16. }
    17. void UUWebSocketGameInstance::OnConnected()
    18. {
    19. UE_LOG(LogTemp, Warning, TEXT("%s"), *FString(__FUNCTION__));
    20. GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, "Successfully Connected");
    21. this->getMyCharacher();
    22. }

    说明:

    1. 这里进行了代码简化,链接成功后将获取的player转化为目标Characher

      1. ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
      2. this->myCharacher = Cast<Amyue521Character>(character);
    2. 绑定

      1. if (this->myCharacher != nullptr) {
      2. JSONMessageTrigger.BindUObject(this->myCharacher, &Amyue521Character::CallBackFunMul);
      3. }
    3. 触发,这个触发实际应该放到收到OnMessage方法里执行

      1. if (this->myCharacher != nullptr) {
      2. FString TestHUDString = FString(TEXT("my json msg"));
      3. JSONMessageTrigger.ExecuteIfBound(TestHUDString);
      4. }

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

闽ICP备14008679号