Servertravel (solved)

Hi everyone!

I am currently working on a local multiplayer game. So far I managed to make a player host, and other player to connect to local IP or Session. Btw, I am not using advanced session plugin.

When a player connects to a session they end up in a same level. I call this level a LobbyMap.

What i want to do next is I want them both (I only have 2 players) to load a new level. (Like start the actuall game).

I have a map MainMenu where the host and connect buttons are, then I have the LobbyMap where the players get to see each other for the first time, and I have the third map PlayMap where the actuall game should happen.

I also have one GameMode which decides what GameState currently is. And then i have 2 GameStates.
When I want to change a GameState I simply create actor from class (on GameMode) and class is newGameState class. And I delete previous GameState.

First: it send to all game controllers an interface event that tells them to create a LobbyWidget where the button Start is located, when someone presses it, everyone should be traveled to a new level.

Second: Gameplay things.

Now the problem is that I dont know where and how to make players and server change level.
I tried to put execute command “servertravel” in SecondGameState on BeginPlay - didnt work, I tried to put in game instance - it didnt work, I also tried to execute it onClick in LobbyWidget, tried to call it in GameMode when changing States - nothing works.

I watched several YT tuts on calling server travel, but none helped.
I need help. please

Execute command “ServerTravel Map_Name?Listen”

Must be called by the Server … Game Mode event works well.

1 Like

So I when my clients and host are connected together and are in the same level, everyone gets a widget with a button. When someone presses the button he sends a message to GameMode which executes command “ServerTravel Map_Name?Listen”. Nothing happens. When i click the button, it makes little stutter and thats it.

I dont understand

Remember if your on a listen server everyone is a client except the host, if it is dedicated everyones a client. Clients have to ask the server to do something

  • (clients can not directly tell a server to do what ever)

they can ask the server, then the server must do it if it is valid to do so.

Most of the basic code is in to server travel except how to get the next map and set it to that. You need to make a transition map(fake map in between the map your leaving to the map your going to), then have it spawn you right at the start in the next map.

Thatis in my GameMode:

Client can call this interface event from a widget on event onClickButton. Is it asking?

Btw. I made a transition map and do I have to open it before the execute command or it does it automatically? And I am using listener server

Sorry, i have no clue how to do it in blueprints. I use c++ to do everything.

I’m not that good at blueprints < I will not be of any help to you. Maybe one of the blueprint gurus can and will help you out.

Okok, np, hope blueprint pro will come and help me out)

the transition map must be added to your config file like so. DefaultEngine.ini file under

[/Script/EngineSettings.GameMapsSettings]
EditorStartupMap=/Game/VehicleAdvCPP/Maps/VehicleAdvExampleMap
LocalMapOptions=
TransitionMap=/Game/VehicleAdvCPP/Maps/TransMap   ; < ADD THIS LINE TO CONFIG USE YOUR PATH TO MAP
bUseSplitscreen=False

That is all you have to do for transition map. add that 1 line, rest will work fine. Just have to make the transition(fake) map and add that line.

1 Like

I did that. Still the servertravel doesnt work. Thx for the help anyway

If you want to try C++
you will need to add in a new function to your game type. Like so

FString AYourGameMode::GetNextMap()
{
      bAlreadyChanged = true;
  FString theCurrentMap = DedicatedMapRotation();

   UE_LOG(LogGameType,Log,TEXT("AYourGameMode::GetNextMap() theCurrentMap is %s"), *theCurrentMap);
   return theCurrentMap;
}

Then in the function RestartGame() add in the function

 if (GameSession->CanRestartGame())
 {
	if (GetMatchState() == MatchState::QuittingMap)
	{
		return;
	}

// these server travels should all be relative to the current URL  add this iff section
if ((GetMatchState() == MatchState::MatchOver) && (!bAlreadyChanged))
{
  FString NextMap = GetNextMap();
UE_LOG(LogGameType, Log, TEXT("AFastLineGameMode::RestartGame() NextMap is %s"), *NextMap);
  if (NextMap != "")
  {
     GetWorld()->ServerTravel(NextMap, GetTravelType());
     return;
  }
 }
	UE_LOG(LogGameType, Log, TEXT("AFastLineGameMode::RestartGame() NextMap is Restart map"));
	GetWorld()->ServerTravel("?Restart", GetTravelType());
}

then in ProcessServerTravel() function. All you need to do is get your new maP name to send on the url. AFTER THIS LINE IN THAT FUNCTION.

FString URLMod = NextURL.ToString();

Add in

FString StartOfLoadMap = TEXT(".");
UE_LOG(LogGameType, Log, TEXT("AFastLineGameMode::ProcessServerTravel() StartOfLoadMap is %s"), *StartOfLoadMap);
LoadMap.InsertAt(0, StartOfLoadMap);
UE_LOG(LogGameType, Log, TEXT(AFastLineGameMode::ProcessServerTravel() Final LoadMap is %s), *LoadMap);|
URLMod.Append(*LoadMap, URLMod.Len()); 

that should get it switching to next map once you get your new map name. You will have to fix it so it spawn at the right location you want in the new map.

2 Likes

omg I did this, its a bug in Engine that doesnt travel clients. I had to launch the game in project folder in order for it to work. Thx for the help!

Good job, nice to see you got it.

Wait a second, is it case sensitive? I’m trying to figure out why servertravel doesn’t work, is it because it needs capital letters?