Launching Map with command line ?!

Hello,

I want to launch a Map of my shipped Game from command line.
I’m using this pattern for the command line : MyGame.exe /Game/Maps/MyMap (Command-Line Arguments | Unreal Engine Documentation)

But everytime the game launches to the default startup map instead of the new given map. The map name is right and the mission exists (can be loaded ingame) but with the parameter it won’t work.

I’m using *.pak files if this matters to this topic.

Thanks in advance!

Try not using the full path, just give the name of the map. And always make sure the map is being cooked into the package.

Also, before this, go to File>Packaging Project>Packaging Setting, then click on Packaging on left, then,set Built Configuration to “Development” and then save and built your project.

This way you can call maps from command line as Jin_VE pointed out in the comment … : ) ,

so you can’t do the trick in shipping builds at all?

Doesn’t appear to be possible but there may be a way if you really need it. What we usually do is change the default loaded map so it always loads the one we want to start the game with. It’s in Project Settings->Maps&Modes.

Yeah, I know that, but I need to create two links to use for starting the game with two different game modes.
Thanks!

Please respond to comments with comments. Answers are for answers. I will respond above.

You can use your own command-line parameters. Here’s my GameMode BeginPlay().

void ADTMainGameMode::BeginPlay()
{
	const TCHAR* cmdLineStr = FCommandLine::Get();
	if(cmdLineStr != NULL)
	{
		int32 inInt;

		if(FParse::Value(cmdLineStr, TEXT("RedTeam"), inInt))
			RedTeamInitialCount = inInt;

		if(FParse::Value(cmdLineStr, TEXT("BlueTeam"), inInt))
			BlueTeamInitialCount = inInt;
	}

	Super::BeginPlay();
}

It looks like there are blueprint nodes that do this as well, if you need to do it there. Check the last post (currently) here.

Sorry, answering from smartphone xD

Ah. It happens. No worries. Just making sure.

You will probably want to get string input so my code above is just an example. If you need help with the string parsing, let me know. But that’s how I get command line parameters into my game logic.

You could also add a start-up level that lets the user choose the option(s) and then loads the main level as needed.

Oh, last thing. The command line parsing is inconsistent. For UE4 command-line parameters (like -PORT or -ResX), you want to use -Option=Value but for custom non-string options, you want to use -Option Value. With strings I had to do this:

FString inStr;
	if(FParse::Value(cmdLineStr, TEXT("IncomingIP"), inStr))
	{
		bOverrideIncomingIP = true;
		IncomingIP = inStr.Right(inStr.Len() - 1).TrimQuotes();
	}

And for that option I had to use the format: -IncomingIP="127.0.0.1". It’s funky but at least it works.

Sorry, last one for real. Just for the sake of completion. There are two other options.

  1. You can create a C++ class derived from UGameInstance. You want to override the StartGameInstance function, copy the code from the base class implementation, and remove the bit that gets included in shipping builds (that’s what stops your command-line options). Then you need to use the custom game instance in your level.
  2. There seems to be a compile-time definition (UE_ALLOW_MAP_OVERRIDE_IN_SHIPPING). The comment reads: This can be defined in the target.cs file to allow map overrides in shipping builds.

Both options will do the same thing. Each method has its own pros and cons.

Very useful, I’ll try them all to find the best one for my situation!

Thank you!

File->Package Project-> Packaging Settings and you need to find “List of maps to include in a packaged build”, then add those levels that you want to run from exe. After this, just run exe with dir from this list. For e.g. “game.exe /Game/NewWorld1”.

I was trying to allow user-defined command-line arguments to be parsed by my game in shipping builds, that runs much faster, and didn’t know how to use the UE_ALLOW_MAP_OVERRIDE_IN_SHIPPING compile-time definition in .Target.cs file to allow it, as @anonymous_user_49a519741 suggested.

It seems to work on my side after adding it like this to .Target.cs file:

using UnrealBuildTool;
using System.Collections.Generic;

public class TransportGridTarget : TargetRules
{
	public TransportGridTarget(TargetInfo Target) : base(Target)
	{
		Type = TargetType.Game;

		ExtraModuleNames.AddRange( new string[] { "TransportGrid" } );
		ProjectDefinitions.Add( "UE_ALLOW_MAP_OVERRIDE_IN_SHIPPING=1" );
	}
}
1 Like