Introducing the Lobby System in Leadwerks 4.6
The new Lobby system in Leadwerks 4.6 allows you to create a public listing of your multiplayer game for others to join. This uses the Steamworks library. You can tap into the Steamworks lib by piggybacking on the default "SpaceWar" example game, which uses the Steam app ID 480. This is set up by default when you create a new Leadwerks project.
Now you might think of a lobby as a place where people hang out and chat before the game starts. You can treat it like this, but it's best to keep the lobby up as the game is running. This will allow other people to find the game to join, if it isn't full or if someone leaves the game. So a lobby is better described as a publicly advertised game others can join.
Creating a new lobby to advertise our game is easy. We just call a command to create it, and then we will set two string values. The game title is set so that we can later retrieve only lobbies that are running this particular game. (This should be done if you are using the SpaceWar app ID instead of your own Steam ID.) We also add a short description that can display information about the game.
Lobby* mylobby = Lobby::Create(); mylobby->SetKey("game", "MyGameTitle"); mylobby->SetKey("description", "Here is my lobby!");
It's also easy to retrieve a list of lobbies:
int count = Lobby::Count(); for (int i = 0; i < count; ++i) { Lobby* lobby = Lobby::Get(i); }
We can use GetKey() to look for lobbies that are running the same game we are:
if (lobby->GetKey("game") == "MyGameTitle")
We can retrieve the owner of the lobby with this command that will return a Steam ID:
uint64 steamid = lobby->GetOwner();
Once you have that Steam ID, that is all you need to start sending messages through the new P2P networking system. More on that later.
And we can also retrieve a list of all members in the lobby:
int count = lobby->CountMembers(); for (int k = 0; k < count; ++k) { uint64 steamid = lobby->GetMember(k); }
After a lobby is created, it will have one member, which will be the same Steam ID as the owner.
Joining a lobby is simple enough:
if (lobby->Join()) System::Print("Joined lobby!");
And leaving is just as easy:
lobby->Leave()
Now here's the magical part: When the owner of the lobby leaves, the lobby is not destroyed. Instead, the system will automatically choose another player to become the owner of that lobby, so the multiplayer game can keep going! The lobby will not be destroyed until everyone leaves the game.
- 2
- 1
5 Comments
Recommended Comments