More work!!!

This commit is contained in:
Starystars67
2026-06-27 00:49:53 +01:00
parent 97a7aaf643
commit c115bc9a42
302 changed files with 28263 additions and 10 deletions
@@ -0,0 +1,11 @@
---
title: BeamMP Scripting
description: BeamMP scripting references for client and server
---
# BeamMP Scripting
## References
- [Mod (In-Game)](./mod-in-game.md)
- [Server v3 (Latest)](./server/latest.md)
- [Server v2 (Legacy)](./server/legacy-v2.md)
@@ -0,0 +1,43 @@
::: warning "This site is under construction!"
This site is being actively worked on.
Feel you could help? Please do by clicking on the page with a pencil on the right!
This can be done any page too.
# Mod/In-Game Scripting Reference
BeamMP allows you to create your own client side plugins as well. We have provided a few functions that you can use to communicate with other multiplayer mods, and other players through the server.
# Functions
List of available functions for scripting:
| Function | Notes |
|-------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `TriggerServerEvent("eventName", "data")` | Triggers an event in the server lua environment, both parameters are strings. |
| `TriggerClientEvent("eventName", "data")` | Triggers an event in the local lua environment, both parameters are strings. Good for communication between plugins. |
| `AddEventHandler("eventName", Function)` | Adds the 2nd parameter to the table to be called when `eventName` is received (either locally or from the server), `Function` will get 1 parameter, a string containing the event data. |
# Code snippets
For example to parse the chat use the included `ChatMessageIncluded` event as such:
```lua
local function chatReceived(msg) -- Receive event with parameters
print("chat received: "..msg)
local i = string.find(s, ":") -- Find where our first ':' is, used to separate the sender and message
if i == nil then
print("error parsing message: separator could not be found!")
return -- Could not find separator, cancel function
end
print("index of separator: "..tostring(i))
local sender = string.sub(msg, 1, i-1) -- Substring our input to separate its 2 parts
local message = string.sub(msg, i+1, -1) -- Do whatever you want to with the message
print("sender: " .. sender)
print("message: ".. message)
end
AddEventHandler("ChatMessageReceived", chatReceived) -- Add our event handler to the list managed by BeamMP
```
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,335 @@
::: warning "This site is under construction!"
This site is being actively worked on.
Feel you could help? Please do by clicking on the page with a pencil on the right!
This can be done any page too.
# Server Scripting Reference
## Server Version 2.X
::: warning
BeamMP server version 2.X is now unsupported. This documentation is provided as a reference only.
Please update to the latest version for maintenance and support.
---
> This is 2.x scripting. Only refer to this if your server is old/outdated (version 2.x.x). For the latest documentation see [here](https://docs.beammp.com/scripting/server/latest-server-reference).
{.is-warning}
### Notes
To get the output of a function in the server console you have to wrap it in a `print()` statement.
For example:
`print(GetPlayerName(0))` will return the name of your server's first player.
`<PlayersServerID>` starts at 0.
### List of available functions for scripting
#### GetPlayerName(playersServerID)
Returns the player's discord name as a string
```lua
function onPlayerJoin(playerID)
local name = GetPlayerName(playerID)
-- Do something
end
```
#### GetPlayerDiscordID(playersServerID)
Returns the player's discord name as a string
```lua
function onPlayerJoin(playerID)
local name = GetPlayerDiscordID(playerID)
-- Do something
end
```
#### GetPlayerHWID(playersServerID)
Returns the player's discord ID as a string
```lua
function onPlayerJoin(playerID)
local name = GetPlayerHWID(playerID)
-- Do something
end
```
#### GetPlayerVehicles(playersServerID)
Returns the player's vehicles as an object/array
```lua
function onChatMessage(playerID, senderName, message)
local vehicleList = GetPlayerVehicles(playerID)
for vehicleID, vehicleData in pairs(vehicleList) do
-- Do something
-- Could also be used to check how many vehicles a player have
end
end
```
#### DropPlayer(playersServerID)
Drops the connection for a specific player. Essentially Kicking them
```lua
function onVehicleSpawn(playerID, vehicleID, vehicleData)
-- Do something
DropPlayer(playerID)
end
```
#### SendChatMessage(playersServerID, message)
Sends a message over the network to the specified user. Use -1 for everyone
```lua
function onPlayerJoin(playerID)
SendChatMessage(-1, "Someone just joined!")
end
```
#### CancelEvent() -- DEPRECIATED
Cancels the event from happening. This might be going soon. Use `return 1` to cancel the event.
#### onInit()
If declared in a lua file, it will be called once C++ successfully finished loading the current lua file
```lua
function onInit()
print("Server ready")
end
```
#### exit()
Will close the server
```lua
function onInit()
print("Server Ready. But who needs a server which is running")
exit() -- Stops the server
end
```
#### CreateThread(functionName, callInterval)
Will execute the function on a dedicated thread and it will run callInterval times a second.
1 = It will run every second.
```lua
function yourFunction()
for i = 1,10 do
SendChatMessage(-1, "Countdown: "..i)
Sleep(1000)
end
end
CreateThread("yourFunction", 30)
```
DEPRECIATED EXAMPLE
Will execute the function on a dedicated thread
```lua
function yourFunction()
for i = 1,10 do
SendChatMessage(-1, "Countdown: "..i)
Sleep(1000)
end
end
CreateThread("yourFunction", 30)
```
#### StopThread(functionName)
Will stop trying to call the thread function of the current script
```lua
function yourFunction()
delayExpired = false
Sleep(10000)
delayExpired = true
end
CreateThread("yourFunction", 30)
-- Do something
if not delayExpired then
StopThread("yourFunction")
else
-- Do something
end
```
#### Sleep(millisecs) - DEPRECIATED
Will pause execution for the amount of time specified (warning doing so will pause the entire server if you didn't create a thread)
```lua
function countdown()
for i = 1,10 do
SendChatMessage(-1, "Countdown: "..i)
Sleep(1000)
end
end
CreateThread("countdown", 10)
```
#### GetPlayerCount()
Will return how many players are connected
```lua
function onPlayerJoin(playerID)
SendChatMessage(playerID, "You are the "..GetPlayerCount().."th player!"
end
```
#### RemoveVehicle(playerServerID, VehicleID)
Will despawn a vehicle
```lua
function onVehicleSpawn(playerID, vehicleID, vehicleData)
if --[[ Vehicle data equal something it shouldn't be ]] then
RemoveVehicle(playerID, vehicleIID)
end
end
```
#### GetPlayers()
Will return a table of IDs with Names
```lua
local function onPlayerJoin(joinedPlayerID)
local players = GetPlayers()
for playerID, playerName in pairs(players) do
if playerID == joinedPlayerID then
-- Do something
end
end
end
```
#### RegisterEvent(eventName, functionName)
Will register that function to the event specified. Both must be strings
```lua
function anyEvent()
-- Do something
end
RegisterEvent("onAnyEventHappen", "anyEvent")
-- Do something
TriggerLocalEvent("onAnyEventHappen")
```
#### TriggerLocalEvent(eventName)
Will call every registered function in the same plugin folder.
```lua
function anyEvent()
-- Do something
end
RegisterEvent("onAnyEventHappen", "anyEvent")
-- Do something
TriggerLocalEvent("onAnyEventHappen")
```
#### TriggerGlobalEvent(eventName)
Will call every registered function with this event name.
```lua
-- File A
function anyEvent()
-- Do something
end
RegisterEvent("onAnyEventHappen", "anyEvent")
```
```lua
-- File B
TriggerGlobalEvent("onAnyEventHappen")
```
#### TriggerClientEvent(playerServerID, eventName, data)
Will call that event with the given data on the specified client (-1 for broadcast)
```lua
function onPlayerJoin(playerServerID)
TriggerClientEvent(playerServerID, "anyEvent", "You just joined the server")
end
RegisterEvent("onAnyEventHappen", "anyEvent")
```
#### Set(configID, newValue)
will set a config setting to the new specified value table below|
```lua
function onChatMessage(playerID, senderName, message)
if playerID == adminPlayer then
if message == --[[ anything ]] then
Set(3, 10)
end
end
end
```
#### List of available config settings for the `Set()` command
> Note that these will not save to the config file.
|Config ID|Name|Will only accept|
|---|---|---|
|`0`|Debug setting|true or false|
|`1`|Private setting|true or false|
|`2`|Max car per player|number|
|`3`|Max players|number|
|`4`|Map|string|
|`5`|Name|string|
|`6`|Description|string|
|any other ID will result in a console warning|
### List of available events for scripting
#### Default Events
Example of how to use an event:
```lua
function onInit()
RegisterEvent("onPlayerJoin", "onPlayerJoin")
end
function onPlayerJoin(playerServerID)
-- Do something
end
```
If you dont want guests on your server:
```lua
function onInit()
print("noGuests Ready")
RegisterEvent("onPlayerAuth","onPlayerAuth")
end
function onPlayerAuth(name, role, isGuest)
if isGuest then
return "You must be signed in to join this server!"
end
end
```
|Event|Parameters|Description|
|---|---|---|
|`onPlayerAuth`|The player's name, forum role, guest account (bool)|A player has authenticated and is requesting to join|
|`onPlayerConnecting`|The player's ID|A player is loading in (Before loading the map)|
|`onPlayerJoining`|The player's ID|A player is loading the map and will be joined soon|
|`onPlayerJoin`|The player's ID|A player has joined and loaded in|
|`onPlayerDisconnect`|The player's ID|A player has disconnected|
|`onChatMessage`|The sender's ID, name, and the chat message|A chat message was sent. This would be good for making a commands system|
|`onVehicleSpawn`|The player's ID, the vehicle ID, and the vehicle data|This is called when someone spawns a vehicle|
|`onVehicleEdited`|The player's ID, the vehicle ID, and the vehicle data|This is called when someone edits a vehicle, or replaces their existing one|
|`onVehicleDeleted`|The player's ID and the vehicle ID|This is called when someone deletes a vehicle they own|
#### Custom Events
Custom events can also be created for your own use. This is done very much the same to how the default ones are done.
Example of how to use a custom event:
```lua
function onInit()
RegisterEvent("myCustomEvent", "myCustomEvent")
end
function myCustomEvent(playerServerID, customData)
-- Do something
end
```
This can then be called either from client side or serverside using the respective functions.
### Players
When a player connects to your server, they are assigned a serverID starting from 0 and counting upwards. serverIDs are reused; if a player leaves and re-joins they will not be assigned a new serverID, they will simply get another available one. When the server restarts, serverIDs will be reset.
#### Static Identifiers
Players in BeamMP have 3 static identifiers which can be obtained from their serverID being their name, discordID, and their hardwareID or HWID. (though the latter of the aforementioned isn't implemented, we will act as if it is) Each of the three ID types has their own origins and strength's/weaknesses to using them for player identification.
| ID TYPE | PROS | CONS | FUNCTION TO OBTAIN |
|-----------|---------------------------------|----------------|:--------------------:|
| name | easy to obtain, straightforward | not secure | GetPlayerName() |
| discordID | fairly secure | inconvenient | GetPlayerDiscordID() |
| HWID | extremely secure | hard to obtain | GetPlayerHWID() |
### Vehicles
Vehicles in beamMP have 3 attributes that the server pays attention to, the owner's serverID, the vehicles vehicleID and it's data. The Owner's serverID is straight forward, it is the serverID, every vehicle also has an ID, vehicle IDs are not unique to the vehicle; two vehicles may have the same ID, assuming they're from different owners. Unlike serverIDs, vehicleIDs are reused, for example, if I have 4 vehicles, their IDs are 0, 1, 2 and, 3 if I delete the vehicle in vehicleID 2, I will have 0, 1 and, 3, when I spawn a new vehicle, the new vehicle will slot into ID 2. Lastly, the last attribute vehicles have is data, data contains a vehicle, name, parts, and other data; as the name implies. data is stored as a raw JSON string, so you will need a JSON library alternatively, you can manually step through the string and dig out the information you need.
@@ -0,0 +1,77 @@
# Getting started
In order to get started with development for BeamMP you will need at least:
- BeamNG.drive, installed locally
- BeamMP, installed locally; at least the launcher, additionally also the server
- Git, installed locally, and a GitHub.com account
- A code editor, for example VSCode or notepad++
---
# Difference between mod, launcher and server
BeamMP is split into three main parts:
- The mod is loaded by BeamNG, like any other vehicle or UI mod for the game. Its main function is to establish a local connection with the launcher and to display the multiplayer UI elements. It's mostly written in Lua, with some JavaScript, HTML, and CSS for the UI elements. Its repo is [https://github.com/BeamMP/BeamMP](https://github.com/BeamMP/BeamMP)
- The launcher's main function is to establish a constant connection to the mod, and once necessary, establish a connection to the chosen server, as well as handling user login with the BeamMP backend. It's written in C++, is precompiled by BeamMP and can be found at [https://github.com/BeamMP/BeamMP-Launcher](https://github.com/BeamMP/BeamMP-Launcher)
- The server establishes connections between one or many launchers, as well as "heartbeating" to the BeamMP backend, providing information such as IP, port, version, number of players, etc. Additionally, it manages and runs server-side Lua plugins. It's written in C++, precompiled by BeamMP for a few different OS and CPU architectures, and can be found at [https://github.com/BeamMP/BeamMP-Server](https://github.com/BeamMP/BeamMP-Server)
---
# Setting up a development environment to work on the mod
## Using an unpacked folder for BeamNG
In order to efficiently work on mods in BeamNG, it is advised to use an `unpacked` folder, rather than packaging zips after every change.
Open up the BeamNG userfolder by navigating to `%appdata%/Local/BeamNG.drive/0.xx/mods` where `xx` is the most recent BeamNG version.
Create a folder called `unpacked` inside the `mods` folder.
Further information about the userfolder can be found at [https://documentation.beamng.com/support/userfolder/](https://documentation.beamng.com/support/userfolder/)
## Enabling dev mode in the BeamMP launcher
In order to prevent auto-update deleting your local git clone, it's necessary to disable it, using `--no-download`.
If you also don't want the launcher to start BeamNG, and would like to see debug prints, then using `--dev` is advised.
| Argument | Note |
|:--------------------------------------|:-------------------------------------------|
| `--help` or `-h` | Will print the following list of arguments |
| `--port <port>` or `-p` | Change the default listen port to `<port>`. This must be configured ingame too |
| `--verbose` or `-v` | Verbose mode, prints debug messages |
| `--no-download` | Skip downloading and installing the BeamMP Lua mod |
| `--no-update` | Skip applying launcher updates (you must update manually) |
| `--no-launch` | Skip launching the game (you must launch the game manually) |
| `--dev` | Developer mode, same as --verbose --no-download --no-launch --no-update |
| `--game <args...>` or `-- <args...>` | Passes arguments to the game |
## Cloning the BeamMP repo into the unpacked folder
While you can manually copy the BeamMP mod files from our github repo, it is highly recommended to use a source-control system like git.
First create a fork of [https://github.com/BeamMP/BeamMP](https://github.com/BeamMP/BeamMP)
Most efficient would be to clone the repo directly into the `unpacked` folder.
For `git`, run `git clone https://github.com/yourName/BeamMP` from a PowerShell or CMD window started from the `unpacked` folder.
While in the userfolder, make sure theres no `multiplayer` folder left in `mods` and that now there's `unpacked/beammp`.
Now give the dev mode a try. Start the BeamMP launcher, start BeamNG manually, once ingame make sure that BeamMP is the only active mod.
You should be able to use BeamMP as usual.
Using a code editor, you can now add or change code directly in the `unpacked` folder.
You can then try the changes by reloading Lua ingame by pressing `Ctrl+L` (and `F5` if you made UI changes).
Once you're happy with your changes, you can commit them through git. See [the Git-SCM website](https://git-scm.com/doc) for tutorials and documentation on how to use Git. As soon as your changes are committed and pushed (to your fork), you can make a pull-request.
Feel free to ask in the #scripting channel in our [Discord](https://discord.gg/beammp) if you encounter any issues.
---
# Setting up a local server
While working on BeamMP, it can be beneficial to use a local server. You can follow the general [server installation](../../server/create-a-server.md) while omitting the first two steps for purely local connections.
Set the server to private in the `serverConfig.toml` while using any string as the `AuthKey`.
---
# Contribution Guidelines
For details on code format, commit message format, general development best practices, etc. please see the `CONTRIBUTING.md` file in each repo. This file contains more detailed information on how to contribute. The `README.md` in each repo usually contains build steps as well (for compiled projects).
+18
View File
@@ -0,0 +1,18 @@
---
title: Guides
description: This set of pages provides some basic guides for BeamMP
status: new
---
::: warning "This site is under construction!"
This site is being actively worked on.
Feel you could help? Please do by clicking on the page with a pencil on the right!
This can be done any page too.
# BeamMP Development Guides
This page will be the introduction and preface for both client and server content creation.
This page needs developing still.
@@ -0,0 +1,112 @@
# Multiplayer mod creation
## Folder structure and file basics
The basic folder and file structure needs to look like this:
```
Resources/
├─ Client/
│ └─ examplePlugin.zip/
│ ├─ scripts/
│ │ └─ modScript.lua
│ └─ lua/
│ └─ ge/
│ └─ extensions/
│ └─ examplePlugin.lua
└─ Server/
└─ examplePlugin/
├─ examplePlugin.lua
└─ further_lua/
└─ further.lua
```
The serverside lua is the bare minimum, if you want to add custom events, you also need at least a clientside lua as well as a modscript.lua
The Server folder must contain subfolders, one for each server-side mod.
It is good practice to only have a single main lua file and add further lua files into subfolders.
However, you are not required to do that, the server will load lua files in alphabetical order should there be multiple.
The Client folder contains the zip files that are sent to a client, which then will load them as a mod.
Any other files in the Client folder will cause an error on server startup, but apart from that will be ignored by the server.
The modScript.lua will be read by BeamNG and instructs the game which plugin to load.
:::example ""
[Download the examplePlugin.zip](../../../../assets/content/ResourcesForExamplePlugin.zip)
:::
## Serverside lua
There's more examples in the examplePlugin, but heres a very basic one, printing a players identifiers:
```lua
function onInit() --runs when plugin is loaded
MP.RegisterEvent("onPlayerAuth", "onPlayerAuth") --Provided by BeamMP
print("examplePlugin loaded")
end
--A player has authenticated and is requesting to join
--The player's name (string), forum role (string), guest account (bool), identifiers (table -> ip, beammp)
function onPlayerAuth(player_name, role, isGuest, identifiers)
local ip = identifiers.ip
local beammp = identifiers.beammp or "N/A"
print("onPlayerAuth: player_name: " .. player_name .. " | role: " .. role .. " | isGuest: " .. tostring(isGuest) .. " | identifiers: ip: " .. ip .. " - beammp: " .. beammp)
end
```
`onPlayerAuth` gets triggered as soon as a player wants to join, also see [onPlayerAuth in the scripting reference](../../../scripting/server/latest-server-reference/#onplayerauth)
Another example using onPlayerAuth, but this will deny guests from joining the server by sending the client a message back, which will then be shown to the player:
```lua
function onPlayerAuth(playerName, playerRole, isGuest, identifiers)
if isGuest then
return "No guests allowed, please use a BeamMP account"
end
end
```
Further info on serverside functions provided by BeamMP can be found in the [latest server reference](../../../scripting/server/latest-server-reference.md)
## Clientside lua
This largely follows the [BeamNG extensions](https://documentation.beamng.com/modding/programming/extensions/)
```lua
local M = {}
if extensions.isExtensionLoaded("examplePlugin") then
log("E", "examplePlugin", "examplePlugin loaded on client side")
return
end
return M
```
Prints to the console that the examplePlugin was loaded
Refer to the [beamNG documentation on debug prints](https://documentation.beamng.com/modding/programming/debugging/#a-add-a-log) to learn more
## modScript.lua
Usually contains only two lines
```lua
load('examplePlugin')
setExtensionUnloadMode('examplePlugin', 'manual')
```
You can add a log print if you want to see in the logs when your modScript gets processed by BeamNG
```lua
load('examplePlugin')
setExtensionUnloadMode('examplePlugin', 'manual')
log('I', 'modScript', "examplePlugin loaded")
```