• Ue5 websocket控制Character前后左右动作


    # 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();
    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(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 Root;
    47. if (FJsonSerializer::Deserialize(Reader, Root))
    48. {
    49. if (Root->HasField(TEXT("Topic"))) {
    50. FString Topic = Root->GetStringField(TEXT("Topic"));
    51. TSharedPtr Data = Root->GetObjectField(TEXT("Data"));
    52. // TSharedPtr 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. }

  • 相关阅读:
    H110主板搭配魔改QNCW升级小记
    nRF52832 之ADC的使用
    Apache Flink ML 2.1.0 发布公告
    服务的动态配置-Config
    SpringMVC(五、AOP)
    【AGC】目前云调试对于RPK软件包是否支持、素材测试服务如何使用、崩溃服务启用接入异常
    悲观锁和乐观锁
    Python自动化之定时任务
    数据特征工程 | Python实现CatBoost特征处理及选择
    【数据结构】树和森林(树和森林的存储结构、树森林二叉树的转换、树和森林的遍历
  • 原文地址:https://blog.csdn.net/xubaoyong/article/details/132608899