Starting PIE programmatically

I’m trying to automate a workflow that involves entering and exiting PIE a few times. Unfortunately, I can’t seem to find a way to cause the equivalent of pressing the “Play” button from code. The closest I’ve found is an EditorPlaySimulate function that is exposed via UEditorLevelLibrary, but as the name implies that simulates instead of playing.

Is there any way to enter PIE from code? Ideally I’d like to use Python or C++ and avoid Blueprint for this.

I got this working in 5.4 by using the following:

FPlayWorldCommands::GlobalPlayWorldActions->ExecuteAction(FPlayWorldCommands::Get().PlayInViewport.ToSharedRef());

It requires the following include and a module dependency on ‘UnrealEd’.

include “Kismet2/DebuggerCommands.h”