How to reset level in C++?

Hey there,

does anyone know how to easily reset the level in C++? I thought there would be a function that does that inside the UWorld class, but I couldn’t find any.

I also tried this “hack”:

GetWorld()->Exec(GetWorld(), TEXT(“RestartLevel”));

But it doesn’t seem to work either (any idea why?)…

// Edit: playerController->ConsoleCommand(TEXT(“RestartLevel”)); seems to do the trick, but whats the clean way of doing this, that also works in a multiplayer game?

If this is a standalone game and maybe multiplayer server.

You can do the following command with ?restart.

ClientTravel( TEXT("?restart"), TRAVEL_Relative );

/**
	 * Travel to a different map or IP address. Calls the PreClientTravel event before doing anything.
	 * NOTE: This is implemented as a locally executed wrapper for ClientTravelInternal, to avoid API compatability breakage
	 *
	 * @param URL				A string containing the mapname (or IP address) to travel to, along with option key/value pairs
	 * @param TravelType 		specifies whether the client should append URL options used in previous travels; if true is specified
	 *							for the bSeamlesss parameter, this value must be TRAVEL_Relative.
	 * @param bSeamless			Indicates whether to use seamless travel (requires TravelType of TRAVEL_Relative)
	 * @param MapPackageGuid	The GUID of the map package to travel to - this is used to find the file when it has been autodownloaded,
	 * 							so it is only needed for clients
	 */
	UFUNCTION()

void ClientTravel(const FString& URL, enum ETravelType TravelType, bool bSeamless = false, FGuid MapPackageGuid = FGuid());

This how the controller does it in the codebase.
Hope that helps :slight_smile:

Thanks, this works perfectly in singleplayer, but in a multiplayer match in the editor this results only in a respawn of all playercontrollers and in a standalone game its even worse, it results in a disconnect of all clients.
I am calling the function on the listen server as well as on all the clients.

Also, I just found a ServerTravel function in the UWorld class.
However, GetWorld()->ServerTravel(TEXT(“?restart”)); doesn’t seem to work (clients get disconnected)… :frowning:

Okay, so maybe this one will help you under your

AGameMode::ProcessServerTravel(const FString& URL, bool bAbsolute)

and make sure that you have bUseSeamlessTravel set true;.

     /**
 * perform map travels using SeamlessTravel() which loads in the background and doesn't disconnect clients
 * @see World::SeamlessTravel()
 */
UPROPERTY()
uint32 bUseSeamlessTravel : 1;

Actually the more I look, it seems that

void UWorld::SeamlessTravel(const FString& SeamlessTravelURL, bool bAbsolute, FGuid MapPackageGuid)

Is what you want. The information in World.cpp has some great info and says it doesnt’ disconnect the clients. :slight_smile:
Good Luck!