Today we’ll talk about command line arguments in an Unreal Engine application.
As you certainly know, you can use command line arguments to launch your compiled UE executable with specific launch options. For instance MyGame.exe -debug to enable some debugging messages, MyGame.exe -inputFile=C:\test to parse a specific file, etc.
If you have already dealt with command line arguments with UE, you have certainly read the documentation about this feature: https://docs.unrealengine.com/en-US/Programming/Basics/CommandLineArguments/index.html
In this tutorial, we’ll focus on our own command line arguments. Of course, we can use managed arguments such as MyGame.exe -windowed -ResX=1000 -ResY=600, etc… (which will start our game in windowed mode with a specific resolution). But that’s not the purpose of this post, as we want to be able to manage our custom arguments.
Here, we’ll have a look at the command line parsing with blueprints. The main advantage of using blueprints is we can get this command line arguments information from everywhere and at every time in our project.
There are several ways to perform the management of custom command line arguments, in this article we will see two of them: the GetCommandLine node, and the Game Options.
The GetCommandLine node
To get the command line used to launch the process, we will use the GetCommandLine node. We can find its documentation here: https://docs.unrealengine.com/en-US/BlueprintAPI/Utilities/GetCommandLine/index.html
This may be the simplest node ever created, it simply returns the command line as a string. We’ll now have to deal with the parsing and information extraction.
Let’s create two different blueprint functions: one to deal with “flag” arguments (such as -debug, -enableLogging, -disableGui, etc…), and another one to manage arguments with values (-inputFile=C:\test, -color=red, -lightIntensity=10, etc.).
Manage flags arguments
We will create a function that will simply split the command line using the space delimiter in order to retrieve every single option. Then, we will have to check if at least one array cell contains the option we are looking for.
We have two possible ways to “find” a string in another. The “contains” function or the “find substring” option.
The “contains” is more flexible, but can produce wrong results. For instance, if you look for “log” option and the “logging” option is present, the function will return true.
On the other hand, the find substring is less flexible and more strict: using it, we will have to strictly use the right syntax for the option.
It’s all yours to choose the way to perform this check 😉
Manage key=value arguments
We need another option to deal with key=value arguments. It’s very similar to the previous one, but we will have to return the value of the parameter instead of returning a single boolean.
As for flags, we still face the dilemma regarding the “contains” or “find substring” nodes.
The Game Options
The game options nodes (https://docs.unrealengine.com/en-US/BlueprintAPI/GameOptions/index.html) can be used to deal with our command line arguments too.
The main purpose of game option nodes was parsing the options coming from the GameMode and options given in the “Open Level” node. But we can easily use them to parse the command line.
For instance the HasOption node (https://docs.unrealengine.com/en-US/BlueprintAPI/GameOptions/HasOption/index.html) can be filled with the GetCommandLine node to do the same behavior as our ContainsCommandLineOption function. However, our own created function will be more flexible and versatile.
The ParseOption node (https://docs.unrealengine.com/en-US/BlueprintAPI/GameOptions/ParseOption/index.html) will also replace our GetCommandLineOption function. But as explained before, we may want to manage the whole process of command line parsing instead of using already created functions.
I strongly advise you to create your own blueprint function library with these two functions.
Here are paste bins of these functions:
This concludes this small tutorial on how to process the command line arguments in an Unreal Engine application. For this we have two possibilities, either the GetCommandLine function, or the Game Options. It’s up to you to use the one you prefer.
Have a nice day!