Ue5 websocket控制Character前后左右动作

# myue521Character.h

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once


#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "myue521Character.generated.h"

UCLASS(config=Game)
class Amyue521Character : public ACharacter
{
	 
public:

	void NotifyServer();
	virtual void myJump();

public: 
	FString Str = TEXT("walawala");
	FString topicLogKey = TEXT("log");
	FString walkLogKey = TEXT("walk");
	FString Forward = TEXT("Forward");
	FString Right = TEXT("Right");
	void TriggeredBySocketMsg(FString jsonString);

	void doMyForward(int f);// 1或者-1

	void doMyRight(int r);// 1或者-1
};

# myue521Character.cpp

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#include "myue521Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "UObject/ConstructorHelpers.h"  
#include "UWebSocketGameInstance.h"
//
// Amyue521Character


void Amyue521Character::myJump()
{
	bPressedJump = true;
	JumpKeyHoldTime = 0.0f;

	this->NotifyServer();


} 

void Amyue521Character::Look(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D LookAxisVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// add yaw and pitch input to controller
		AddControllerYawInput(LookAxisVector.X);
		AddControllerPitchInput(LookAxisVector.Y);
	}
}

void Amyue521Character::NotifyServer() {

	//从Character里获取UGameInstance的例子
	UUWebSocketGameInstance* GameInstance = Cast<UUWebSocketGameInstance>(GetGameInstance());
	if (GameInstance) {
		if (GameInstance->WebSocket->IsConnected()) {
			GameInstance->WebSocket->Send("FROM UnrealEngine 5");
		}
	}
} 

void Amyue521Character::TriggeredBySocketMsg(FString jsonString)
{
	
	//UE_LOG(LogTemp, Warning, TEXT("%s 事件触发调用了我的函数CallBackFunMul。%s-%s"), *FString(__FUNCTION__), *(this->Str), *jsonString);
	//GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, FString::Printf(TEXT("事件触发调用了我的函数CallBackFunMul,%s-%s"), *(this->Str), *jsonString));

	TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(jsonString);
	TSharedPtr<FJsonObject> Root;

	if (FJsonSerializer::Deserialize(Reader, Root))
	{
		if (Root->HasField(TEXT("Topic"))) {
			FString Topic = Root->GetStringField(TEXT("Topic"));
			TSharedPtr<FJsonObject, ESPMode::ThreadSafe> Data = Root->GetObjectField(TEXT("Data"));

			// TSharedPtr<FJsonObject, ESPMode::ThreadSafe> ObjectField = Object->GetObjectField(TEXT("realtime"));
			FString KeyField = Data->GetStringField("Key");
			FString ValueField = Data->GetStringField("Value");

			// log类型打印log
			if (this->topicLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
				GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, ValueField);
			}else if (this->walkLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
				FString DirectionField = Data->GetStringField("Direction");
				if (this->Forward.Equals(DirectionField, ESearchCase::IgnoreCase)) {
					this->doMyForward(FCString::Atoi(*ValueField));

				}else if (this->Right.Equals(Right, ESearchCase::IgnoreCase)) {
					this->doMyRight(FCString::Atoi(*ValueField));
				}

			}
			else {
				GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "1-Key: " + KeyField + ",Value:" + ValueField);
			}

		}
		else {
			GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "解析json异常");
		}
	}
}

void Amyue521Character::doMyForward(int f) // 1或者-1
{
	FVector2D MovementVector = FInputActionValue::Axis2D(0, f);

	 
	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}

void Amyue521Character::doMyRight(int r)// 1或者-1
{
	FVector2D MovementVector = FInputActionValue::Axis2D(r, 0);


	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}
相关推荐
好奇龙猫4 分钟前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
sp_fyf_202440 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸1 小时前
链表的归并排序
数据结构·算法·链表
jrrz08281 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time1 小时前
golang学习2
算法
南宫生2 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步3 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
Ni-Guvara3 小时前
函数对象笔记
c++·算法
泉崎3 小时前
11.7比赛总结
数据结构·算法
你好helloworld3 小时前
滑动窗口最大值
数据结构·算法·leetcode