Skip to content

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).

  1. Copy BeamVoice.lua into the lua/ folder of your plugin, and LICENSE next to it as BeamVoice-LICENSE:

    Resources/Server/MyPlugin/main.lua
    Resources/Server/MyPlugin/lua/BeamVoice.lua
    Resources/Server/MyPlugin/lua/BeamVoice-LICENSE

    The license must ship with every plugin that includes BeamVoice.lua.

  2. 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.

main.lua
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 = groupId
end)
-- everyone who joins the voice chat lands in the team group
BeamVoice.on("playerJoinedVoice", function(playerId)
if teamGroup then BeamVoice.groupChat.addPlayer(teamGroup, playerId) end
end)

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).

  • Return values. Every function returns its value on success and nil, errorCode on failure. Functions that change something return true (groupChat.create returns the new group id, groupChat.addPlayers a result table). Some functions can return false as a valid answer (isInVoice, isSpeaking), so compare with nil when you need to tell an error apart:

    local inVoice, err = BeamVoice.isInVoice(playerId)
    if inVoice == nil then
    print("BeamVoice error: " .. err)
    end
  • Error codes are strings, listed in Error codes and in the BeamVoice.error table (BeamVoice.error.groupNotFound == "groupNotFound"). Any function can also return notReady, incompatible, timeout, unsupported or unknownError; the other codes of each function are listed with it. Treat a code you don’t know like unknownError.

  • 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 notReady and the next call tries again. BeamMP loads plugins in alphabetical order, so put your setup in BeamVoice.onReady (see Waiting for BeamVoice), or call the API from onInit or 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 are nil.

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.lua loads, even before BeamVoice is loaded.
  • If BeamVoice is too busy to answer within the timeout, onReady still returns true and 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.

  • 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 timeout on 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.

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:

configs/BeamVoice.toml
[api]
speaking_state = false
voice_keys = false
  • speaking_state: who is talking. When off, isSpeaking and getSpeakingPlayers return featureDisabled and speakingChanged never fires. It can be changed while the server runs: turning it off sends speakingChanged(playerId, false, ...) for everyone talking, turning it on sends true for everyone talking.
  • voice_keys: BeamVoice key presses (push to talk, mute, group mute and deafen). When off, voiceKey never 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.

  • 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 return notReady on every call, isDisabled() stays false and no event fires. Features that need a newer audio node or plan show up in getCapabilities() and return unsupported or featureNotInPlan when missing.
  • Load order does not matter with onReady, or when you call the API from onInit or later.
  • BeamMP 3.9.3 or newer is recommended. Older servers pass values between plugins in a less safe way.