赞
踩
获取到引用了,就可以自然的调用。忽略
- #pragma once
-
-
- #include "myue521Character.h"
- #include "CoreMinimal.h"
- #include "Engine/GameInstance.h"
- #include "WebSockets/Public/IWebSocket.h"
- #include "UWebSocketGameInstance.generated.h"
-
- DECLARE_DELEGATE_OneParam(JSONMessageTrigger, FString)
- /**
- *
- */
- UCLASS()
- class MYUE521_API UUWebSocketGameInstance : public UGameInstance
- {
-
- GENERATED_BODY()
- public:
- JSONMessageTrigger JSONMessageTrigger;
-
- public:
- virtual void Init() override;
- virtual void Shutdown() override;
- TSharedPtr<IWebSocket> WebSocket;
-
-
-
- void OnConnected();
- void OnConnectionError(const FString& Error);
- void OnClosed(int32 StatusCode, const FString& Reason, bool bWasClean);
- void OnMessage(const FString& Message); // 接收消息时
- void OnMessageSent(const FString& MessageString); // 发送消息时
- private:
- Amyue521Character* myCharacher;
- public:
- Amyue521Character* getMyCharacher();
-
-
- };
说明,
- Amyue521Character* UUWebSocketGameInstance::getMyCharacher() {
- if (this->myCharacher != nullptr) {
- return this->myCharacher;
- }
- //获取player,并转化为Amyue521Character
- ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
- this->myCharacher = Cast<Amyue521Character>(character);
-
- if (this->myCharacher != nullptr) {
- //转化成功后则进行绑定
- JSONMessageTrigger.BindUObject(this->myCharacher, &Amyue521Character::CallBackFunMul);
-
- //模拟触发,这个触发实际应该放到收到message方法里执行
- FString TestHUDString = FString(TEXT("my json msg"));
- JSONMessageTrigger.ExecuteIfBound(TestHUDString);
- }
- return this->myCharacher;
-
- }
-
- void UUWebSocketGameInstance::OnConnected()
- {
- UE_LOG(LogTemp, Warning, TEXT("%s"), *FString(__FUNCTION__));
-
- GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, "Successfully Connected");
-
- this->getMyCharacher();
-
- }
说明:
这里进行了代码简化,链接成功后将获取的player转化为目标Characher
- ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
- this->myCharacher = Cast<Amyue521Character>(character);
绑定
- if (this->myCharacher != nullptr) {
- JSONMessageTrigger.BindUObject(this->myCharacher, &Amyue521Character::CallBackFunMul);
-
- }
触发,这个触发实际应该放到收到OnMessage方法里执行
- if (this->myCharacher != nullptr) {
- FString TestHUDString = FString(TEXT("my json msg"));
- JSONMessageTrigger.ExecuteIfBound(TestHUDString);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。