Skip to content
Gothic MultiplayerMultiplayer
Gothic MP Wiki

Functions

Global functions of server-side scripts: events, player slots, chat, commands, custom events and timers.

since 0.1.0-alpha

Server-side scripts run on the game server (gmp_server). All functions are global - no require is needed.

Characters are identified by ids (pid): ids 0 to getMaxSlots() - 1 are players (one per player slot), ids from getMaxSlots() up are server-side NPCs created with 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

addEventHandler

since 0.1.0-alpha
addEventHandler(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 (onPlayerSpawn, onPlayerDead, onPlayerHit, onPlayerRespawn) are reported for players and for server-side NPCs; the id tells which one it is. A handler of onPlayerChat can return false to stop the message from being shown in the chat.

Custom events sent by clients (triggerServerEvent) use the same function; their handlers get the id of the sending player first, then the arguments.

addEventHandler("onPlayerConnect", function(pid)
  sendMessageToAll(getPlayerName(pid) .. " entered the Colony")
end)

addEventHandler("buyItem", function(pid, item, amount)   -- triggerServerEvent("buyItem", ...) on the client
  giveItem(pid, item, amount)
end)
ParameterTypeDescription
eventServerEventNameName of the event.
handlerfunctionFunction called when the event occurs; its arguments depend on the event (see Events).

Returns: boolean - Always true.

removeEventHandler

since 0.1.0-alpha
removeEventHandler(event, handler)

Removes a handler registered with addEventHandler (the same function value).

ParameterTypeDescription
eventServerEventNameName of the event.
handlerfunctionThe registered function.

Returns: boolean - false when this function was not registered for the event.

triggerClientEvent

since 0.1.0-alpha
triggerClientEvent(pid, name, ...)

Sends a custom event to one player's client scripts (addEventHandler(name, ...) on the client). Arguments may be nil, booleans, numbers, strings and tables of those.

triggerClientEvent(pid, "showScore", 10)
ParameterTypeDescription
pidintegerPlayer id.
namestringName of the event (1-64 characters).
...anyArguments.

Returns: boolean - false when no player is connected on this slot.

triggerClientEventToAll

since 0.1.0-alpha
triggerClientEventToAll(name, ...)

Sends a custom event to the client scripts of every connected player.

ParameterTypeDescription
namestringName of the event (1-64 characters).
...anyArguments.

Returns: boolean - Always true.

Players and ids

getMaxSlots

since 0.1.0-alpha
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, getMaxSlots() - 1 do
  if isPlayerConnected(pid) then sendMessage(pid, "Server restart in 5 minutes") end
end

Returns: integer - Number of player slots.

getPlayerCount

since 0.1.0-alpha
getPlayerCount()

Returns the number of connected players.

Returns: integer

isPlayerConnected

since 0.1.0-alpha
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

since 0.1.0-alpha
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

since 0.1.0-alpha
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

since 0.1.0-alpha
sendMessageToAll(text)

Sends a chat message to all connected players.

ParameterTypeDescription
textstringMessage text (UTF-8).

Commands

registerCommand

since 0.1.0-alpha
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.

registerCommand("ore", function(pid, args)
  local amount = tonumber(args) or 10
  giveItem(pid, "ItMi_Orenugget", amount)
  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

since 0.1.0-alpha
setTimer(handler, intervalMs, repeating?)

Calls a function after the given time, once or repeatedly.

local announcer = setTimer(function()
  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

since 0.1.0-alpha
killTimer(timerId)

Stops a timer started with setTimer.

ParameterTypeDescription
timerIdintegerId returned by setTimer.

Returns: boolean - false when no such timer is running.

Types

ServerEventName

since 0.1.0-alpha

Names of the server events accepted by addEventHandler. See Events for the handler arguments. Any other name is a custom event sent by a client with triggerServerEvent.

ValueDescription
"onPlayerConnect"A player connected to the server (got a slot).
"onPlayerDisconnect"A player disconnected from the server.
"onPlayerSpawn"A player's character entered the world, or a server-side NPC was created.
"onPlayerChat"A player sent a chat message.
"onPlayerDead"A player or a server-side NPC died.
"onPlayerHit"A player or a server-side NPC was hit.
"onPlayerRespawn"A player or a server-side NPC was revived.
stringA custom event from a client (triggerServerEvent).