How to launching a PIE session from code

I am writing some automated tests and I need the game to be running to test what I want to test. I guess the best way to do this is to launch a PIE at the start of the test (and preferably kill it at the end). How is this to be done?

I am trying to use:

GEngine->CreatePIEWorldByDuplication(Context, GEngine->GetWorldContexts()[0].World(), MapName);

But can’t seem to get it to work (passing in an uninitialised context and either an empty string for MapName or a string with a “/Game/Maps/MassOcean” (MassOcean is a map). It fails with the following error:

Assertion failed: PIEInstanceID != -1

For the PIE session I believe this may work for you.

FLevelEditorModule& LevelEditorModule = FModuleManager::Get().GetModuleChecked<FLevelEditorModule>(TEXT("LevelEditor"));
TSharedPtr<class ILevelViewport> ActiveLevelViewport = LevelEditorModule.GetFirstActiveViewport();

GUnrealEd->RequestPlaySession(false, ActiveLevelViewport, bSimulateInEditor, NULL, NULL, -1, false);

I need the game to be running to test
what I want to test.

Are you looking to run game tests or editor tests?
If you are indeed wanting to run your tests in the game you can do so by launching the game with Win64\UE4Editor.exe YourGameName -game -messaging

The -messaging will allow the UFE and the game to talk to each other.
If you’d rather not use the frontend then you could add the -execcmds=“Automation Run FTestName” or -AutomationTests=“Pretty.Test Name”.
Take note the -automationtests argument will close the session once the tests have been completed.

Here’s some good documentation on our automation system: Automation System in Unreal Engine | Unreal Engine 5.3 Documentation

That lets me launch one session, but as an added complication I actually need to start up parallel clients (it is a multi-player game). That is, I need to simulate doing the PIE with clients set to, say, 6 with a dedicated server.

I tried running the above code n times, but it seems to replace each instance each time a new one starts. I suppose I need to use a different Viewport for each one but I can’t see a way to get hold of a different one.

I’ve found the automation documentation to not be complete, since it doesn’t cover non-trivial tests such as this, starting tests from command line and how to pass/fail tests for example.

I’m looking for someone to answer you question.

It would most likely be better to create a script that launches a server and a clients in game. Make sure to run with -game -messaging -sessionID=‘GUID Number’. Make sure to use the same GUID number for each session you are opening.

You can visit here to create a session id without any hyphens: http://www.guidgenerator.com/online-guid-generator.aspx

The SessionID allows the UFE to view all of the running games as one session. This allows you to parallel test across multiple machines. This even allows you to send console commands to all of the machines at once. For example you could have the clients join the server. You can tell the server to change maps. Or you can just tell the group to split up the available tests and run them.