Skip to main content
Supported on
Snapchat

Session Controller

SessionController is the scripting API for the live session. Where Multiplayer Manager is the component you configure in the Inspector, Session Controller is what you call from code to ask who is in the match, who the host is, what the server time is, and to react as players join and leave.

It is a singleton. Get it with SessionController.getInstance(), or from the Multiplayer Manager with getSessionController().

Local user

Name (signature)Description
getLocalUserId(): string | nullPersistent user id for the local player.
getLocalConnectionId(): string | nullConnection id for this session only. Changes between sessions.
getLocalUserName(): string | nullDisplay name for the local player.
getLocalUserInfo(): ConnectedLensModule.UserInfo | nullFull user info for the local player.
isLocalUser(userInfo): booleanWhether the given user is the local player.

Use the user id when you need identity that survives a reconnect, such as a persistent score. Use the connection id when you mean "this participant in this session." They are not interchangeable, and a single user id can appear more than once, which is why getUsersByUserId() returns an array.

Host

Name (signature)Description
isHost(): boolean | nullWhether the local player is the host.
getHostUserId(): string | nullPersistent user id of the host.
getHostConnectionId(): string | nullConnection id of the host.
getHostUserName(): string | nullDisplay name of the host.
getHostUserInfo(): ConnectedLensModule.UserInfo | nullFull user info for the host.
isHostUser(userInfo): booleanWhether the given user is the host.

The host can change mid-session when the current host leaves. Listen to onHostUpdated rather than caching the result of isHost().

Users

Name (signature)Description
getUsers(): ConnectedLensModule.UserInfo[]Every user currently in the session.
getUserById(userId: string): ConnectedLensModule.UserInfo | nullLook up a user by persistent user id.
getUserByConnectionId(connectionId: string): ConnectedLensModule.UserInfo | nullLook up a user by connection id.
getUsersByUserId(userId: string): ConnectedLensModule.UserInfo[]Every participant sharing one user id.

Session state

Name (signature)Description
getSession(): MultiplayerSession | nullThe underlying session object.
getState(): StateCurrent session state.
getSessionCreationType(): ConnectedLensSessionOptions.SessionCreationTypeHow this session was created.
isSingleplayer(): booleanWhether the Lens is running without a real session.
getIsSessionShared(): booleanWhether the session has been shared with others.
shareInvite()Opens the invite flow so the local player can bring friends in.
getIsReady(): booleanWhether the controller has finished initializing.
waitForReady(): Promise<void>Resolves once the controller is ready.
leaveSession()Leaves the current session.
getIsConnectionFirstJoiner(): booleanWhether this connection was the first to join.
getIsUserFirstJoiner(): booleanWhether this user was the first to join.

Server time

Name (signature)Description
getServerTimestamp(): number | nullCurrent server timestamp.
getServerTimeInSeconds(): number | nullCurrent server time in seconds.

Use server time, not local device time, for anything every client must agree on: round countdowns, match duration, and finish times. Device clocks drift.

Realtime stores

Name (signature)Description
getSessionStore(): GeneralDataStore | nullThe session-wide store, shared by every participant.
createStore(storeOptions: RealtimeStoreCreateOptions): Promise<GeneralDataStore>Creates a new realtime store.
getTrackedStores(): StoreInfo[]Every store the controller is tracking.
getStoreInfoById(networkId: string): StoreInfo | nullLook up a tracked store by network id.

Most gameplay code should not touch stores directly. Prefer Sync Entity and Storage Property, which manage a store for you and give you change events.

Events

NameCalled withDescription
onReadynoneThe controller finished initializing.
onSessionCreatedsession, creationTypeA session was created.
onSessionSharedsessionThe session was shared.
onConnectedsession, connectionInfoThe local player connected.
onDisconnectedsession, disconnectInfoThe local player disconnected.
onUserJoinedSessionsession, userInfoA player joined.
onUserLeftSessionsession, userInfoA player left.
onHostUpdatedsession, removalInfoThe host changed.
onMessageReceivedsession, userId, message, senderInfoA network message arrived.
onErrorsession, errorA session error occurred.
onConnectionFailederrorConnecting failed.
onRealtimeStoreCreatedsession, store, persistence, ownerInfo, ownershipA realtime store was created.
onRealtimeStoreUpdatedsession, store, key, updateInfoA realtime store value changed.
onRealtimeStoreDeletedsession, store, deleteInfoA realtime store was deleted.
onRealtimeStoreKeyRemovedsession, store, removalInfoA key was removed from a store.
onRealtimeStoreOwnershipUpdatedsession, store, ownerInfo, ownershipUpdateInfoStore ownership changed.

Usage

import { SessionController } from 'ConnectedFramework.lspkg/Core/SessionController';

@component
export class RosterDisplay extends BaseScriptComponent {
onAwake() {
const session = SessionController.getInstance();

session.onReady.add(() => {
print(`Players in session: ${session.getUsers().length}`);
});

session.onUserJoinedSession.add((_session, userInfo) => {
print(`${userInfo.displayName} joined`);
});

session.onUserLeftSession.add((_session, userInfo) => {
print(`${userInfo.displayName} left`);
});

session.onHostUpdated.add(() => {
if (session.isHost()) {
print('This client is now the host');
}
});
}
}

Advanced

Name (signature)Description
addSessionOptionsModifier(modifier: SessionOptionsModifier): SessionOptionsModifierRegisters a modifier applied when session options are built.
removeSessionOptionsModifier(modifier: SessionOptionsModifier): voidRemoves a previously registered modifier.
createSessionOptions()Builds the session options, with every registered modifier applied.
Was this page helpful?
Yes
No