Unreal Engine添加UGameInstanceSubsystem子类

  1. 点击C++类文件夹,在右边的区域点击鼠标右键,在弹出的菜单中选择"新建C++类"

  2. 在弹出的菜单中选中"显示所有类",选择GameInstanceSubsystem作为父类, 点击"下一步"按钮

  3. 输入子类名称"UVRVIUOnlineGameSubsystem",选择插件作为新类的目标模块,点击"公共"选择器

  4. 打开C++工程,找到".Build.cs"文件,在"PublicDependencyModuleNames"下,添加"MultiPlayerPlugin"

    |-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 4 5 6 7 8 9 10 11 | public class LandMaster : ModuleRules { ``public LandMaster(ReadOnlyTargetRules Target) : base(Target) ``{ ``PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; ``PublicDependencyModuleNames.AddRange(``new string[] { ``"Core"``, ``"CoreUObject"``, ``"Engine"``, ``"InputCore"``, ``"UMG"``, ``"Http"``, ``"Json"``, ``"JsonUtilities"``, ``"Sockets"``, ``"Networking"``, ``"OnlineSubsystem"``, ``"OnlineSubsystemUtils"``, ``"MultiPlayerPlugin" }); ``PrivateDependencyModuleNames.AddRange(``new string[] { ``"Slate"``, ``"SlateCore" }); ``} } |

  5. 设置"MultiPlayerPlugin.uplugin"文件

    |-------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | { ``"FileVersion"``: 3, ``"Version"``: 1, ``"VersionName"``: ``"1.0"``, ``"FriendlyName"``: ``"MultiPlayerPlugin"``, ``"Description"``: ``"plugin for multi player "``, ``"Category"``: ``"Other"``, ``"CreatedBy"``: ``"VRVIU_Jacky"``, ``"CreatedByURL"``: ``""``, ``"DocsURL"``: ``""``, ``"MarketplaceURL"``: ``""``, ``"SupportURL"``: ``""``, ``"EngineVersion"``: ``"4.26.0"``, ``"CanContainContent"``: ``true``, ``"Installed"``: ``true``, ``"Modules"``: [ ``{ ``"Name"``: ``"MultiPlayerPlugin"``, ``"Type"``: ``"Runtime"``, ``"LoadingPhase"``: ``"Default"``, ``"WhitelistPlatforms"``: [ ``"Win32"``, ``"Win64" ``] ``} ``] } |

  6. 设置插件".Build.cs"文件,添加需要引用的模块

    |----------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // Copyright Epic Games, Inc. All Rights Reserved. using UnrealBuildTool; public class MultiPlayerPlugin : ModuleRules { ``public MultiPlayerPlugin(ReadOnlyTargetRules Target) : base(Target) ``{ ``bEnableUndefinedIdentifierWarnings = ``false``; ``PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; ``//bUsePrecompiled = true; ``PublicIncludePaths.AddRange( ``new string[] { ``"MultiPlayerPlugin/Public" ``} ``); ``PrivateIncludePaths.AddRange( ``new string[] { ``} ``); ``PublicDependencyModuleNames.AddRange( ``new string[] ``{ ``"Core" ``// ... add other public dependencies that you statically link with here ... ``} ``); ``PrivateDependencyModuleNames.AddRange( ``new string[] ``{ ``"CoreUObject"``, ``"Engine"``, ``"Slate"``, ``"SlateCore"``, ``"UMG"``, ``"Http"``, ``"Json"``, ``"JsonUtilities"``, ``"Sockets"``, ``"Networking" ``// ... add private dependencies that you statically link with here ... ``} ``); ``DynamicallyLoadedModuleNames.AddRange( ``new string[] ``{ ``// ... add any modules that your module loads dynamically here ... ``} ``); ``} } |

  7. 获取本机IP地址

    |----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | FString UVRVIUOnlineGameSubsystem::GetIpAddress(``bool bHasPort ``/*= true*/``) { ``FString IpAddr(``"NONE"``); ``bool canBind = ``false``; ``TSharedRef<FInternetAddr>LocalIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind); ``if (LocalIp->IsValid()) ``{ ``IpAddr = LocalIp->ToString(bHasPort); ``} ``return IpAddr; } |

  8. 创建会话

    |-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 | FString cmd = ``"open " + Map + ``"?listen"``; UGameplayStatics::GetPlayerController(GetWorld(), 0)->ConsoleCommand(cmd); m_ServerAddress = ServerAddress.Len() == 0 ? GetIpAddress(``false``):ServerAddress; |

  9. 加入会话

    |-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    | 1 2 3 4 5 | UE_LOG(LogTemp, Warning, TEXT(``"UVRVIUOnlineGameSubsystem::ConnectServer %s"``), *IntranetIP); FString cmd = ``"open " + IntranetIP; UGameplayStatics::GetPlayerController(GetWorld(), 0)->ConsoleCommand(cmd); m_ServerAddress = IntranetIP; |

相关推荐
悄悄敲敲敲3 小时前
C++:dfs习题四则
c++·算法·深度优先
安於宿命4 小时前
【Linux】进程间通信——进程池
linux·c++
南郁9 小时前
001-监控你的文件-FSWatch-C++开源库108杰
c++·开源·文件系统·文件监控·fswatch·文件变动信息·libfswatch
linux开发之路10 小时前
C++Linux进阶项目分析-仿写Redis之Qedis
linux·c++·redis·多线程·后端开发
EPSDA10 小时前
Linux线程库与线程库封装
linux·运维·服务器·开发语言·c++
孤独得猿11 小时前
排序算法复习——包括插入排序、希尔排序、冒泡排序、快排(包括霍尔法、挖坑法、快慢指针法)、堆排、选择排序、归并排序等 (代码采用c/c++混编)
c语言·数据结构·c++·笔记·算法·排序算法
编程探索者小陈11 小时前
【C++】智能指针的使用及其原理
开发语言·c++
半桔13 小时前
贪吃蛇解析
c语言·开发语言·数据结构·c++·算法
Pan_peter13 小时前
零基础学QT、C++(一)安装QT
c++·qt
万能的小裴同学13 小时前
MFC 自定义十六进制显示控件
开发语言·c++·mfc