Cloud Code용 Cloud Save 튜토리얼
Use Cloud Code to access, modify, and manage Cloud Save data to achieve server-authoritative gameplay.
읽는 시간 1분최근 업데이트: 20일 전
Cloud Code용 Cloud Save SDK를 사용하면 플레이어에 대한 데이터를 읽고 쓸 수 있으며, 게임 데이터를 읽고 쓰는 것뿐 아니라 Cloud Code에서 데이터를 쿼리할 수 있습니다(데이터를 쿼리할 수 있도록 인덱스를 구성한 경우). 사용할 수 있는 모든 메서드 목록은 Cloud Code용 Cloud Save SDK 기술 자료에서 참고하십시오.
Cloud Code에서 Cloud Save에 액세스
다음은 Cloud Code 스크립트에서 플레이어 데이터를 작성하는 방법에 관한 예제입니다. JavaScriptconst _ = require("lodash-4.17");const { DataApi} = require("@unity-services/cloud-save-1.4");const NUMBER_OF_SIDES = 6;module.exports = async ({ params, context, logger }) => { const { projectId, playerId } = context; const api = new DataApi(context); const roll = rollDice(NUMBER_OF_SIDES); if (roll > NUMBER_OF_SIDES) { // Log an error message with information about the exception logger.error("The roll is greater than the number of sides: " + roll); // Return an error back to the client throw Error("Unable to roll dice!"); } // Return a JSON result to the client const result = { sides: NUMBER_OF_SIDES, roll: roll, }; const setResult = await api.setItem(projectId, playerId, { key: "DIE_ROLL", value: result }); logger.debug(JSON.stringify(setResult.data)); return result;};// Function outside of the script wrapperfunction rollDice(sides) { return _.random(1, sides);}
플레이어 간 데이터
Cloud Save SDK로 다른 플레이어의 ID를 전달하면 해당 플레이어의 Cloud Save 데이터를 읽고 쓸 수 있으므로 비동기식으로 멀티플레이어 상호 작용을 수행할 수 있습니다. 이는 클라이언트 내에서는 불가능하며, 여기서는 자신의 데이터만 사용자가 액세스할 수 있도록 허용되기 때문입니다. 다음 예제에는 동일한 Lobby 내에서 다른 플레이어에게 칭찬을 제공하는 방법이 나와 있습니다. 샘플 코드는 Cloud Save에 저장된 다른 플레이어 데이터에 액세스하고 수정하는 방법을 보여 줍니다.otherPlayerId플레이어로 인해 발생한 요청은 신뢰할 수 없는 입력으로 간주되어야 하므로 위의 예시와 같이 다른 플레이어 ID를 파라미터로 수락할 때 주의해야 합니다. 게임에서 플레이어가 서로 상호 작용할 수 있는 시점이나 방법이 제한되어 있으면(예: 플레이어가 멀티플레이어를 선택 해제할 수 있는 경우) 스크립트 내에서 관련 전제 조건을 확인할 필요가 있습니다.const { DataApi } = require('@unity-services/cloud-save-1.4');module.exports = async ({ params, context, logger }) => { const { projectId } = context; const { otherPlayerId } = params; // Initialize the cloud save API client const cloudSaveAPI = new DataApi(context); // Access the save data for the specified player const otherPlayerSave = await cloudSaveAPI.getItems( projectId, otherPlayerId, // or any other player ID "likes" // Cloud Code key ); // Assume that player's likes is 0 if the key doesn't exist const otherPlayerLikes = otherPlayerSave.data.results[0]?.value || 0; // Update the data for the specified player cloudSaveAPI.setItem( projectId, otherPlayerId, { key: "likes", value: otherPlayerLikes + 1 } )};
사용 사례 및 예제
Cloud Code 스크립트로 Cloud Save SDK를 사용하는 다른 사용 사례와 예제를 살펴봅니다. | 주제 | 설명 | |-|-| | 퀘스트 시스템 | 서버 권한/부정 행위 방지 퀘스트 시스템Cloud Code 모듈(C#) | | 교환 가능한 쿠폰 | 플레이어에게 게임 내 보상 제공
Cloud Code 스크립트(JavaScript) | | 서버 시간 부정 행위 방지 | 부정 행위 방지 시스템 구현
Cloud Code 스크립트(JavaScript)|