PlayerManager

PlayerManager

The player manager is a model-side service that automatically spawns an actor whenever a new player joins. When that player exits, it automatically destroys the actor. (it listens for the view-join and view-exit events from the base Croquet library. This allows you to automatically spawn an avatar for a player when they enter a session.

  1. Create your own player manager class that inherits from this base class.
  2. Override the createPlayer() method to create your player avatar.
  3. Add your player manager as a service to the root model.
class MyPlayerManager extends PlayerManager {

     createPlayer(options) {
         options.extra = "an extra option";
         return MyPlayerAvatar.create(options);
     }
}

class MyModelRoot extends ModelRoot {
     createServices() {
         this.addService(MyPlayerManager);
 }

Each player has a unique playerId that can be used to retrieve it from the player manager's player list.

Extends

Members

# count :number

The number of players in the session.

Type:
  • number

Methods

# createPlayer(options) → {Actor}

Spawns a player actor. Called automatically when a new player joins. You should override this method so that it returns your player avatar. Pass the options object to the create method of your avatar class. You can add other custom options to the options object before passing it. (For example, if you wanted to randomly pick a spawn point for the avatar.)

Note: Your player avatar should be extended with the AM_Player mixin.

Parameters:
Name Type Description
options Object

An options object that should be passed to the actor when its created.

Returns:
Type
Actor
Example
class MyPlayerManager extends PlayerManager {

     createPlayer(options) {
         options.translation = [0,0,0]; // The default spawn point
         return MyPlayerAvatar.create(options);
     }
}

class MyPlayerAvatar extends mix(Actor).with(AM_Player) {}

# player(playerId) → {Actor}

Given a playerId, returns a pointer to the avatar associated with the player.

Parameters:
Name Type Description
playerId string

A unique identifier assigned when the player joins.

Returns:
Type
Actor

# init(name, optionsopt)

Called when the new service is instantiated. An options object may be supplied by the model root. You should overload this when you create your own service and name the service using super.init(). If the service needs to regularly update itself, start a tick here using this.future().

Warning: Never create a service directly. Always use modelServices() in the model root.

Parameters:
Name Type Attributes Description
name string

The public name of the service. Use super.init() in your service to set its name.

options Object <optional>

An options object that is supplied to when the service is added.

Inherited From:
Example
class MyModelService extends ModelService {
        init(options = {}) {
            super.init("MyModelServiceName");
            // Apply options and perform other initialization.
        }
    }

# service(name) → {ModelService}

Returns a pointer to the named model service.

Parameters:
Name Type Description
name string

The public name of the model service.

Inherited From:
Returns:
Type
ModelService