Creating a Multiplayer Game with Blueprints in Unreal Engine
In this tutorial, we'll go through the steps of creating a basic multiplayer game using Unreal Engine's Blueprint visual scripting system. We'll create a lobby system where players can join in and package the game for distribution.
using BluePrint
Step 1: Create a New Project
First, we'll start by creating a new project. For this tutorial, we will use the "Third Person Project" preset to give us a head start with some basic mechanics.
Step 2: Create a Lobby Level
Next, we set up a Lobby level that listens for other players to join. This is crucial for multiplayer functionality as it allows players to connect to a common environment.
Step 3: Blueprint Commands
In the default map, we open the Blueprint editor and set up commands to handle the game's networking. Compile and save your Blueprint once you've finished setting up the commands.
Here's what we've set up:
- Press '1' to open the lobby level and listen for other players.
- Press '2' to link to the host for multiplayer sessions.
If you dont know the ip4 address, open the terminal and type ipconfig.
Step 4: Package the Game
With our Blueprints configured, it's time to package the game. This process compiles the game into an executable format that can be distributed and run on other machines.
Step 5: Testing the Game
Finally, we test the game:
- Open the local host by pressing '1' to enter the lobby and wait for other players.
- On a second machine, press '2' to connect to the host machine.
Using C++
Step 1: Create BluePrintCallable Functions
void ATestLan_cCharacter::openLobby()
called by the server host to open the local network listen server
void ATestLan_cCharacter::openLobby()
{
// 只适用于服务器
// 服务器会为所有已经连接的玩家调用clientTravel
auto world = this->GetWorld();
if (world)
{
world->ServerTravel("/Game/ThirdPerson/Maps/Lobby?listen");
// how to get the url
// copy the absolute url from game
// substitute the ...content as /Game
// remove '.umap'
}
}
pitfalls:
- For the path of umap, first you should replace the
*/content/*as/Game/*, which is a easy way to point to the root dir. - Remove the
.umapof the map name. - add
?listen, then current player as the listen server.
void ATestLan_cCharacter::callOpenLevel(const FString &address) AND void ATestLan_cCharacter::callClientLevel(const FString &address)
add const&, then it can be called in blueprint with address as parameter. called by the client to join the server.
- address: the ip address of server.
void ATestLan_cCharacter::callOpenLevel(const FString &address)
{
// address is an ip address of the server
// https://docs.unrealengine.com/5.3/en-US/API/Runtime/Engine/Kismet/UGameplayStatics/OpenLevel/
UGameplayStatics::OpenLevel(this, *address);
}
void ATestLan_cCharacter::callClientLevel(const FString &address)
{
auto controller = GetGameInstance()->GetFirstLocalPlayerController();
if (controller)
{
controller->ClientTravel(address, ETravelType::TRAVEL_Absolute);
}
}
Step 2: call the functions in the blueprint
replace the functions in BluePrint and use our own function.
End
And there you have it! A simple multiplayer setup in Unreal Engine using Blueprints. With this foundation, you can now expand your game with more complex features and gameplay mechanics.
Remember, testing is a crucial part of game development. Make sure to test your game on different machines and with various player counts to ensure everything runs smoothly.
Happy developing!