Unreal C++ Client Reference¶
The GameFuse Unreal plugin (game-fuse-cpp) has a multiplayer client for C++ and Blueprints. It joins the same rooms, runs the same room code and has the same shape as the browser client, so Unreal, Unity and browser players can be in one room together.
It uses the engine's WebSockets and HTTP modules. All callbacks and events fire on the game thread.
Setup¶
- Turn on Multiplayer for your game on its Multiplayer tab in the GameFuse dashboard, and upload room code (see the quick start).
- Enable the GameFuse plugin. Its
Build.csalready depends onWebSocketsandHTTP. - For C++, add
"GameFuse"and"Json"to your module'sBuild.csdependencies.
Everything is on the UGameFuseMultiplayer game instance subsystem. It uses the game ID and token from UGameFuseManager::SetUpGame, or call SetGame(Id, Token). Rooms are UGameFuseRoom objects.
A whole session in C++¶
#include "Subsystems/GameFuseMultiplayer.h"
#include "Objects/GameFuseRoom.h"
UGameFuseMultiplayer* Multiplayer = GetGameInstance()->GetSubsystem<UGameFuseMultiplayer>();
Multiplayer->SignInAsGuest(FGFMultiplayerCallback::CreateLambda([this, Multiplayer](bool bSuccess, const FGFMultiplayerError& Error) {
if (!bSuccess) { UE_LOG(LogTemp, Error, TEXT("%s"), *Error.Message); return; }
TSharedPtr<FJsonObject> Options = MakeShared<FJsonObject>();
Options->SetStringField(TEXT("clientVersion"), TEXT("1"));
Multiplayer->JoinQueue(TEXT("1v1"), Options, FGFMultiplayerJoinCallback::CreateLambda(
[this](bool bJoined, UGameFuseRoom* Room, const FGFMultiplayerError& JoinError) {
if (!bJoined) { UE_LOG(LogTemp, Error, TEXT("%s (%s)"), *JoinError.Message, *JoinError.Code); return; }
UE_LOG(LogTemp, Log, TEXT("In room %s as seat %d"), *Room->GetCode(), Room->GetMe().Seat);
Room->OnMessageNative.AddLambda([](const FString& Type, const TSharedPtr<FJsonValue>& Data) {
UE_LOG(LogTemp, Log, TEXT("message %s"), *Type);
});
Room->OnClosed.AddDynamic(this, &AMyActor::HandleRoomClosed); // UFUNCTION(const FString& Reason, const FString& Detail)
TSharedPtr<FJsonObject> Move = MakeShared<FJsonObject>();
Move->SetNumberField(TEXT("x"), 1);
Room->SendObject(TEXT("move"), Move);
}));
}));
Blueprints¶
Get the GameFuse Multiplayer subsystem from the game instance, then:
- Sign In As Guest, Use GameFuse User Token or Use Token.
- Join Queue (Queue, Options JSON), Create Room (Game Mode, Options JSON), Join Room (Code, Options JSON), Resume, Leave Queue. Options JSON is a JSON object as text, such as
{"clientVersion":"1"}, or empty. Each join node has a callback withbSuccess, theRoomand anError(Message, Code, Status). - On the room, bind On Message (Type, Data JSON), On Players, On Reconnecting, On Reconnected and On Closed (Reason, Detail). Call Send (JSON), Leave, Get Code, Get Me, Get Players, Is Reconnecting and Is Closed.
- Settings: Set Game, Set Auto Reconnect, Set Client Version, and Set Api Url (only for a GameFuse other than gamefuse.co).
Signing in¶
| Function | What it does |
|---|---|
SignInAsGuest(Callback) |
Plays as a guest. The guest is made once and saved, so it is the same player next time. |
UseGameFuseUserToken() |
Plays as the player signed in with UGameFuseUser. |
UseToken(Token) |
Plays as any GameFuse player, given their authentication token. |
IsSignedIn() |
True once one of the above has been done. |
Getting into a room¶
The options you pass reach your room code as player.options.
| Function | What it does |
|---|---|
JoinQueue(Queue, Options, Callback) |
Joins a queue. The queue name is the game mode. Put clientVersion in the options; only players with the same one are matched. |
LeaveQueue() |
Stops waiting. The waiting JoinQueue fails with Error.Code "cancelled". |
CreateRoom(GameMode, Options, Callback) |
Makes a private room. An empty game mode means default. Share Room->GetCode(). |
JoinRoom(Code, Options, Callback) |
Joins a private room by its 6-character code. |
Resume(Callback) |
Gets back into the room the game was in before it restarted. No room and an empty error means there was nothing to resume. |
A failed join has Error.Code "no_match", "rejected", "cancelled" or "busy", the same as the browser client.
The room¶
| Function | Meaning |
|---|---|
GetCode() |
The room's 6-character code. |
GetMe() |
You: Id, Name, Seat. |
GetPlayers() |
The latest player list. |
Send(Type, FJsonValue), SendObject(Type, FJsonObject), SendJson(Type, Text) |
Sends to your room code's message(room, player, type, data). Returns false for a message over 4 KB. At most 60 a second. |
Leave() |
Leaves on purpose. Your room code sees playerLeft with 'quit'. |
IsReconnecting(), IsClosed() |
Where the connection is. |
Events¶
| Event | Arguments | When |
|---|---|---|
OnMessage / OnMessageNative |
type, data (JSON text / TSharedPtr<FJsonValue>) |
Your room code sent you something. Messages that arrive before anything is bound are kept (up to 200) and delivered when you bind. |
OnPlayers |
TArray<FGFMultiplayerPlayer> (Id, Name, Seat, bConnected) |
The player list changed. |
OnReconnecting |
Your connection dropped and the plugin is getting back in. | |
OnReconnected |
You are back in the same seat. | |
OnClosed |
reason, detail | You are out of the room: left, ended, kicked, lost, error or no_match. See the browser client for what each means. |
Saved data and timeouts¶
- The guest token and the room to resume are saved in
GameUserSettings.iniunder[GameFuse.Multiplayer], soResumeworks after the game restarts. - Room code with custom matchmaking keeps the join request open while it looks for a match. If your project lowers the engine's HTTP timeouts (
[HTTP]inDefaultEngine.ini), keep them longer than yourmatchmaking.waitSeconds.
Next¶
- Quick start: write and upload the room code. Step 5 there is the browser side; this page is the Unreal side.
- Connection drops and rejoining
- Room code reference