From 5736699cd1263c32c53f941c2441ba45e7f42c1c Mon Sep 17 00:00:00 2001 From: "alex.ita" Date: Sun, 14 Jul 2024 18:14:03 +0200 Subject: [PATCH] Update mod-reference.md Migrated `https://wiki.beammp.com/en/Scripting/client_scripting` from the wiki. --- docs/en/scripting/mod-reference.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/en/scripting/mod-reference.md b/docs/en/scripting/mod-reference.md index 2ba9a90e..ad420ad7 100644 --- a/docs/en/scripting/mod-reference.md +++ b/docs/en/scripting/mod-reference.md @@ -7,3 +7,37 @@ 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 +``` \ No newline at end of file