gmp
The gmp module on the server side: character ids, events, player slots, chat, commands and timers.
Server-side scripts run on the game server and use the same entry point:
local gmp = require("gmp")Characters are identified by ids (pid): ids 0 to gmp.getMaxSlots() - 1 are players (one per player slot), ids from gmp.getMaxSlots() up are server-side NPCs created with gmp.createNpc. Functions with Player in the name (Players) work for both; functions with Npc in the name (NPCs) only for server-side NPCs. Common types and constants are shared with the client side.
Events
on
gmp.on(event, handler)Registers a handler for a server event. Several handlers can be registered for the same event;
they are called in the order of registration. Events about characters (playerDead, playerHit,
playerRespawn) are reported for players and for server-side NPCs; the id tells which one it is.
gmp.on("playerJoin", function(pid)
gmp.sendMessageToAll(gmp.getPlayerName(pid) .. " entered the Colony")
end)| Parameter | Type | Description |
|---|---|---|
event | gmp.EventName | Name of the event. |
handler | function | Function called when the event occurs; its arguments depend on the event (see Events). |
Players and ids
getMaxSlots
gmp.getMaxSlots()Returns the number of player slots of the server. Player ids are 0 to getMaxSlots() - 1;
ids from getMaxSlots() up belong to server-side NPCs.
for pid = 0, gmp.getMaxSlots() - 1 do
if gmp.isPlayerConnected(pid) then gmp.sendMessage(pid, "Server restart in 5 minutes") end
endReturns: integer - Number of player slots.
isPlayerConnected
gmp.isPlayerConnected(pid)Tells whether a player is connected on the given slot.
| Parameter | Type | Description |
|---|---|---|
pid | integer | Player id (0 to getMaxSlots() - 1). |
Returns: boolean - false for an empty slot and for NPC ids.
kickPlayer
gmp.kickPlayer(pid, reason?)Disconnects a player from the server.
| Parameter | Type | Description |
|---|---|---|
pid | integer | Player id. |
reason (optional) | string | Reason shown to the player. |
Returns: boolean - false when no player is connected on this slot.
Chat
sendMessage
gmp.sendMessage(pid, text)Sends a chat message to one player.
| Parameter | Type | Description |
|---|---|---|
pid | integer | Player id. |
text | string | Message text (UTF-8). |
Returns: boolean - false when no player is connected on this slot.
sendMessageToAll
gmp.sendMessageToAll(text)Sends a chat message to all connected players.
| Parameter | Type | Description |
|---|---|---|
text | string | Message text (UTF-8). |
Commands
registerCommand
gmp.registerCommand(name, handler)Registers a chat command. When a player types /name ... in the chat, the handler is called
with the player id and the rest of the line, and the line is not shown in the chat.
gmp.registerCommand("ore", function(pid, args)
local amount = tonumber(args) or 10
gmp.giveItem(pid, "ItMi_Orenugget", amount)
gmp.sendMessage(pid, "The Ore Barons are watching...")
end)| Parameter | Type | Description |
|---|---|---|
name | string | Command name without the slash, e.g. "ore" for /ore. |
handler | function | Called as handler(pid, args): pid = the player who typed the command, args = text after the command name ("" when none). |
Timers
setTimer
gmp.setTimer(handler, intervalMs, repeating?)Calls a function after the given time, once or repeatedly.
local announcer = gmp.setTimer(function()
gmp.sendMessageToAll("Visit the Old Camp market!")
end, 600000, true) -- every 10 minutes| Parameter | Type | Description |
|---|---|---|
handler | function | Function to call (no arguments). |
intervalMs | integer | Time in milliseconds. |
repeating (optional) | boolean | true = call it every intervalMs until killTimer. Default false (once). |
Returns: integer - Id of the timer, for killTimer.
killTimer
gmp.killTimer(timerId)Stops a timer started with setTimer.
| Parameter | Type | Description |
|---|---|---|
timerId | integer | Id returned by setTimer. |
Returns: boolean - false when no such timer is running.
Types
EventName
Names of the server-side events accepted by gmp.on. See Events for the handler arguments.
| Value | Description |
|---|---|
"playerJoin" | A player connected to the server. |
"playerLeave" | A player disconnected from the server. |
"playerChat" | A player sent a chat message. |
"playerDead" | A player or a server-side NPC died. |
"playerHit" | A player or a server-side NPC was hit. |
"playerRespawn" | A player or a server-side NPC was revived. |
