Rechadder

Lua API Information


To start creating scripts. Create a folder called 'plugins' in the same directory as your Rechadder executable. All .lua files in that folder will automatically be loaded.


You can reload all scripts without having to restart Rechadder by pressing F1.

Events


events.on_client

events.on_client(function()
    print("I will run when a client starts!")
end)

Called when a client is loaded and connects to a server. Expects a function with no parameters as the first parameter.


events.on_server

events.on_server(function()
    print("I will run when a server starts!")
end)

Called when a server is loaded. Expects a function with no parameters as the first parameter.


events.on_start

events.on_start(function()
    print("I will run when Rechadder finishes loading!")
end)

Called when either a client or a server finishes loading, this does not require the client to be connected to a server.


events.on_message

events.on_message(function(ctx)
    print("Username: " .. ctx.username .. " Message: " .. ctx.message)
    return true 
    -- Returning true cancels the default handler.
    -- When server: the message won't be broadcasted. When client: the message won't display on the screen.
end)

Expects the function callback to have a parameter. Passes a table containing elements 'username', and 'message'.
Server: Called when a client sends a message, return true to stop the message from being broadcasted to all other clients.
Client: Called when you recieve a message from the server, return true to stop the message from being printed.

Client


client.send_message

client.send_message("Hello!")

Sends a normal message to the server. Parameter is a string, which is the message content. You cannot choose your username.


client.on_compose

-- Script that appends text to any message you send
client.on_compose(function(ctx)
    client.send_message(ctx.message .. " appended text")
    return true -- cancel default handler (sending to server)
end)

Called when you finish composing a message. Return true to cancel sending to server.

Server


server.broadcast

server.broadcast("Make sure to read the rules!")

Prints the first argument to all connected clients screen.


server.send

-- Displays a MOTD to any connecting client.
server.on_connection(function(ctx)
    server.send(ctx.username, "Welcome to the server!")
end)

Prints the second argument to the client whose username matches the first argument. Please note that when sending messages to a client as soon as they connect might have undefined behaviour as the client might not have acknowledged the connection handshake. I personally recommend yielding for a few hundred milliseconds.


server.on_connection

-- Displays a MOTD to any connecting client.
server.on_connection(function(ctx)
    server.send(ctx.username, "Welcome to the server!")
end)

Calls the first argument with a context parameter with a table containing the element: 'username'


server.connections

-- When a client types '@online': send them the names of all connected clients
events.on_server(function()
    events.on_message(function(ctx)
        if ctx.message == "@online" then
            for index, name in ipairs(server.connections()) do
                server.send(ctx.username, "> " .. name)
            end
            return true -- cancel handler
        end
    end)
end)

Returns a table containing the usernames of all connected clients.

Util


util.create_thread

-- Prints 'hello' and 'goodbye' at the same time.
events.on_server(function()
    util.create_thread(function()
        while true do
            print("hello")
            util.yield(0)
        end
    end)
end)
while true do
    print("goodbye")
end

Creates a parallel execution state. This is however, not asynchronous. You will need to call util.yield to return execution back to the script thread handler. If you're familiar with the term, this creates a fiber.


util.yield

-- Reminds users of the rules every ten seconds
events.on_server(function()
    util.create_thread(function()
        while true do
            server.broadcast("Remember the rules: No swearing!")
            util.yield(10000)
        end
    end)
end)

This sleeps a thread for a variable amount of time. The time (in milliseconds) are provided by the first argument. If you have a while true loop, you need to util.yield(0) as this returns execution to the script handler.