9 Dec 2020 16:12
Accessing command line arguments at runtime
Simple task: change the colour of two lights
This post covers some discoveries from earlier this year in May. A question came up during some initial discussion of how we can use Unreal Engine to develop new pipelines, specifically whether it would be possible to include arguments in a launch script to modify something in the scene. Initially we wondered if we could specify an FBX model file to import and display, without having to open and re-package an entire project. This turned out to be borderline impossible with the architecture of Unreal Engine projects, but we did manage to get a small demo working with some coloured lights, just to get off the ground.
The launch command
./TheProjectBinary --args SmokySpace -windowed -ResX=1000 -ResY=600 LightOneHue=20 LightTwoHue=330
TheProjectBinary refers to the executable binary that Unreal Engine produced when packaging for Linux. Truth be told, the screenshot above is running on a Mac using
open TheProjectBinary.app command instead. The
--args flag tells UE to expect further arguments. The next is
SmokySpace which is the name of the level you're looking at (we could avoid this if it was the default level). The next few arguments request to run the game in a window rather than fullscreen, at a specific size.
The interesting part here are the custom arguments
LightTwoHue. These are totally custom terms and mean nothing to Unreal Engine, however with some handy nodes in the level blueprint, we are able to retrieve these and use them at runtime. Here's a few more executions with different hue values provided.
Here's our level blueprint. A custom function called Get Option Value has the task of taking in a specified argument name (e.g LightOneHue) and finding its value. The key node here is the first Get Command Line node which retrieves the entire command that was used to launch the project as a string. From there, after much splitting and searching, we return the value assigned to the argument.
From there, after the BeginPlay event, this is called and the integer is passed into a HSV to RGB node to generate a colour and applied to the geometry material and light colour of a point light actor. Currently this is repeated for each light but optimisation can come later!
From this little experiment we can see that it's absolutely possible to retrieve basic pieces of information at runtime (numbers and text). Next we'd like to explore the possibility of specifying a CSV file (for example), or a more complex dataset or 3D model at runtime.