Can we make command line arguments?

Hello there, I would like to add a new command line argument to my game that enables a special mode, is there any way that I can create a new one without needing to modify the engine?

Greetings.

#Yes

try running this anywhere

if(FParse::Param(FCommandLine::Get(), TEXT("MyFlagPresent")))
{
  //UE_LOG or Printstirng "My flag was present in the commandline!" ;
}

and run the game with

-MyFlagPresent

#:heart:

Rama

3 Likes

Just what I needed, thanks!

But what about getting parameters after flags?

like -MyFlagPresent 1

Just what I needed, thanks!

But what about getting parameters after flags?

like -MyFlagPresent 1

I would love to know this answer as well.

Hi, you’ve probably already found the answer to this, but in case anyone else is looking, having a look at Parse.h gives the clue: you can use the “Value” method of FParse, eg:

 int32 val;
  if(FParse.Value(FCommandLine::Get(), TEXT("MyFlagPresent"),val)) {

      blah
  }

Also, you can manually parse the string returned by FCommandLine::Get().

2 Likes

Same here. Works for me when using standard exe, but not on my server.exe.

Since there was no answer chosen and a few comments, here is an example of what I used to do this. To do strings, I have to do -param=“STRING”, so I need the quotes and the equals sign. Here is a sample command and tthe code that pareses it.

EDIT: I noticed I forgot to strip the = sign.

mygame.exe -skinByClass -startPose 2 -scb="\pathtoafile"

FString fileName;
uint32 startPose;

//string
if (FParse::Value(FCommandLine::Get(), TEXT("scb"), fileName)) {
	fileName = fileName.Replace(TEXT("="), TEXT("")).Replace(TEXT("\""), TEXT("")); // replace quotes
	_SCBFile = fileName;
}

//integer
if (FParse::Value(FCommandLine::Get(), TEXT("startPose"), startPose)) {
	_startPose = startPose;
}

//boolean
if (FParse::Param(FCommandLine::Get(), TEXT("skinByClass"))) {
	_skinByClass = true;
}
2 Likes

My Solution

The .h file....

	UFUNCTION(BlueprintCallable, Category = TDLHelpers)
		static bool CommandLineArgPresent(FString key = "unknown");
	UFUNCTION(BlueprintCallable, Category = TDLHelpers)
		static FString CommandLineArgValue(FString key = "unknown", FString defaultValueIfAbsent = "empty");
	UFUNCTION(BlueprintCallable, Category = TDLHelpers)
		static int32 CommandLineArgIntValue(FString key = "unknown", int32 defaultValueIfAbsent = 0);


and in the .cpp file...

bool UtdlBlueprintHelpers::CommandLineArgPresent(FString key)
{
	return FParse::Param(FCommandLine::Get(), *key);
}

FString UtdlBlueprintHelpers::CommandLineArgValue(FString key, FString defaultValueIfAbsent)
{
	FString s;

	if (FParse::Value(FCommandLine::Get(), *key, s)) {
		return s;
	}

	return defaultValueIfAbsent;
}

int32 UtdlBlueprintHelpers::CommandLineArgIntValue(FString key, int32 defaultValueIfAbsent)
{
	int32 i;

	if (FParse::Value(FCommandLine::Get(), *key, i)) {
		return i;
	}

	return defaultValueIfAbsent;
}

hello,i am a new programer ,i don’t know how to use Command-Line Arguments,can you tell me? thank you very much!!

This is actually really strange. The way they present command lines but in game…Their should be a library

We have a small plugin to access command line parameters from Blueprint: GitHub - DescendentStudios/CommandLineParametersPlugin: Plugin for Unreal Engine 4 (UE4) to access command line from Blueprint

For future googlers like myself

This is the correct answer

Do you know how can I get string parameter including “,”? FParse::Value(FCommandLine::Get(), TEXT(“scb”), fileName) get just before “,”.

where shall i add this code? i need to add a boolean check for the user which will run a specific code block in the client_example.py

If using command line parameters like this:

-ParamBool -ParamInt 3 -ParamValue="MyTextValue"

then a quick removal of the = sign is necessary.

 FString UtdlBlueprintHelpers::CommandLineArgValue(FString key, FString defaultValueIfAbsent)
 {
     FString s;
 
     if (FParse::Value(FCommandLine::Get(), *key, s)) {
         return s.Replace(TEXT("="), TEXT(""));
     }
 
     return defaultValueIfAbsent;
 }

You can also just put the “=” into the parser function so you don’t need to modify the string later on.

FString GameModeArg;
if (FParse::Value(FCommandLine::Get(), TEXT("-gamemode="), GameModeArg))
{
GameMode = GameModeArg;
}