getstringfield">getstringfield">
当前位置:   article > 正文

Ue5 websocket控制Character前后左右动作_if (root->hasfield(text("topic"))) { fstring topic

if (root->hasfield(text("topic"))) { fstring topic = root->getstringfield

# myue521Character.h #

  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "GameFramework/Character.h"
  5. #include "InputActionValue.h"
  6. #include "myue521Character.generated.h"
  7. UCLASS(config=Game)
  8. class Amyue521Character : public ACharacter
  9. {
  10. public:
  11. void NotifyServer();
  12. virtual void myJump();
  13. public:
  14. FString Str = TEXT("walawala");
  15. FString topicLogKey = TEXT("log");
  16. FString walkLogKey = TEXT("walk");
  17. FString Forward = TEXT("Forward");
  18. FString Right = TEXT("Right");
  19. void TriggeredBySocketMsg(FString jsonString);
  20. void doMyForward(int f);// 1或者-1
  21. void doMyRight(int r);// 1或者-1
  22. };

# myue521Character.cpp #

  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. #include "myue521Character.h"
  3. #include "Camera/CameraComponent.h"
  4. #include "Components/CapsuleComponent.h"
  5. #include "Components/InputComponent.h"
  6. #include "GameFramework/CharacterMovementComponent.h"
  7. #include "GameFramework/Controller.h"
  8. #include "GameFramework/SpringArmComponent.h"
  9. #include "EnhancedInputComponent.h"
  10. #include "EnhancedInputSubsystems.h"
  11. #include "UObject/ConstructorHelpers.h"
  12. #include "UWebSocketGameInstance.h"
  13. //
  14. // Amyue521Character
  15. void Amyue521Character::myJump()
  16. {
  17. bPressedJump = true;
  18. JumpKeyHoldTime = 0.0f;
  19. this->NotifyServer();
  20. }
  21. void Amyue521Character::Look(const FInputActionValue& Value)
  22. {
  23. // input is a Vector2D
  24. FVector2D LookAxisVector = Value.Get<FVector2D>();
  25. if (Controller != nullptr)
  26. {
  27. // add yaw and pitch input to controller
  28. AddControllerYawInput(LookAxisVector.X);
  29. AddControllerPitchInput(LookAxisVector.Y);
  30. }
  31. }
  32. void Amyue521Character::NotifyServer() {
  33. //从Character里获取UGameInstance的例子
  34. UUWebSocketGameInstance* GameInstance = Cast<UUWebSocketGameInstance>(GetGameInstance());
  35. if (GameInstance) {
  36. if (GameInstance->WebSocket->IsConnected()) {
  37. GameInstance->WebSocket->Send("FROM UnrealEngine 5");
  38. }
  39. }
  40. }
  41. void Amyue521Character::TriggeredBySocketMsg(FString jsonString)
  42. {
  43. //UE_LOG(LogTemp, Warning, TEXT("%s 事件触发调用了我的函数CallBackFunMul。%s-%s"), *FString(__FUNCTION__), *(this->Str), *jsonString);
  44. //GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, FString::Printf(TEXT("事件触发调用了我的函数CallBackFunMul,%s-%s"), *(this->Str), *jsonString));
  45. TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(jsonString);
  46. TSharedPtr<FJsonObject> Root;
  47. if (FJsonSerializer::Deserialize(Reader, Root))
  48. {
  49. if (Root->HasField(TEXT("Topic"))) {
  50. FString Topic = Root->GetStringField(TEXT("Topic"));
  51. TSharedPtr<FJsonObject, ESPMode::ThreadSafe> Data = Root->GetObjectField(TEXT("Data"));
  52. // TSharedPtr<FJsonObject, ESPMode::ThreadSafe> ObjectField = Object->GetObjectField(TEXT("realtime"));
  53. FString KeyField = Data->GetStringField("Key");
  54. FString ValueField = Data->GetStringField("Value");
  55. // log类型打印log
  56. if (this->topicLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
  57. GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, ValueField);
  58. }else if (this->walkLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
  59. FString DirectionField = Data->GetStringField("Direction");
  60. if (this->Forward.Equals(DirectionField, ESearchCase::IgnoreCase)) {
  61. this->doMyForward(FCString::Atoi(*ValueField));
  62. }else if (this->Right.Equals(Right, ESearchCase::IgnoreCase)) {
  63. this->doMyRight(FCString::Atoi(*ValueField));
  64. }
  65. }
  66. else {
  67. GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "1-Key: " + KeyField + ",Value:" + ValueField);
  68. }
  69. }
  70. else {
  71. GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "解析json异常");
  72. }
  73. }
  74. }
  75. void Amyue521Character::doMyForward(int f) // 1或者-1
  76. {
  77. FVector2D MovementVector = FInputActionValue::Axis2D(0, f);
  78. if (Controller != nullptr)
  79. {
  80. // find out which way is forward
  81. const FRotator Rotation = Controller->GetControlRotation();
  82. const FRotator YawRotation(0, Rotation.Yaw, 0);
  83. // get forward vector
  84. const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  85. // get right vector
  86. const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  87. // add movement
  88. AddMovementInput(ForwardDirection, MovementVector.Y);
  89. AddMovementInput(RightDirection, MovementVector.X);
  90. }
  91. }
  92. void Amyue521Character::doMyRight(int r)// 1或者-1
  93. {
  94. FVector2D MovementVector = FInputActionValue::Axis2D(r, 0);
  95. if (Controller != nullptr)
  96. {
  97. // find out which way is forward
  98. const FRotator Rotation = Controller->GetControlRotation();
  99. const FRotator YawRotation(0, Rotation.Yaw, 0);
  100. // get forward vector
  101. const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  102. // get right vector
  103. const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  104. // add movement
  105. AddMovementInput(ForwardDirection, MovementVector.Y);
  106. AddMovementInput(RightDirection, MovementVector.X);
  107. }
  108. }

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

闽ICP备14008679号