Bolt allows you to hook into various global callback methods that are triggered when different actions occur inside of Bolt. To implement these callbacks create a new C# script in Unity and instead of inheriting from
Bolt.GlobalEventListener. Below is an example of this.
We can for example implement the
Connected method and have Bolt notify us every time we are connected to a new peer. You can find a list of all the callback methods you can implement in the API Documentation.
Important: Bolt uses the C# standard
public override ... and not the way Unity does it where you don't have to specify the
There are two ways to get Bolt to detect your callback script and invoke the methods on it, the most basic one is to just do the default 'Unity thing' and attach it to a game object somewhere in a scene like you would with any script.
The other way is to use the
[BoltGlobalBehaviour] attribute, if you specify this attribute on your callback class, like below, Bolt will automatically find your class and create an instance of it that lives with together with Bolt and is destroyed when Bolt is shutdown.
Using this attribute means that you don't have to manually deal with keeping a game object around, etc. You can also specify a couple of parameters to the attribute which lets you customize a few options, here are a few example of this.
If you want callbacks that only get called on the server you can pass in
BoltNetworkModes.Serveras the first parameter:
[BoltGlobalBehaviour(BoltNetworkModes.Server)], the same goes if you only want to have callbacks on the clients:
You can also specify that you only want the callbacks to be active in certain scenes by passing in a scene name
[BoltGlobalBehaviour("Level1")]. You can also pass in several scene names after one another:
[BoltGlobalBehaviour("Level1", "Level2")]. Or you can specify a regular expression that can match several scenes names:
And as a last option you can combine both the client/server mode option and the scene name filter: