Multiplay Room provides a State property for managing connected player or object location.
State's data definition be managed from the Schema file, which is below the multiplay package.
The onStateChange event is first called when the client successfully establishes a connection to the room, and is afterwards called whenever there are changes to the state data.
Server API
API | Description |
---|---|
state.SCHEMA_NAME | You can access the SCHEMA_NAME field defined in the schema file with state. You can modify the field value at a request from a client or internal server logic. The changed state is then automatically propagated to the clients using the OnStateChange event. |
Please refer to the following guide. [ZEPETO.Multiplay(Server) API]
import { Location } from 'ZEPETO.Multiplay.Schema';
export default class extends Sandbox {
...
const location: Location = this.state.playerLocation;
// Handling State.
...
}
Client API
API | Description |
---|---|
room.OnStateChange(State, isFirst) | You can register a state change callback when you join the room. You can receive the state of the entire room for the first time, and only the changed state will be received afterwards. |
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Location } from 'ZEPETO.Multiplay.Schema';
export default class SampleClient extends ZepetoScriptBehaviour {
Start() {
this.multiplay.RoomJoined += (room: Room) => {
room.OnStateChange += this.OnStateChange;
};
}
private OnStateChange(state: State, isFirst: boolean) {
const location: Location = state.playerLocation;
// Handling player location using updated State.
}
}
Define Schema file
Schema is a data structure for defining the current state in the room. Rooms are primarily used to manage player information, player or object locations, etc.
To edit a schema, you can select Project View → MultiplayPackage Asset → schema.json, and then modify the data type to use for the World in the Inspector window.
You can modify the data type to use for the World in the Inspector window.
STEP 1 : Defining Schema Types
Scheme Types is the Data Structure used in the World. In the Inspector → Schema Types, press the [+] button and add data.
STEP 2 : Add Room State
The Room State is the Property that shows the current status of the World Room, and is expressed in the Schema structure as defined above.
Among the Schema Types, select the typeRoom State that will be used as the current status of the World and set it as the SCHEMA_NAME.
Schemas.json Example
{
"State" : {"players" : {"map" : "Player"}},
"Player" : {"sessionId" : "string","zepetoUserId" : "string","transform" : "Transform","state" : "number","subState" : "number"},
"Transform" : {"position" : "Vector3","rotation" : "Vector3"},
"Vector3" : {"x" : "number","y" : "number","z" : "number"}
}