Bark Service
Description
Decides when agents bark, picks what they say, and hands the line to presentation and audio. Everything it plays through is behind an interface, so any part can be replaced or added to. See Barks for how barks are authored.
Every service is registered with the module system. Get one with:
var barks = ModuleInitializer.GetService<IBarkService>();
IBarkService
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| Sources | N/A | IReadOnlyList<IBarkSource> | Where barks come from, in the order they're asked. |
| AddSource | IBarkSource source | void | Adds a source after the others. |
| InsertSource | int index, IBarkSource source | void | Adds a source at a position. 0 is asked first. |
| RemoveSource | IBarkSource source | bool | Removes a source. |
| TryBark | IAgentService agent | bool | Gives the agent its regular chance to bark. True if a bark started. |
| Bark | IAgentService agent, BarkData bark, bool ignoreCooldowns = false | bool | Makes the agent say a specific line now. Still gives way to conversations and blocks. |
| Cancel | IAgentService agent | void | Stops the agent's current bark. |
| CancelAll | N/A | void | Stops every bark. |
| IsBarking | IAgentService agent | bool | Whether the agent is barking. |
To make an agent bark from your own code, for example when it spots the player:
ModuleInitializer.GetService<IBarkService>()?.Bark(agent, spottedLine);
IBarkSource
Somewhere a bark can come from. Sources are asked in order and the first to offer a collection is used. The General bank is added as the last source.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| FindCollection | IAgentService agent | BarkCollection | The collection this agent should bark from, or null to pass it on. |
Insert your own source at index 0 to take priority over the General bank.
IBarkAvailabilityCheck
A veto on an agent starting a bark. Every check is asked, and any one returning false stops the bark. This is the bark version of IConversationAvailabilityCheck.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| CanBark | IAgentService agent | bool | False to stop this agent barking. |
Register your own with RegisterMulti<IBarkAvailabilityCheck> in your module registrar. They sit alongside NodeBarkBlockPolicy, which applies the Block Barks option on behaviour tree nodes.
IBarkPresentationService
Shows a bark's text.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| Present | IAgentService agent, string text, float duration | void | Shows the text and hides it after the duration. |
| Cancel | IAgentService agent | void | Hides the agent's text now. |
| CancelAll | N/A | void | Hides all bark text. |
| Implementation | Used when |
|---|---|
WorldTextBarkPresentationService | CIVIL-DIALOGUE-SYSTEM isn't installed. |
DialogueBarkPresentationService | CIVIL-DIALOGUE-SYSTEM is installed. Uses its overhead text bubble. |
NoOpBarkPresentationService | CIVIL-DIALOGUE-SYSTEM isn't installed and Show Text is off, or TextMeshPro's essential resources are missing. |
IBarkAudioResolver
Chooses which clip an agent plays for a line. The default returns the line's own clip. Replace it to give agents different voices for the same line, using the line's id.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| Resolve | IAgentService agent, string lineId, AudioClip fallbackClip | AudioClip | The clip to play, or null. |
A line's id is fixed when the line is created and doesn't change if the asset is renamed. Read it with BarkData.GetLineId().
IBarkAudioPlayer
Plays bark audio.
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| ActiveCount | N/A | int | How many bark clips are playing. Used for the audio limit. |
| Play | IAgentService agent, AudioClip clip, float pitch | void | Plays the clip from the agent. |
| Stop | IAgentService agent | void | Stops the agent's bark clip. |
| StopAll | N/A | void | Stops every bark clip. |
The default, AgentAudioSourceBarkPlayer, plays through an Audio Source on the agent and never changes that Audio Source's assigned clip, so it can share one with dialogue.
Replacing a default
The Civil AI module registers the bark service when modules register, and registers the presenter, audio player and audio resolver later, when modules initialise, but only if nothing is registered for them yet. To use your own, register it in your module registrar's Register method:
public void Register(IServiceRegistry registry, ModuleBootstrapContext context)
{
registry.TryRegisterSingleton<IBarkAudioResolver>(ModuleId, new VoiceBankResolver());
}
Every module finishes registering before any module initialises, so yours is in place first and the default is skipped.