SampSharp

Controllers Edit this page on GitHub

Introduction

Controllers distribute events between instances and manage the living instances. For example, if the BaseMode.PlayerEnteredVehicle event is called, the BasePlayerController will trigger the BasePlayer.EnteredVehicle event. If a player disconnected, the BasePlayerController will destroy the instance of the disconnected player.

Creating Controllers

To create a controller, create a class which implements IController. There are some additional interfaces which can be implemented to add functionality to the controller:

class MyController : IController, IEventListener, ITypeProvider, IGameServiceProvider
{
    public virtual void RegisterEvents(BaseMode gameMode)
    {
        // TODO: Register your events
    }

    public virtual void RegisterTypes()
    {
        // TODO: Register your types
    }

    public virtual void RegisterServices(BaseMode gameMode, GameModeServiceContainer serviceContainer)
    {
        // TODO: Register your services
    }
}

Loading Controllers

To load a controller, override LoadControllers in your game mode and add them to the controllers collection.

protected override void LoadControllers(ControllerCollection controllers)
{
    // Load the default controllers first
    base.LoadControllers(controllers);
            
    controllers.Add(new MyController());
}

Overloading Controllers

To overload a controller, create a subclass of the controllers you are overloading and add it to the controllers collection using the ControllerCollection.Override method.

class MyPlayerController : BasePlayerController
{
    public override void RegisterTypes()
    {
        // Register your own player implementation instead of the default.
        MyPlayer.Register<MyPlayer>();
    }
}

protected override void LoadControllers(ControllerCollection controllers)
{
    // Load the default controllers first
    base.LoadControllers(controllers);
    
    // Override default controllers
    controllers.Override(new MyPlayerController());

    // Add you own controllers
    controllers.Add(new MyController());
}