ue4 classuobject没有成员beginplay_UE4_使用WebSocket和Json(上)

48f0009d9b0ce7686ce60a32ff7c344c.png

记录UE4中WebSocket和Json的使用方法(C++使用,蓝图没有的,想在蓝图用可以C++暴露方法给蓝图)。本文记录如何在UE4中使用WebSockets模块实现Client,在Python中实现Server,进行通信。

内容:

  1. UE4 WebSocket Client
  2. Python WebSocket Server
  3. UE4 Json

UE4.25.3。项目github:

https://github.com/tiax615/UE4_Network​github.com

1. UE4 WebSocket Client

UE4中使用WebSocket,感觉这方面资料很少。百度里只会搜到一些蓝图插件,我曾用过一个,不够稳定(不如自己写);谷哥也没有太多内容,讲的比较详细的是一个社区的wiki,但也有错误的地方。

但实际上在UE4中用起来很方便。

WebSocket这个协议,能轻松和Web进行通信,而且很多语言都有WS的库,应用广泛。

1.1. 增加模块

增加WebSockets模块。关闭IDE,右键uproject项目文件->Generate Visual Studio project files,重新生成项目文件(不重新生成也许也可以):

// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class UE4_Network : ModuleRules
{
    
	public UE4_Network(ReadOnlyTargetRules Target) : base(Target)
	{
    
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[]
		{
    
			"Core", "CoreUObject", "Engine", "InputCore",
			"WebSockets"
		}); // 增加WebSockets模块

		PrivateDependencyModuleNames.AddRange(new string[] {
      });

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

1.2. 加载模块

wiki说In order to use this module, we need to load it. To do so, we will load it on game instance initialization(为了使用此模块,我们需要加载它。为此,我们将在游戏实例初始化时加载它),给出的例子是在GameInstance中Init的适合加载了模块。我尝试过在GameInstanceSubsystem的子类中加载,也可以正常使用,这两个类的生命周期相似。这次尝试在Actor中加载。(仅用来测试,不用在实际项目中,实际用的时候WS可能会需要作为单例)

新建一个Actor类,叫做ActorWebSocket。为了使用WS,需要包含头文件:

#include "WebSocketsModule.h"
#include "IWebSocket.h"

在BeginPlay时,加载WebSockets模块:

// Called when the game starts or when spawned
void AActorWebSocket::BeginPlay()
{
    
	Super::BeginPlay();
	
	FModuleManager::Get().LoadModuleChecked("WebSockets");
}

注意wifi中这个写法是错误的,没有使用Get&#

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在UE4使用C++创建程序化网格的详细步骤: 1. 在头文件中包含ProceduralMeshComponent.h头文件。 ``` #include "ProceduralMeshComponent.h" ``` 2. 创建一个Procedural Mesh Component对象,并将其添加到场景中。在构造函数中创建和初始化对象。 ``` AMyActor::AMyActor() { Mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh")); RootComponent = Mesh; } ``` 3. 在Actor的BeginPlay函数中,创建顶点缓冲区,索引缓冲区和法线缓冲区,并将它们分配给Procedural Mesh Component对象。 ``` void AMyActor::BeginPlay() { Super::BeginPlay(); TArray<FVector> Vertices; TArray<int32> Triangles; TArray<FVector> Normals; // 添加顶点、三角形和法线数据到缓冲区中 Mesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, Normals, TArray<FVector2D>(), TArray<FLinearColor>(), TArray<FProcMeshTangent>(), true); } ``` 4. 在Actor的Tick函数中,更新程序化网格的变换和材质。 ``` void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // 更新网格的变换和材质 Mesh->SetWorldTransform(GetActorTransform()); Mesh->SetMaterial(0, Material); } ``` 5. 可选地,您可以添加更多的顶点、三角形和法线数据,以及纹理和其他属性。例如,您可以通过以下方法向程序化网格添加一个四边形: ``` TArray<FVector> Vertices; TArray<int32> Triangles; TArray<FVector> Normals; // 添加四个顶点 Vertices.Add(FVector(-100, -100, 0)); Vertices.Add(FVector(100, -100, 0)); Vertices.Add(FVector(100, 100, 0)); Vertices.Add(FVector(-100, 100, 0)); // 添加两个三角形 Triangles.Add(0); Triangles.Add(1); Triangles.Add(2); Triangles.Add(0); Triangles.Add(2); Triangles.Add(3); // 计算法线 FVector Normal = FVector::CrossProduct(Vertices[1] - Vertices[0], Vertices[2] - Vertices[0]).GetSafeNormal(); for (int32 i = 0; i < Vertices.Num(); i++) { Normals.Add(Normal); } // 添加材质坐标 TArray<FVector2D> UVs; UVs.Add(FVector2D(0, 0)); UVs.Add(FVector2D(1, 0)); UVs.Add(FVector2D(1, 1)); UVs.Add(FVector2D(0, 1)); // 添加网格数据到Procedural Mesh Component中 Mesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, Normals, UVs, TArray<FLinearColor>(), TArray<FProcMeshTangent>(), true); ``` 这些是使用C++创建程序化网格的详细步骤。您可以根据需要添加更多的顶点、三角形和法线数据,以及纹理和其他属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值