สร้างโลกของคุณเอง
ผู้เล่นและตัวละคร: ขั้นสูง
ไกด์การสร้างผู้เล่นบอท
15นาที
ผู้เล่นบอทถูกใช้เพื่อเติมเต็มเมื่อไม่มีคนเพียงพอในการเริ่มโลกแบบผู้เล่นหลายคน หรือเมื่อผู้เล่นออกจากโลกในระหว่างนั้น พฤติกรรมของผู้เล่นบอทต้องถูกนำไปใช้สำหรับเนื้อหาทุกประเภท คู่มือนี้อธิบายวิธีการทั่วไปในการสร้างผู้เล่นบอท 📘 คู่มือการสร้างผู้เล่นบอทอิงจากคู่มือผู้เล่นหลายคน \[ บทเรียนการเล่นหลายคน docid\ bfzi crxt7x2ujbo1znog ] ขั้นตอนที่ 1 สร้างผู้เล่นบอท 1 1 เพิ่มค่าบูลีนที่เรียกว่า isbot ลงในสคีมาของผู้เล่นหลายคน 1 2 กำหนดฟังก์ชันด้านล่างเพื่อสร้างผู้เล่นบอทในสคริปต์เซิร์ฟเวอร์ index ts และเรียกใช้ที่จุดที่ต้องการ index ts // วิธีการ `createbot()` ใช้เพื่อสร้างผู้เล่นบอทด้วย `userid` ที่กำหนดไว้ createbot(userid string) { // สร้าง id เซสชันสำหรับผู้เล่นบอทโดยใช้ `userid` ที่ให้มา const sessionid = "bot " + userid; // ตรวจสอบว่ามีผู้เล่นบอทที่มี id เซสชันเดียวกันอยู่แล้วหรือไม่ ถ้ามี ให้กลับโดยไม่สร้างซ้ำ if (this state players has(sessionid)) { return; } // สร้างวัตถุ `player` ใหม่สำหรับผู้เล่นบอท const player player = new player(); player sessionid = sessionid; if (userid) { player zepetouserid = userid; } player isbot = true; // เพิ่มผู้เล่นบอทลงในแผนที่ผู้เล่นของสถานะโดยใช้ id เซสชันเป็นกุญแจ this state players set(player sessionid, player); this botmap set(sessionid, player); } 👍 เคล็ดลับ userid ของผู้ใช้เฉพาะจะถูกเก็บไว้ล่วงหน้าเพื่อสร้างตัวละครผู้เล่นบอท คุณสามารถตรวจสอบ userid ของผู้ใช้เฉพาะได้โดยการตรวจสอบ userid ของไคลเอนต์ที่เชื่อมต่อกับ onjoin บนเซิร์ฟเวอร์ หลังจากเขียนสคริปต์ด้านล่างในสคริปต์เซิร์ฟเวอร์ เชื่อมต่อจากโลกที่เกี่ยวข้อง index ts onjoin(client sandboxplayer) { 	console log(client userid); } ขั้นตอนที่ 2 สร้างผู้เล่น bot บนไคลเอนต์ 2 1 หากคุณมีเซิร์ฟเวอร์สร้างผู้เล่น bot ในบางจุด ไคลเอนต์จะรับรู้ว่าเป็นผู้เล่นใหม่ใน onjoinplayer() สร้างโปรเจกต์ > สร้าง > zepeto > typescript และเปลี่ยนชื่อเป็น botplayermanager เพิ่มตรรกะใน onaddedplayer() เพื่อสร้างผู้เล่นแต่ละคน และเพิ่มตรรกะเพื่อแยกผู้เล่น bot และสร้างตัวละคร zepeto ของพวกเขา botplayermanager ts import { zepetocharacter, zepetoplayers } from 'zepeto character controller'; import { room } from 'zepeto multiplay'; import { zepetoscriptbehaviour } from 'zepeto script' import { zepetoworldmultiplay } from 'zepeto world'; export default class botplayermanager extends zepetoscriptbehaviour { public zepetoworldmultiplay zepetoworldmultiplay; // ตัวแปรส่วนตัวเพื่อเก็บข้อมูลห้องปัจจุบันและข้อมูลผู้เล่น bot private room room; private botmapdata map\<string, zepetocharacter> = new map\<string, zepetocharacter>(); start() { 		 	 // ฟังเหตุการณ์ `roomjoined` จากคอมโพเนนต์ `zepetoworldmultiplay` this zepetoworldmultiplay roomjoined += (room room) => { this room = room; } 	 // ฟังเหตุการณ์ `onaddedplayer` จาก `zepetoplayers instance` เพื่อจัดการผู้เล่นที่เพิ่มเข้ามาใหม่ zepetoplayers instance onaddedplayer addlistener((userid string) => { 	 // รับข้อมูลผู้เล่นปัจจุบันจากสถานะห้องโดยใช้ `userid` const currentplayer = this room state players get item(userid); 	 // ตรวจสอบว่าผู้เล่นเป็น bot หรือไม่ และถ้าใช่ ให้ตั้งค่าพวกเขาเป็นผู้เล่น bot if (currentplayer isbot) { this setbotplayer(currentplayer sessionid); } }); } } 2 2 เขียนฟังก์ชัน setbotplayer เพื่อเพิ่มแท็กและส่วนประกอบการซิงโครไนซ์ให้กับผู้เล่นบอทและสร้างสคริปต์เพื่อควบคุมพวกเขา ตั้งค่า botmapdata เพื่อบันทึกข้อมูลผู้เล่นบอทในรูปแบบแผนที่เพื่อจัดการผู้เล่นบอท botplayermanager ts // วิธีการ `setbotplayer()` ใช้เพื่อกำหนดผู้เล่นเป็นบอท setbotplayer(userid string) { // รับตัวละคร zepeto ที่เกี่ยวข้องกับผู้เล่นบอทโดยใช้ `userid` ของพวกเขา const bot = zepetoplayers instance getplayer(userid) character; // ตั้งชื่อของตัวละครเป็น `userid` เพื่อการระบุ bot gameobject name = userid; // เก็บข้อมูลผู้เล่นบอทในแผนที่ ` botmapdata` โดยใช้ `userid` ของพวกเขาเป็นกุญแจ 	 this botmapdata set(userid, bot); } 👍 เคล็ดลับ คุณสามารถเพิ่มสคริปต์หรือการตั้งค่าเพิ่มเติมใน setbotplayer() เพื่อควบคุมพฤติกรรมของผู้เล่นบอท ขั้นตอนที่ 3 สร้างปุ่มผู้เล่นบอทบนไคลเอนต์ สำหรับโลกที่ต้องการจำนวนผู้เล่นที่แน่นอนในการเริ่มต้น บางครั้งอาจไม่มีผู้เล่นเพียงพอและคุณต้องรอนานเพื่อให้โลกเริ่มต้น ในกรณีนี้ คุณสามารถเริ่มโลกได้โดยการเพิ่มผู้เล่น bot 3 1 ลงทะเบียนฟังก์ชันเพื่อเรียกใช้ createbot() เมื่อเซิร์ฟเวอร์ได้รับข้อความจากไคลเอนต์ใน index ts async oncreate() { 	 // จัดการข้อความ "createbot" ซึ่งสร้างผู้เล่นบอทด้วย userid ที่กำหนด this onmessage("createbot", (client, message) => { this createbot(message); }); } 3 2 ในสคริปต์ไคลเอนต์ botplayermanager ts เขียนฟังก์ชันเพื่อส่งข้อความ "createbot" ไปยังเซิร์ฟเวอร์ วิธีการเรียกใช้ฟังก์ชันคือการส่งข้อความโดยการกดปุ่ม ส่ง user id ของผู้เล่น bot ที่จะสร้างเป็นสตริงผ่านข้อความ botplayermanager ts public buttoncreatebot button; public botplayerid string; start() {	 	 // เพิ่มผู้ฟังการคลิกไปยังปุ่ม "create bot" เพื่อส่งข้อความเพื่อสร้างผู้เล่นบอท 	 this buttoncreatebot onclick addlistener(() => { this room send("createbot", this botplayerid); }); } 3 3 ตอนนี้ เมื่อคุณรันเซิร์ฟเวอร์และรันไทม์ คุณจะเห็นว่าผู้เล่นบอทถูกสร้างขึ้นเมื่อคุณกดปุ่ม ขั้นตอนที่ 4 เริ่มต้นโลกโดยการเพิ่มผู้เล่นบอท เมื่อมีผู้เล่นไม่เพียงพอในการเริ่มต้นโลก คุณสามารถเพิ่มผู้เล่นบอทเพื่อเริ่มต้นโลกได้ 4 1 ในสคริปต์เซิร์ฟเวอร์ ให้เพิ่มโค้ดต่อไปนี้ในช่วง onjoin เพื่อตรวจสอบจำนวนผู้เล่นและเริ่มโลกเมื่อมีผู้เล่นอย่างน้อยสี่คน เพิ่มฟังก์ชันเพื่อตรวจสอบจำนวนผู้เล่นใน createbot() เพิ่มตัวนับสำหรับจำนวนการเล่นในฟังก์ชัน startworld() index ts async onjoin(client sandboxplayer) { // ตรวจสอบจำนวนผู้เล่นในห้องหลังจากผู้เล่นเข้าร่วม 	this checkplayernumber(); } // เมธอด `checkplayernumber()` จะตรวจสอบจำนวนผู้เล่นในห้องและเริ่มโลกหากมีผู้เล่นอย่างน้อยสี่คน checkplayernumber() { 	// พิมพ์จำนวนผู้เล่นในห้องปัจจุบันไปยังคอนโซล 	console log(`player number, ${this state players size}`); 	// หากมีผู้เล่นอย่างน้อยสี่คนในห้อง ให้เริ่มโลก 	if (this state players size >= 4) { this startworld(); 	} } // เมธอด `createbot()` ใช้เพื่อสร้างผู้เล่นบอทด้วย `userid` ที่กำหนด createbot(userid string) { 	// สร้าง id เซสชันสำหรับผู้เล่นบอทโดยใช้ `userid` ที่ให้มา 	const sessionid = "bot " + userid; 	// ตรวจสอบว่ามีผู้เล่นบอทที่มี id เซสชันเดียวกันอยู่แล้วหรือไม่ หากมี ให้กลับโดยไม่สร้างซ้ำ if (this state players has(sessionid)) { 	return; 	} 	// สร้างวัตถุ `player` ใหม่สำหรับผู้เล่นบอท 	const player player = new player(); player sessionid = sessionid; 	if (userid) { 	 player zepetouserid = userid; 	} 	player isbot = true; 	// เพิ่มผู้เล่นบอทลงในแผนที่ผู้เล่นของสถานะโดยใช้ id เซสชันเป็นกุญแจ 	this state players set(player sessionid, player); 	this botmap set(sessionid, player); 	// ตรวจสอบจำนวนผู้เล่นในห้องหลังจากเพิ่มผู้เล่นบอท 	this checkplayernumber(); } private playtime number = 0; // เมธอด `startworld()` จะเพิ่มเวลาเล่นและส่งข้อความ "startworld" ไปยังลูกค้าทั้งหมด startworld() { 	 this playtime += 1; // พิมพ์ข้อความที่บ่งบอกถึงการเริ่มต้นของโลกและเวลาเล่นปัจจุบัน console log("start world!"); this broadcast("startworld", this playtime); } บนเซิร์ฟเวอร์ onjoin จะถูกเรียกใช้เมื่อผู้เล่นจริงเข้าร่วมห้อง ดังนั้นเมื่อผู้เล่น bot ถูกสร้างขึ้นผ่าน createbot และเมื่อผู้เล่นเข้ามาผ่าน onjoin, checkplayernumber() จะเพิ่มจำนวนคน 4 2 ในสคริปต์ของไคลเอนต์ botplayermanager ts ให้เขียน startworld() ซึ่งจะถูกเรียกใช้เมื่อได้รับข้อความ startworld จากเซิร์ฟเวอร์ botplayermanager ts start() { 	// ฟังเหตุการณ์ `roomjoined` จากคอมโพเนนต์ `zepetoworldmultiplay` 	this zepetoworldmultiplay roomjoined += (room room) => { // เพิ่มตัวจัดการข้อความสำหรับประเภทข้อความ "startworld" 	this room addmessagehandler("startworld", (playtime number) => { this startworld(playtime); }); } // เมธอด `startworld()` จะถูกเรียกใช้เมื่อโลกเริ่มต้นด้วย `playtime` ที่กำหนด startworld(playtime number) { 	// พิมพ์ข้อความเริ่มต้นของโลกพร้อมกับ `playtime` console log(`start world ${playtime}`); } 4 3 ในระหว่างการทำงาน เมื่อมีผู้เล่นมากกว่า 4 คนรวมถึงผู้เล่น bot คุณจะเห็นบันทึกที่เรียกว่า world start ปรากฏขึ้นในคอนโซลของเซิร์ฟเวอร์และในคอนโซลของไคลเอนต์ ขั้นตอนที่ 5 ซิงโครไนซ์ตำแหน่งของผู้เล่น bot ด้านล่างนี้คือตัวอย่างโค้ดที่ย้ายผู้เล่น bot ที่เพิ่มเข้ามาไปยังตำแหน่งของผู้เล่นท้องถิ่นและซิงโครไนซ์ตำแหน่งการเคลื่อนไหว 5 1 ก่อนอื่นให้เขียนโค้ดเพื่อย้ายผู้เล่น bot เมื่อข้อความ movebot ถูกส่งมาจากไคลเอนต์ใน index ts ของเซิร์ฟเวอร์ async oncreate() { // จัดการกับข้อความ "movebot" ซึ่งย้ายผู้เล่นบอทไปยังตำแหน่งที่กำหนด 	this onmessage("movebot", (client, message) => { this movebot(client, message); }); } // เมธอด `movebot()` ย้ายผู้เล่นบอทไปยังตำแหน่งที่กำหนดตามข้อความที่ได้รับ movebot(client sandboxplayer, message string) { // แยกข้อความ json ที่ได้รับจากไคลเอนต์เพื่อดึงข้อมูลตำแหน่ง const position = json parse(message); // สร้างวัตถุข้อความใหม่ด้วย id เซสชันของผู้ใช้และข้อมูลตำแหน่งที่แยกออก const newmessage = { user client sessionid, positionx position x, positiony position y, positionz position z } // กระจายข้อความ "movebottoposition" ไปยังไคลเอนต์ทั้งหมดด้วยข้อมูลข้อความใหม่ในรูปแบบ json this broadcast("movebottoposition", json stringify(newmessage)); } 5 2 ในสคริปต์ไคลเอนต์ botplayermanager ts ให้เขียน sendbotposition() ที่ส่งตำแหน่งของผู้เล่นท้องถิ่นไปยังเซิร์ฟเวอร์เมื่อปุ่มcallbot ถูกกด จากนั้นเขียนโค้ดเพื่อย้ายผู้เล่น bot ทั้งหมดไปยังข้อมูลตำแหน่งที่รวมอยู่ในข้อความเมื่อได้รับข้อความ movebottoposition จากเซิร์ฟเวอร์ botplayermanager ts public buttoncallbot button; start(){ 	// ฟังเหตุการณ์ `roomjoined` จากคอมโพเนนต์ `zepetoworldmultiplay` this zepetoworldmultiplay roomjoined += (room room) => { this room = room; // เพิ่มตัวจัดการข้อความสำหรับประเภทข้อความ "startworld" this room addmessagehandler("startworld", (playtime number) => { this startworld(playtime); }); 	// เพิ่มผู้ฟังการคลิกไปยังปุ่ม "call bot" เพื่อส่งข้อความเพื่อส่งตำแหน่งผู้เล่น bot 	this buttoncallbot onclick addlistener(() => { 	 this sendbotposition(); 	}); } // วิธีนี้ส่งตำแหน่งของตัวละครผู้เล่นท้องถิ่นไปยังเซิร์ฟเวอร์เพื่อการซิงโครไนซ์การเคลื่อนไหวของ bot sendbotposition() { // รับตำแหน่งของตัวละครผู้เล่นท้องถิ่น const localplayerposition = zepetoplayers instance localplayer zepetoplayer character transform position; // สร้างวัตถุตำแหน่งที่มีพิกัด x, y, และ z ของตัวละครผู้เล่นท้องถิ่น const position = { x localplayerposition x, y localplayerposition y, z localplayerposition z } // แปลงวัตถุตำแหน่งเป็นสตริง json และส่งไปยังเซิร์ฟเวอร์ด้วยประเภทข้อความ "movebot" this room send("movebot", json stringify(position)); } movebottoposition(message) { // แยกข้อความ json ที่ได้รับจากไคลเอนต์เพื่อดึงข้อมูลตำแหน่ง const jsonmessage = json parse(message); const position = new vector3(jsonmessage positionx, jsonmessage positiony, jsonmessage positionz); // ย้ายตัวละคร bot แต่ละตัวในแผนที่ ` botmapdata` ไปยังตำแหน่งที่กำหนดโดยมีการเลื่อนแบบสุ่มเล็กน้อยในแกน x และ z this botmapdata foreach((character zepetocharacter) => { // วิธี `movetoposition()` ใช้เพื่อย้ายตัวละครไปยังตำแหน่งที่กำหนด // ที่นี่จะมีการเพิ่มการเลื่อนแบบสุ่มเล็กน้อยไปยังตำแหน่งเป้าหมายเพื่อสร้างการเคลื่อนไหวที่ดูเป็นธรรมชาติสำหรับ bot character movetoposition(position + new vector3(random range(0 5, 1), 0, random range(0 5, 1))); }); } 5 3 ตอนนี้ถ้าคุณสร้างผู้เล่น bot ในระหว่างการทำงานและกดปุ่ม buttoncallbot คุณควรเห็นผู้เล่น bot ที่สร้างขึ้นเคลื่อนที่ไปยังตำแหน่งตัวละครผู้เล่นท้องถิ่น โค้ดทั้งหมดของ botplayermanager ts botplayermanager ts import { random, vector3 } from 'unityengine'; import { button } from 'unityengine ui'; import { zepetocharacter, zepetoplayers } from 'zepeto character controller'; import { room } from 'zepeto multiplay'; import { zepetoscriptbehaviour } from 'zepeto script' import { zepetoworldmultiplay } from 'zepeto world'; export default class botplayermanager extends zepetoscriptbehaviour { // public properties to reference necessary components and settings from the inspector public zepetoworldmultiplay zepetoworldmultiplay; public buttoncreatebot button; public buttoncallbot button; public botplayerid string; // private variables to store the current room and bot player data private room room; private botmapdata map\<string, zepetocharacter> = new map\<string, zepetocharacter>(); start() { // listen for the `roomjoined` event from the `zepetoworldmultiplay` component this zepetoworldmultiplay roomjoined += (room room) => { this room = room; // add a message handler for the "startworld" message type this room addmessagehandler("startworld", (playtime number) => { this startworld(playtime); }); } // listen for the `onaddedplayer` event from `zepetoplayers instance` to handle newly added players zepetoplayers instance onaddedplayer addlistener((userid string) => { // get the current player data from the room state using the `userid` const currentplayer = this room state players get item(userid); // check if the player is a bot, and if so, set them as a bot player if (currentplayer isbot) { this setbotplayer(currentplayer sessionid); } }); // add a click listener to the "create bot" button to send a message to create a bot player this buttoncreatebot onclick addlistener(() => { this room send("createbot", this botplayerid); }); this zepetoworldmultiplay roomjoined += (room room) => { this room = room; this room addmessagehandler("movebottoposition", (message string) => { this movebottoposition(message); }); } // add a click listener to the "call bot" button to send a message to send a bot player position this buttoncallbot onclick addlistener(() => { this sendbotposition(); }); } // the `setbotplayer()` method is used to set a player as a bot setbotplayer(userid string) { // get the zepeto character associated with the bot player using their `userid` const bot = zepetoplayers instance getplayer(userid) character; // set the name of the character to the `userid` for identification bot gameobject name = userid; // store the bot player's data in the ` botmapdata` map using their `userid` as the key this botmapdata set(userid, bot); } // the `startworld()` method is called when the world starts with the provided `playtime` startworld(playtime number) { // print the world start message along with the `playtime` console log(`start world ${playtime}`); } // this method sends the position of the local player's character to the server for bot movement synchronization sendbotposition() { // get the position of the local player's character const localplayerposition = zepetoplayers instance localplayer zepetoplayer character transform position; // create a position object containing the x, y, and z coordinates of the local player's character const position = { x localplayerposition x, y localplayerposition y, z localplayerposition z } // convert the position object to a json string and send it to the server with the "movebot" message type this room send("movebot", json stringify(position)); } // this method is called when the server receives the "movebot" message from a client and moves the bot characters to the specified position movebottoposition(message) { // parse the json message received from the client to extract the position information const jsonmessage = json parse(message); const position = new vector3(jsonmessage positionx, jsonmessage positiony, jsonmessage positionz); // move each bot character in the ` botmapdata` map to the specified position with a small random offset on the x and z axes this botmapdata foreach((character zepetocharacter) => { // the `movetoposition()` method is used to move the character to the specified position // here, a small random offset is added to the target position to create a natural looking movement for the bots character movetoposition(position + new vector3(random range(0 5, 1), 0, random range(0 5, 1))); }); } } โค้ดเซิร์ฟเวอร์ index ts เต็มรูปแบบ นำเข้า { sandbox, sandboxoptions, sandboxplayer } จาก "zepeto multiplay"; import { datastorage } จาก "zepeto multiplay datastorage"; import { player, transform, vector3 } จาก "zepeto multiplay schema"; export default class extends sandbox { storagemap map\<string, datastorage> = new map\<string, datastorage>(); // บันทึกข้อมูลแผนที่ของผู้เล่นบอทเป็น botmap private botmap map\<string, player> = new map\<string, player>(); private playtime number = 0; constructor() { super(); } oncreate(options sandboxoptions) { // ถูกเรียกเมื่อวัตถุห้องถูกสร้างขึ้น // จัดการสถานะหรือการเริ่มต้นข้อมูลของวัตถุห้อง this onmessage("onchangedtransform", (client, message) => { this state players get(client sessionid); const player = this state players get(client sessionid); const transform = new transform(); transform position = new vector3(); transform position x = message position x; transform position y = message position y; transform position z = message position z; transform rotation = new vector3(); transform rotation x = message rotation x; transform rotation y = message rotation y; transform rotation z = message rotation z; if (player) { player transform = transform; } }); this onmessage("onchangedstate", (client, message) => { const player = this state players get(client sessionid); if (player) { player state = message state; player substate = message substate; } }); // จัดการข้อความ "createbot" ซึ่งสร้างผู้เล่นบอทด้วย userid ที่กำหนด this onmessage("createbot", (client, message) => { this createbot(message); }); // จัดการข้อความ "movebot" ซึ่งย้ายผู้เล่นบอทไปยังตำแหน่งที่กำหนด this onmessage("movebot", (client, message) => { this movebot(client, message); }); } // วิธี `createbot()` ใช้เพื่อสร้างผู้เล่นบอทด้วย `userid` ที่กำหนด createbot(userid string) { // สร้าง session id สำหรับผู้เล่นบอทโดยใช้ `userid` ที่ให้มา const sessionid = "bot " + userid; // ตรวจสอบว่ามีผู้เล่นบอทที่มี session id เดียวกันอยู่แล้วหรือไม่ ถ้ามีให้กลับโดยไม่สร้างซ้ำ if (this state players has(sessionid)) { return; } // สร้างวัตถุ `player` ใหม่สำหรับผู้เล่นบอท const player player = new player(); player sessionid = sessionid; if (userid) { player zepetouserid = userid; } player isbot = true; // เพิ่มผู้เล่นบอทไปยังแผนที่ผู้เล่นของสถานะโดยใช้ session id เป็นกุญแจ this state players set(player sessionid, player); this botmap set(sessionid, player); // ตรวจสอบจำนวนผู้เล่นในห้องหลังจากเพิ่มผู้เล่นบอท this checkplayernumber(); } // วิธี `checkplayernumber()` ตรวจสอบจำนวนผู้เล่นในห้องและเริ่มโลกหากมีผู้เล่นอย่างน้อยสี่คน checkplayernumber() { // พิมพ์จำนวนผู้เล่นในห้องปัจจุบันไปยังคอนโซล console log(`จำนวนผู้เล่น, ${this state players size}`); // หากมีผู้เล่นอย่างน้อยสี่คนในห้องให้เริ่มโลก if (this state players size >= 4) { this startworld(); } } // วิธี `startworld()` เพิ่มเวลาเล่นและกระจายข้อความ "startworld" ไปยังลูกค้าทั้งหมด startworld() { this playtime += 1; // พิมพ์ข้อความที่บ่งบอกถึงการเริ่มต้นโลกและเวลาเล่นปัจจุบัน console log("เริ่มโลก!"); this broadcast("startworld", this playtime); } // วิธี `movebot()` ย้ายผู้เล่นบอทไปยังตำแหน่งที่กำหนดตามข้อความที่ได้รับ movebot(client sandboxplayer, message string) { // แยกข้อความ json ที่ได้รับจากลูกค้าเพื่อดึงข้อมูลตำแหน่ง const position = json parse(message); // สร้างวัตถุข้อความใหม่ด้วย session id ของผู้ใช้และข้อมูลตำแหน่งที่แยกออก const newmessage = { user client sessionid, positionx position x, positiony position y, positionz position z } // กระจายข้อความ "movebottoposition" ไปยังลูกค้าทั้งหมดด้วยข้อมูลข้อความใหม่ในรูปแบบ json this broadcast("movebottoposition", json stringify(newmessage)); } async onjoin(client sandboxplayer) { // สร้างวัตถุผู้เล่นที่กำหนดใน schemas json และตั้งค่าค่าตั้งต้น console log(`\[onjoin] sessionid ${client sessionid}, hashcode ${client hashcode}, userid ${client userid}`) const player = new player(); player sessionid = client sessionid; if (client hashcode) { player zepetohash = client hashcode; } if (client userid) { player zepetouserid = client userid; } // \[datastorage] การโหลด datastorage ของผู้เล่นที่เข้ามา const storage datastorage = client loaddatastorage(); this storagemap set(client sessionid, storage); let visit cnt = await storage get("visitcount") as number; if (visit cnt == null) visit cnt = 0; console log(`\[onjoin] จำนวนการเยี่ยมชมของ ${client sessionid} ${visit cnt}`) // \[datastorage] อัปเดตจำนวนการเยี่ยมชมของผู้เล่นและบันทึก storage await storage set("visitcount", ++visit cnt); // จัดการวัตถุผู้เล่นโดยใช้ sessionid ซึ่งเป็นค่ากุญแจที่ไม่ซ้ำกันของวัตถุผู้ใช้ // ลูกค้าสามารถตรวจสอบข้อมูลเกี่ยวกับวัตถุผู้เล่นที่เพิ่มโดยการตั้งค่าโดยการเพิ่มเหตุการณ์ add onadd ไปยังวัตถุผู้เล่น this state players set(client sessionid, player); // ตรวจสอบจำนวนผู้เล่นในห้องหลังจากผู้เล่นเข้าร่วม this checkplayernumber(); } ontick(deltatime number) void { // ถูกเรียกซ้ำ ๆ ในแต่ละเวลาที่ตั้งไว้ในเซิร์ฟเวอร์และสามารถจัดการเหตุการณ์ในช่วงเวลาที่แน่นอนได้โดยใช้ deltatime } async onleave(client sandboxplayer, consented? boolean) { // โดยการตั้งค่า allowreconnection จะสามารถรักษาการเชื่อมต่อสำหรับวงจรได้ แต่ทำความสะอาดทันทีในคู่มือพื้นฐาน // ลูกค้าสามารถตรวจสอบข้อมูลเกี่ยวกับวัตถุผู้เล่นที่ถูกลบโดยการเพิ่มเหตุการณ์ add onremove ไปยังวัตถุผู้เล่น this state players delete(client sessionid); } }