UE4 parse command line arguments

Transfer: https://blog.csdn.net/u012999985/article/details/53544389

I. Description of command line arguments
command line argument is a series of keyword strings, when you can run the executable file from the command line or shortcut to the executable file to be passed. Their purpose is to customize the way the engine running, in order to meet the needs of developers or users. As a general purpose, Note: the command line is not case-sensitive

First, it can make run the editor without running game becomes very simple (as long as you can add a few characters, see Figure 1-1). Second, it can more easily configure the client IP, servers, and other richer operational information. Third, it can also be more complex. Players can customize the command-line parameters, and analysis and use in your code, such as the use of a specific map to start the game at the specified resolution and frame rate. Figure 1-1 is a four bat batch file is used to open different game end (editor, local game, the game client and proprietary servers), followed by the project name of the game is the command-line parameters. We can also shortcut the program which added direct, shown in Figure 1-2.

  Figure 1-1 Open a command line parameter different end of the game by Bat files

  Figure 1-2 Incoming command-line parameters in the shortcut in

Overall, the command-line parameter is actually divided into two categories, one is UE4 engine parameters provided, such as configuration map name, IP address, such as whether to turn on the server. (Want to know more parameters can refer to the official documentation) and the other players to customize the parameter is used to provide another way to configure the game's developers. In fact, players can add parameters, but if not resolved in the code, these meaningless parameters will not have any impact.

1.1 understand the URL, URL parameters and command-line parameters difference
in UE4, we need to understand the relationship URL, URL parameters and command-line parameters. URL parameter belongs to a part of the command-line parameters, as well as command-line arguments are parsed multiple parts, that part of the URL parameter will be credited as part of the internal structure of the information in the URL. In other words, the internal URL information not only from the command line, the command-line parameters and URL intersection, the intersection is the URL parameter.

URL参数除了负责传入地图信息,IP地址外,还可以通过“?PlayerNum=6”这样的形式传入其他信息,这也就是我们通过命令行参数获取配置信息的重要途径。(图1-3画横线的字符就是URL参数)

        图 1-3  Url参数示意图

 

二. 参数的提取

 

在UE4的代码中,命令行参数对应的类是FCommandLine。URL对应的类是FURL。如下图2-1,图2-2,图2-3所示。

图 2-1 URL信息对应类

  图 2-2  引擎4.8前命令行信息对应类

图 2-3  引擎4.9以后命令行信息对应类

有了上面的类,我们知道了两个类的内部数据的结构布局。对于FCommandLine来说,信息的存储很简单,所有命令行信息直接放在CmdLine里面(受版本影响,见下文)。而FUrl除了存储地图,Options(自定义参数)外,还有端口号port,协议名称protocol等。这里给出一个例子,下面的字符串是一个通过命令行参数启动的专有服务器的bat文件内容。

Start Engine\Binaries\Win64\UE4Editor.exe"Game/Game.uproject"MapName?MaxSizeAllowed=1?StartTime=6300.0 -game -server -log

我们通过执行代码const TCHAR* commandTest = FCommandLine::Get();可以获取图2-4的字符串。发现结果是,"Game/Game.uproject"MapName? -NoSuperCrates  -game -server -log

注意:4.9版本的FCommandLine函数添加了OriginalCmdLine属性与对应的Get方法(图2-3)。也就是说在4.5版本的情况下的Get与4.9以后版本的GetOriginal得到的结果是相同的。4.9以后的版本的Get(),获取的是不带工程名称与路径的命令行参数。

可以看出,CommandTest获取的就是启动命令后所有的字符串信息。那么我们的URL信息怎么提取呢?我们可以在UGameInstance::StartGameInstance()函数里面通过上面的方法获取整个命令行参数。然后构造新的FURL对象,读取本地的配置信息完成初始化,像端口号,Protocol等信息我们可以在\Engine\Config 的BaseEngine.ini获取,如图2-4。

图 2-4  BaseEngine中URL相关配置

随后,通过LoadURLConfig(TEXT("DefaultPlayer"),GGameIni);可以读取其他相关的配置信息,通过FParse::Token等一系列步骤可以将命令行参数里面的URL参数部分取出来。这时候的URL如图2-5所示。

图2-5 URL内部信息

上面的Op数组,其实就是我们在参数里面传入的MaxSizeAllowed等3个参数。到这里,我们URL参数的信息就提取出来了,接下来我们要在游戏开始前合适的位置将这些参数应用到游戏中。

 

Guess you like

Origin www.cnblogs.com/sevenyuan/p/12028723.html