Code Samples
This page provides some common examples for the functionality available that can be used as templates when developing your project using the Lua runtime.
Match handler #
A match handler represents all server-side functions for handling game inputs and operations for authoritative multiplayer matches. See the Match Handler API and Match Runtime API reference pages to learn about the match handler functions.
This is an example of a Ping-Pong match handler. Messages received by the server are broadcast back to the peer who sent them.
| |
Context #
| |
Database handler #
This example creates a system ID - an ID that cannot be used from a client - and the custom SQL query inserting it in the users table:
| |
RPC #
The example below registers a function with the identifier custom_rpc_func_id. This ID can then be used within client code to send an RPC message to execute the function on the server and return the result.
| |
Before hook #
The code example below fetches the current user’s profile and checks the metadata, which is assumed to be JSON encoded with "{level: 12}" in it. If a user’s level is too low, an error is thrown to prevent the Friend Add message from being passed onwards in the server:
| |
After hook #
The example code below writes a record to a user’s storage when they add a friend. Any data returned by the function will be discarded.
| |
Example module #
As an example let’s use the Pokéapi and build a helpful module named “pokeapi.lua”.
| |
You can now make an RPC call for a Pokémon from a client:
