Przejdź do treści
Gothic MultiplayerMultiplayer
Gothic MP Wiki

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)
ParameterTypeDescription
eventgmp.EventNameName of the event.
handlerfunctionFunction 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
end

Returns: integer - Number of player slots.

isPlayerConnected

gmp.isPlayerConnected(pid)

Tells whether a player is connected on the given slot.

ParameterTypeDescription
pidintegerPlayer 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.

ParameterTypeDescription
pidintegerPlayer id.
reason (optional)stringReason 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.

ParameterTypeDescription
pidintegerPlayer id.
textstringMessage 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.

ParameterTypeDescription
textstringMessage 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)
ParameterTypeDescription
namestringCommand name without the slash, e.g. "ore" for /ore.
handlerfunctionCalled 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
ParameterTypeDescription
handlerfunctionFunction to call (no arguments).
intervalMsintegerTime in milliseconds.
repeating (optional)booleantrue = 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.

ParameterTypeDescription
timerIdintegerId 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.

ValueDescription
"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.