Networked audio clips

Updated a year ago

What is this feature for?

Networked audio clip feature of LCSoundTool let's you send any audio clip over the network while in game to all clients. This can be used to make sure all players have the same custom sound or maybe you want to record something client side and then send it over to all players? The use cases are up to you as the mod developer using this part of the API.

Requirements

To be able to utilize the networking of LCSoundTool, all clients must have the mod installed and the config option for networking enabled. If someone has either of these off they won't be able to connect to the lobby and will receive an error. If you use this part of LCSoundTool in your mod, you should mention it to users on the mod page to avoid unnecessary trouble.

Accessing the networked audio clips

Remember to check out some basic tutorials for BepInEx mods first, but this should be fairly easy to follow. First of all, to be able to use any part of LCSoundTool, you want to add using LCSoundTool; to the top of your CSharp file. Most LCSoundTool functionality can be accessed through the SoundTool class. Here is some basic pseudo code that let's you access a specified networked sound.

using LCSoundTool;

AudioClip networkedSound = SoundTool.networkedClips["MyEpicSound"];

This code uses the SoundTool.networkedClips dictionary to pull a sound with the specified name, in this case "MyEpicSound".

Sending and removing networked audio clips

When manipulating networked audio clips, remember that the changes effect all connected players. Here is some pseudo code that sends a new networked clip to all clients.

using LCSoundTool;

AudioClip networkedSound;

SoundTool.SendNetworkedAudioClip(networkedSound);

Here we use the SendNetworkedAudioClip() method to send the audio clip called networkedSound to all clients and after that it can be accessed through the SoundTool.networkedClips dictionary. The networkedSound could of course be loaded with various different ways. You could use AssetBundles or the built in audio file loader. Here is some pseudo code that removes a networked clip from all clients.

using LCSoundTool;

SoundTool.RemoveNetworkedAudioClip(networkedSound);

Here we use the RemoveNetworkedAudioClip() method to request the specified clips removal from all clients. The parameter networkedSound is the name of the networked sound, but it can be either an audio clip or string, as with most methods the mod converts passed audio clips to strings by grabbing their names if you use them as parameters. What if someone joins the lobby later or you want to make sure all players have the same networked audio clips? Here is some pseudo code for those occasions.

using LCSoundTool;

SoundTool.SyncNetworkedAudioClips();

Here we simply use the SyncNetworkedAudioClips() method to ensure all connected clients get the same networked audio clips. This method just takes all of the host's networked clips and sends them to all connected clients and if they are missing any clips the host sends them over to them or if they have extra audio clips they get removed.

Tracking changes to networked sounds

Maybe your mod needs to do things when a new networked sound is added or it just needs to know about the occasion. Whatever the case may be, you can use these events to get notified of changes to the networked audio clips. Here is some pseudo code.

using LCSoundTool;

// Here are the two available events
SoundTool.ClientNetworkedAudioChanged;
SoundTool.HostNetworkedAudioChanged;

// Let's subscribe to the client one
SoundTool.ClientNetworkedAudioChanged += YourMethod;

// Let's unsubscribe from the client one
SoundTool.ClientNetworkedAudioChanged -= YourMethod;

public void YourMethod() { ... }

Here we show the two available events and how to subscribe and unsubscribe from them. If you want more information I recommend looking up C# events. Remember that if you use events you should always make sure to unsubscribe from them as well. For way more in-depth example on how these events can be utilized check out the example mod on github.