1. ヘルプセンター
  2. Diarkis の始め方
  3. オンライン・マルチプレイ機能

個別ロビーの作り方

個別ロビーでは限られたプレーヤーが参加することでお互いに同期をとり、ゲーム・セッションを開始したりすることができます。

必要な Diarkis モジュール

個別ロビーを作るには Diarkis Room と Diarkis P2P を使います。

個別ロビーを使ってできること

個別ロビーは、Diarkis Room を使います。このため簡単にロビー内でプレーヤー同士の同期を取ることが可能です。

個別ロビーを使って実現できる主な機能を紹介します:

  • リモート・プレーヤーの同期(メッセージの送受信)
  • ロビーのプロパティの更新と同期
    • Diarkis Room のプロパティ機能を使います
  • 個別ロビー内での通知の送受信
    • Diarkis Room を使うことで通知の送受信をします
  • ゲーム・セッションを作る
    • 別途ルームを作る
    • Diarkis Room の MessageTo を使う
    • Diarkis P2P を使う

個別ロビーの作り方

個別ロビーの作成は Diarkis Room のルーム作成とほぼ同じです。

ルームの作成についてはこちらを参照ください。

個別ロビーを他のプレーヤーに見つけてもらう

ルームではそれぞれにカテゴリをアサインすることが可能です。この機能を使うことで、カテゴリごとに他のプレーヤーから検索できるようになります。

個別ロビー(ルーム)にカテゴリをアサインする

NOTE: 以下のコード例では C# Client SDK を使っています。

// For example, category = 1 means difficulty HARD
uint category = 1;

// Room name that you may assign to the room
string roomName = "For hardcore players only";

// Any information that you may want to assign to the room
string roomMetadata = "No noob allowed lol";

// Capture the server response of room.Register
room.OnRegister += (bool success, byte[] message) =>
{
// This event is the response of room.Register
}

// This is how to assign the category to a room that the client has already joined
room.Register(category, roomName, roomMetadata)

個別ロビー(ルーム)をカテゴリごとに検索する

NOTE: 以下のコード例では C# Client SDK を使っています。

// For example, category = 1 means difficulty HARD
uint category = 1;

// This controls how many rooms to retrieve
uint howManyItemsToRetrieve = 10;

// This capture the server response to receive the list of rooms
room.OnFindRoomsByType += (bool success, List<RoomListItem> rooms) =>
{
// Loop the list to work your magic!
for (int i = 0; i < rooms.Cont; i++)
{
string roomID = rooms[i].GetID();
string roomName = rooms[i].GetName();
string roomMetadata = rooms[i].GetMetadata();
}
}

// This is how to request for the list of rooms
room.FindRoomsByType(category, howManyItemsToRetrieve);

ロビーのプロパティデータの更新と同期

ロビーは Diarkis Room を使っているため、ルームのプロパティ機能を使ってルームに紐づくプロパティのデータを自由に操作して同期することが可能です。

ルームのプロパティの操作についてはこちらを参照ください。

ルームのプロパティの同期(任意取得)についてはこちらを参照ください。

ルームのプロパティをロビー参加時に取得する方法についてはこちらを参照ください。

リモート・プレーヤーの同期

Diarkis Room の BroadcastTo と MessageTo を使うことでロビーに参加しているプレーヤー同士で簡単に同期を取ることが可能です。

ロビーの参加メンバー全員に対してメッセージを送受信する

NOTE: 以下のコード例では C# Client SDK を使っています。

// This is the listener callback to capture and handle messages sent by room.BroadcastTo from remote players
room.OnMemberBroadcast += (byte[] message) =>
{
// Deserialize the message using Diarkis.Lib.SyncData
Diarkis.Lib.SyncData syncdata = new Diarkis.Lib.SyncData();
syncData.Deserialize(message);

// These are the properties from the deserialized message
Dictionary<string, object> properties = syncData.Properties;
long remotePlayerPositionX = properties.Get("x");
long remotePlayerPositionY = properties.Get("y");
long remotePlayerVelocity = properties.Get("velocity");

// This is the player ID of the remote player
string remotePlayerUID = syncdata.UID;

}

// Properties to be serialized and sent
Dictionary<string, object> properties = new Dictionary<string, object>();
properties.Add("x", playerPositionX);
properties.Add("y", playerPositionY);
properties.Add("velocity", playerVelocity);

// Serialize the property data to be sent as a message
Diarkis.Lib.SyncData syncdata = new Diarkis.Lib.SyncData();
syncdata.UID = playerUID; // Player ID
syncdata.Properties = properties;
syncdata.Serialize();
byte[] message = syncdata.Serialized;

// If UDP client is used setting this flag to true will make the message delivered as an RUDP message
bool reliable = false;

// This is how to send a message to all members in the lobby
room.BroadcastTo(room.GetRoomID(), message, reliable);

ロビー参加メンバーの一部(任意に選定した)に対してメッセージを送受信する

NOTE: 以下のコード例では C# Client SDK を使っています。

// This is the listener callback to capture and handle messages sent by room.MessageTo from remote players
room.OnMemberMessage += (byte[] message) =>
{
// Deserialize the message using Diarkis.Lib.SyncData
Diarkis.Lib.SyncData syncdata = new Diarkis.Lib.SyncData();
syncData.Deserialize(message);

// These are the properties from the deserialized message
Dictionary<string, object> properties = syncData.Properties;
long remotePlayerPositionX = properties.Get("x");
long remotePlayerPositionY = properties.Get("y");
long remotePlayerVelocity = properties.Get("velocity");

// This is the player ID of the remote player
string remotePlayerUID = syncdata.UID;

}

// Properties to be serialized and sent
Dictionary<string, object> properties = new Dictionary<string, object>();
properties.Add("x", playerPositionX);
properties.Add("y", playerPositionY);
properties.Add("velocity", playerVelocity);

// Serialize the property data to be sent as a message
Diarkis.Lib.SyncData syncdata = new Diarkis.Lib.SyncData();
syncdata.UID = playerUID; // Player ID
syncdata.Properties = properties;
syncdata.Serialize();
byte[] message = syncdata.Serialized;

// Player ID list to send the message to
List<string> playerIDs = new List<string>();
playerIDs.Add(playerAID);
playerIDs.Add(playerBID);
playerIDs.Add(playerCID);

// If UDP client is used setting this flag to true will make the message delivered as an RUDP message
bool reliable = false;

// This is how to send a message to all members in the lobby
room.MessageTo(playerIDs, room.GetRoomID(), message, reliable);

ゲーム・セッションを作る

ゲーム・セッションを作る方法にはいくつかあり、用途によって使い分けることが可能です。

別のルームを作ってゲーム・セッションにする

ロビーと別に新たにルームを作ってゲームセッションとして動かすことが可能です。

Diarkis Room では、同時に複数のルームに参加することができないため、ゲーム・セッションルームは空のルームとして作成する必要があります。

NOTE:ルームを使ったゲーム・セッションに移動する際には一度ロビーから退出する必要があります。

// Maximum number of members for the game session
int maxMembers = 10;

// If true, the room will not be discard when it is empty
bool allowEmpty = false;

// By setting this flag to false, the room will be empty
bool join = false;

// This means that the empty room will be available for 5 minutes
int ttl = 300;

// Broadcast message buffering interval in milliseconds to lessen server stress
int interval = 200;

room.Create(maxMembers, allowEmpty, join, ttl, interval);

Diarkis Room の MessageTo を使ってゲーム・セッションを作る

別途ルームを作ってゲーム・セッションを始めることをせずに、ロビーの中で、Diarkis Room の MessageTo を使ってゲーム・セッションに参加するロビーのメンバー・プレーヤーだけと同期をすることでゲーム・セッションを作ることが可能です。

この手法の場合、ロビーから退出する必要が無いため、ゲーム・セッションに参加していてもロビーとの同期を取ることが可能です。

P2P を使ってゲームセッションを作る

ロビーでは Diarkis Room の BroadcastTo や MessageTo を使ってクライアントのアドレスを自由に交換し合うことが可能です。これを利用して Diarkis P2P を使うことでゲーム・セッションを作ることが可能です。

この手法の場合、ロビーから退出する必要が無いため、ゲーム・セッションに参加していてもロビーとの同期を取ることが可能です。