Session Controller
The SessionController is a script that manages the state of a shared session and provides APIs for accessing session information, such as users in the session and server time. It acts as a central interface that all Spectacles Sync Kit scripts can talk to and get the information they need. The SessionControllerComponent can be found on the SessionController [CONFIGURE_ME] scene object in the Scene Hierarchy.
Skip Joining Flow UI in Lens Studio
The Spectacles Sync Kit package includes UI for guiding users to map or relocalize against the colocated coordinate space. The UI can be viewed in the Preview panel after selecting Multiplayer in the Start Menu. During Lens development, you can choose to skip this UI by enabling Skip UI in Lens Studio in the Inspector panel.
Referencing SessionController
The SessionController script can be referenced from other scripts as follows:
- TypeScript
- JavaScript
import {SessionController} from "SpectacleSyncKit/Core/SessionController" // path may differ
onAwake() {
const sessionController: SessionController = SessionController.getInstance()
}
const sessionController = global.sessionController;
Session Setup
The SessionController handles a number of steps when a user starts or joins a shared session. It is important that the SessionController has completed this setup before other scripts attempt to use SessionController APIs. To check that the SessionController is ready to use, call SessionController.notifyOnReady()
. The method accepts a callback function that executes once session setup is complete. If setup is already finished, the callback function runs immediately.
- TypeScript
- JavaScript
const sessionController: SessionController = SessionController.getInstance();
sessionController.notifyOnReady(() => {
// SessionController is ready to use
});
global.sessionController.notifyOnReady(() => {
// SessionController is ready to use
});
You can also check if setup has completed by checking SessionController.getIsReady()
, which returns a boolean.
- TypeScript
- JavaScript
if (SessionController.getInstance().getIsReady()) {
// Session is ready
}
if (global.sessionController.getIsReady()) {
// Session is ready
}
Logging
The SessionController scene object has a script attached to it called SyncKitLogLevelConfiguration. From the Inspector panel, select the Log Level Filter to control what type of logs are printed from Spectacles Sync Kit in the Logger. The available options are: Error, Warning, Info, Debug, or Verbose.
APIs
The SessionController script offers the following APIs for accessing information about the current shared session.
Important: Wait until SessionController.notifyOnReady()
has been called or SessionController.getIsReady()
returns true before using these APIs.
SessionController.getIsReady(): boolean
Returns true if the SessionController has finished joining the session, and the user has successfully mapped or relocalized.
SessionController.getLocalConnectionId(): string
Returns the connection ID of the local user.
SessionController.getLocalUserId(): string
Returns the user ID of the local user.
SessionController.getLocalUserInfo(): ConnectedLensModule.UserInfo
Returns the UserInfo object of the local user.
SessionController.getLocalUserName(): string
Returns the display name of the local user.
SessionController.getSession(): MultiplayerSession
Returns the current MultiplayerSession object. Always use this instead of storing a reference to the session, since it is possible for the session to change.
SessionController.getUsers(): ConnectedLensModule.UserInfo[]
Returns a list of users currently connected to the session, represented as UserInfo objects.
SessionController.getUserByConnectionId(connectionId: string): ConnectedLensModule.UserInfo
Returns the UserInfo of a user currently connected to the session with the matching connection ID, or null if none exists.
SessionController.getUsersByUserId(userId: string): ConnectedLensModule.UserInfo[]
Returns a list of users currently connected to the session who have a matching user ID.
SessionController.getServerTimeInSeconds(): number?
Returns the current server timestamp in seconds, or null if not available.
SessionController.isSingleplayer(): boolean
Returns true if the current session is singleplayer, and false if it is multiplayer.
SessionController.notifyOnReady(onReady)
Calls the onReady callback as soon as the SessionController has finished all setup, and the user has successfully mapped or relocalized. If setup is already finished, the callback is called immediately.