ドキュメント

サポート

Cloud Code

Lobby の使用

Use Cloud Code and Lobby service together to trigger server-side actions when players join lobbies.
読み終わるまでの所要時間 2 分最終更新 23日前

Lobby と Cloud Code を一緒に使用して、ゲーム内に一般的なトリガーを定義できます。ゲームロビーですでにマッチを開始する準備ができているソリューションについて考えます。準備ができたゲームロビーに反応して、以下のスクリプトをトリガーできます。この例は、Remote Config とのインテグレーションも備えています。 この例に従う前に、Unity Cloud Dashboard を通じて Lobby と Cloud Code の両方を有効にし、必要な SDK をインストールするステップに従います。サービス認証を使用してプロジェクトに ロビーを作成 し、その ID をメモしておきます。これは、Cloud Code を通じて行うことができます (Unity Lobby の例を確認してください)。 以下の例は、スクリプトに 2 つのパラメーター (ロビーの作成に使用されるサービス ID と、ロビー ID) を付与します。ロビーを取得し、そのメタデータにいくつかの変更を加えて更新します。これらの変更は以下のとおりです。
  • ロビーをロックして、それ以上新規のプレイヤーが参加できないようにします。
  • ロビーのプレイヤー ID を使用して、ゲームのランダムな "プレイヤーの順序" を構成します。
  • Remote Config で定義された
    LOBBY_CURRENT_MAPS
    設定を使用して、ゲームのマップをランダムに選択します。スクリプトは、成功または失敗を示す基本的なブーリアンを返します。
JavaScript
const _ = require("lodash-4.17");const { LobbyApi, DataObjectVisibilityEnum } = require("@unity-services/lobby-1.2");const { SettingsApi } = require("@unity-services/remote-config-1.1");module.exports = async ({params,context,logger}) => { const { serviceId, lobbyId } = params; const { projectId, environmentId, playerId } = context; const lobbyApi = new LobbyApi(context); const remoteConfig = new SettingsApi(context); try { const {data: lobby} = await lobbyApi.getLobby(lobbyId, serviceId); return setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby); } catch (err) { logger.error(`Failed to set up lobby: ${err.response.data.detail}`); return false; }};// Updates a lobby to set up for the start of a game, including reading a value from Remote Config to randomly assign to the lobby.async function setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby) { let playerIds = []; for (let i = 0; i < lobby.players.length; i++) { playerIds.push(lobby.players[i].id); } // Generate a turn order for the game by shuffling the player IDs. _.shuffle(playerIds); // Load all of the maps that are currently configured as available in Remote Config and pick a random one for this game. let maps = getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) if (maps == null) { throw "Maps loaded from Remote Config are null."; } let gameMap = _.sample(maps); try { const updateResponse = await lobbyApi.updateLobby( lobby.id, serviceId, null, { isLocked: true, // Lock the lobby so that new players cannot join. hostId: playerIds[0], // Transfer host ownership from the service to one of the players. data: { "playerOrder": { value: playerIds.toString(), visibility: DataObjectVisibilityEnum.Member // Set the visibility of the player order so that only lobby members can access it. }, "map": { value: gameMap, visibility: DataObjectVisibilityEnum.Public // Set the visibility of the game map so that anyone who views the lobby can access it. } } } ); return true; } catch (err) { throw err; } return lobbyUpdateSuccess;}// Loads the Remote Config JSON value for LOBBY_CURRENT_MAPS and returns the array of maps inside of it.// If the JSON is invalid, the return value is null.async function getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) { const currentMapsId = 'LOBBY_CURRENT_MAPS'; const remoteConfigResponse = await remoteConfig.assignSettingsGet( projectId, environmentId, 'settings', [currentMapsId] ); let mapsJSON = remoteConfigResponse?.data?.configs?.settings?.[currentMapsId]; if (!mapsJSON || !mapsJSON.maps || mapsJSON.maps.length == 0) { return null; } return mapsJSON.maps;}// Uncomment the code below to enable the inline parameter definition// - Requires Cloud Code JS dev environment setup with NodeJS (https://docs.unity3d.com/Packages/com.unity.services.cloudcode@latest/index.html?subfolder=/manual/Authoring/javascript_project.html)//// module.exports.params = {// serviceId: { type: "String", required: true },// lobbyId: { type: "String", required: true },// };
ここでの Remote Config JSON は単純な構造を持ちますが、この設定例を拡張して、よりきめ細かくゲームをコントロールできます。同様に、例えば以下のように、ゲームの設定に加えて検討できる、Cloud Code のさまざまなトリガーがあります。
  • メタデータ値を調整し、文字列をサニタイズするためのロビー作成のトリガー。
  • Cloud Save プレイヤーデータを使用した、プレイヤーのクエリ結果のフィルター処理、参加の承認/拒否、およびメタデータの設定。
  • Economy データを使用したプレイヤーまたはロビーのメタデータの設定。