Lua API
BeamVoice.lua is a small Lua file that lets your BeamMP server plugins use BeamVoice: see who is in the voice chat, manage group chats, mute players, invite them to join and react to voice events.
This is version 1.1 of the API. It needs a BeamVoice server plugin with API 1.1 on the same BeamMP server and does not work with a plugin older than v1.2.0 (see Compatibility).
Install
Section titled “Install”-
Copy
BeamVoice.luainto thelua/folder of your plugin, andLICENSEnext to it asBeamVoice-LICENSE:Resources/Server/MyPlugin/main.luaResources/Server/MyPlugin/lua/BeamVoice.luaResources/Server/MyPlugin/lua/BeamVoice-LICENSEThe license must ship with every plugin that includes
BeamVoice.lua. -
Load it from your code:
local BeamVoice = require("BeamVoice")
BeamVoice.lua, its LICENSE and a complete example plugin (group chats with /cat, /mouse and /leave) are in the BeamVoice API repository.
Quick start
Section titled “Quick start”local BeamVoice = require("BeamVoice")
local teamGroup = nil
-- runs once BeamVoice is up, and again after each reconnection (the groups are gone by then)BeamVoice.onReady(function() local groupId, err = BeamVoice.groupChat.create("Team") if not groupId then print("Could not create the group: " .. err) end teamGroup = groupIdend)
-- everyone who joins the voice chat lands in the team groupBeamVoice.on("playerJoinedVoice", function(playerId) if teamGroup then BeamVoice.groupChat.addPlayer(teamGroup, playerId) endend)Editing this file on a running server runs it again: the listeners of the previous run stay registered and another group is created. The example plugin handles that (see Edit your plugin on a running server).
How it works
Section titled “How it works”-
Return values. Every function returns its value on success and
nil, errorCodeon failure. Functions that change something returntrue(groupChat.createreturns the new group id,groupChat.addPlayersa result table). Some functions can returnfalseas a valid answer (isInVoice,isSpeaking), so compare withnilwhen you need to tell an error apart:local inVoice, err = BeamVoice.isInVoice(playerId)if inVoice == nil thenprint("BeamVoice error: " .. err)end -
Error codes are strings, listed in Error codes and in the
BeamVoice.errortable (BeamVoice.error.groupNotFound == "groupNotFound"). Any function can also returnnotReady,incompatible,timeout,unsupportedorunknownError; the other codes of each function are listed with it. Treat a code you don’t know likeunknownError. -
Loading. Loading the file does nothing. The wrapper looks for the BeamVoice plugin on the first call. If BeamVoice is not loaded yet, the call returns
notReadyand the next call tries again. BeamMP loads plugins in alphabetical order, so put your setup inBeamVoice.onReady(see Waiting for BeamVoice), or call the API fromonInitor later (commands, events, timers): by then every plugin is loaded, whatever the folder names. -
Calls are synchronous. Your plugin waits for the answer, up to a timeout (10 seconds by default, see Timeouts).
-
Ids. Players are BeamMP player ids (numbers, the first player is
0). Groups are strings such as"g1". -
Lists are arrays of records (
{ { id = 0, name = "..." }, ... }), never tables keyed by player id. Fields that have no value arenil.
Waiting for BeamVoice
Section titled “Waiting for BeamVoice”BeamVoice.onReady(function(epoch) -- create your groups, set voice access...end)listener(epoch) runs once each time the voice chat comes up: right away when it already is, else as soon as it does, then again after every reconnection (a new epoch, your groups are gone by then).
- It works whatever the plugin load order: call it when your
main.lualoads, even before BeamVoice is loaded. - If BeamVoice is too busy to answer within the timeout,
onReadystill returnstrueand asks again every second until it answers. - It can run before your
onInit, so define what it uses above the call. - An error in the listener is logged.
- A listener that already ran for the current epoch is not run again, even if you register it again.
When you edit your main.lua on a running server, call BeamVoice.off("ready") first, otherwise the listeners of the previous run stay registered and run again at each reconnection.
Timeouts
Section titled “Timeouts”- Every call waits at most 10 seconds by default (real time).
BeamVoice.setTimeout(seconds)changes it for your plugin. - While a call waits, your plugin does nothing else: BeamMP runs each plugin on its own thread and the wrapper sleeps on yours. Most calls answer in a few milliseconds. Calls that change something on the voice chat (groups, voice access) talk to the audio node and can take longer.
- BeamVoice handles calls one at a time, so a slow call delays the next ones, also those of other plugins.
- Each call carries a deadline. BeamVoice skips a call that waited past it, but a call it already started keeps going even if your plugin gave up. So
timeouton a change (create,delete,rename,setOptions,addPlayer,addPlayers,removePlayer,setVoiceAccess,promptJoin) means the outcome is unknown: read the state again (getGroup,getPlayerInfo,getVoiceAccess…) before trying again. A read can simply be retried.
Privacy
Section titled “Privacy”BeamVoice never gives other plugins any audio. Who is in the voice chat, groups and voice access are always available. Two kinds of voice data are off by default, and only the server owner can turn them on, in configs/BeamVoice.toml:
[api]speaking_state = falsevoice_keys = falsespeaking_state: who is talking. When off,isSpeakingandgetSpeakingPlayersreturnfeatureDisabledandspeakingChangednever fires. It can be changed while the server runs: turning it off sendsspeakingChanged(playerId, false, ...)for everyone talking, turning it on sendstruefor everyone talking.voice_keys: BeamVoice key presses (push to talk, mute, group mute and deafen). When off,voiceKeynever fires.
getCapabilities() tells you both settings (speakingState, voiceKeys). If your plugin acts on this data (logs it, shows it, sends it somewhere, rewards or punishes players for it), tell your players, for example in the server description or rules.
Compatibility
Section titled “Compatibility”- This file needs a BeamVoice plugin with API 1.1. It does not work with a plugin older than v1.2.0: such a plugin never answers this file and looks like a plugin that is not loaded.
isReady()and every function that asks BeamVoice returnnotReadyon every call,isDisabled()staysfalseand no event fires. Features that need a newer audio node or plan show up ingetCapabilities()and returnunsupportedorfeatureNotInPlanwhen missing. - Load order does not matter with
onReady, or when you call the API fromonInitor later. - BeamMP 3.9.3 or newer is recommended. Older servers pass values between plugins in a less safe way.

