Skip to content

Functions

Stefan A. Brannfjell edited this page Mar 11, 2020 · 12 revisions

Game

Available methods:

createItem(itemId, count[, position])
createContainer(itemId, size[, position])
createMonster(monsterName, position, extended = false, force = false)
createNpc(npcName, position, extended = false, force = false)
createTile(x, y, z[, isDynamic = false])
createTile(position[, isDynamic = false])
getExperienceStage(level)
getGameState()
getHouses()
getMonsterCount()
getMonsterTypes()
getNpcCount()
getPlayerCount()
getPlayers()
getReturnMessage(value)
getSpectators(position, multifloor = false, onlyPlayer = false, minRangeX = 0, maxRangeX = 0, minRangeY = 0, maxRangeY = 0)
getTowns()
getWorldType()
loadMap(path)
setGameState(state)
setWorldType(type)
startRaid(raidName)

os

Available methods:

mtime()

table

Available methods:

create(arrayLength, keyLength)


Game.createItem(itemId, count[, position])

Description: Creates an item.
Parameters:

  • itemId - Id of the item to be created
  • count - How many are we creating?
  • position - Where do we place it? (optional)
Returns: The item created. (userdata)
Example:
-- Create an item somewhere on the map.
local item = Game.createItem(2400, 1, Position(100, 100, 7))
--
-- Create a temporary item that is given to a player (if it isn't moved from creation it will be deleted)
local player = Player(...)
player:addItemEx(Game.createItem(2400, 1))

[To the top] Added in version: 1.0


Game.createContainer(itemId, size[, position])

Description: Creates a container with given size.
Parameters:

  • itemId - Id of the item to be created
  • size - Size of the container
  • position - Where do we place it? (optional)
Returns: The container created. (userdata)
Example:
-- Create an amulet of loss as a container with size 5 at position: x 100, y 100, z 7.
local item = Game.createContainer(ITEM_AMULETOFLOSS, 5, Position(100, 100, 7))
--
-- Create a temporary container that is given to a player (if it isn't moved from creation it will be deleted)
local player = Player(...)
player:addItemEx(Game.createContainer(ITEM_AMULETOFLOSS, 5))

[To the top] Added in version: 1.1


Game.createMonster(monsterName, position[, extended = false[, force = false]])

Description: Creates a monster.
Parameters:

  • monsterName - Name of the monster to be created
  • position - Where do we place it?
  • extended - Extend the range? (optional, default: false)
  • force - Will it be created even if it cannot stand at the position? (optional, default: false)
Returns: The monster created. (userdata)
Example:
-- This should always create this monster.
local monster = Game.createMonster("demon", Position(100, 100, 7), false, true)
if not monster then
	-- Something went wrong?
end

[To the top] Added in version: 1.0


Game.createNpc(npcName, position[, extended = false[, force = false]])

Description: Creates a npc.
Parameters:

  • npcName - Name of the npc to be created
  • position - Where do we place it?
  • extended - Extend the range? (optional, default: false)
  • force - Will it be created even if it cannot stand at the position? (optional, default: false)
Returns: The npc created. (userdata)
Example:
-- This may possibly not create the npc.
local npc = Game.createMonster("some npc", Position(100, 100, 7), false, false)
if not npc then
	-- Something is probably blocking that position and around it aswell
end

[To the top] Added in version: 1.0


Game.createTile(x, y, z[, isDynamic = false])

Description: Creates a tile if it can.
Parameters:

  • x - X coordinate
  • y - Y coordinate
  • z - Z coordinate
  • isDynamic - Is this tile dynamic? (optional, default: false)
Returns: The tile created or the previous existing one. (userdata)
Example:
local tile = Game.createTile(100, 100, 7)
local ground = tile:getGround()
if ground then
	print(ground:getName())
end

[To the top] Added in version: 1.0


Game.createTile(position[, isDynamic = false])

Description: Creates a tile if it can.
Parameters:

  • position - Location of the tile.
  • isDynamic - Is this tile dynamic? (optional, default: false)
Returns: The tile created or the previous existing one. (userdata)
Example:
local tile = Game.createTile(Position(100, 100, 7))
local ground = tile:getGround()
if ground then
	print(ground:getName())
end

[To the top] Added in version: 1.0


Game.getExperienceStage(level)

Description: Find the experience rate related to a certain level.
Parameters:

  • level - Level to get the stage with.
Returns: The experience rate related to the specified level.
Example:
-- Print the experience rate for level 8 to the console
print(Game.getExperienceStage(8))

[To the top] Added in version: 1.0


Game.getGameState()

Description: Gets the current gamestate.
Parameters: None
Returns: Current gamestate on the server.
Example:

print(Game.getGameState())

[To the top] Added in version: 1.0


Game.getHouses()

Description: Get all houses.
Parameters: None
Returns: A table containing all houses (userdata).
Example:

-- This prints the names of all houses that exist on the server
local houses = Game.getHouses()
for i = 1, #houses do
	print(houses[i]:getName())
end

[To the top] Added in version: 1.0


Game.getMonsterCount()

Description: Find the total amount of monsters on the server.
Parameters: None
Returns: Total amount of monsters on the server.
Example:

print(Game.getMonsterCount())

[To the top] Added in version: 1.0


Game.getMonsterTypes()

Description: List all loaded monster types on the server.
Parameters: None
Returns: List of monster types in the server.
Example:

for name, monstertype in pairs(Game.getMonsterTypes()) do
	print(" - Name: " .. name 
		.. " - Health: " .. monstertype:getMaxHealth()
		.. " - Armor: " .. monstertype:getArmor()
	)
end

[To the top] Added in version: 1.3


Game.getNpcCount()

Description: Find the total amount of npcs on the server.
Parameters: None
Returns: Total amount of npcs on the server.
Example:

print(Game.getNpcCount())

[To the top] Added in version: 1.0


Game.getPlayerCount()

Description: Find the total amount of players on the server.
Parameters: None
Returns: Total amount of players on the server.
Example:

print(Game.getPlayerCount())

[To the top] Added in version: 1.0


Game.getPlayers()

Description: Get all connected players.
Parameters: None
Returns: A table containing all connected players (userdata).
Example:

-- This prints the names of all players connected to the server
local players = Game.getPlayers()
for i = 1, #players do
	print(players[i]:getName())
end

[To the top] Added in version: 1.0


Game.getReturnMessage(value)

Description: Gets a message associated with the value.
Parameters:

  • value - Internal return value
Returns: true
Example:
-- This makes no sense but its an example.
local player = Player(...)
player:say(Game.getReturnMessage(RETURNVALUE_YOUARENOTTHEOWNER), TALKTYPE_SAY)

[To the top] Added in version: 1.0


Game.getSpectators(position[, multifloor = false[, onlyPlayer = false[, minRangeX = 0[, maxRangeX = 0[, minRangeY = 0[, maxRangeY = 0]]]]]])

Description: Get all creatures in the area around center position.
Parameters:

  • position - Center position
  • multifloor - Search multiple floors? (optional, default: false)
  • onlyPlayer - Find only players? (optional, default: false)
  • minRangeX - Maximum range to the west in x axis (optional, default: 0)
  • maxRangeX - Maximum range to the east in x axis (optional, default: 0)
  • minRangeY - Maximum range to the north on the y axis (optional, default: 0)
  • maxRangeY - Maximum range to the south on the y axis (optional, default: 0)
Returns: A table containing creatures found (userdata).
Example:
-- This finds all players in a 5x5 area around (100, 100, 7)
-- (98-102, 98-102, 7)
local spectators = Game.getSpectators(Position(100, 100, 7), false, true, 2, 2, 2, 2)
for i = 1, #spectators do
	local spectator = spectators[i]
	spectator:say(spectator:getName(), TALKTYPE_SAY)
end

[To the top] Added in version: 1.0


Game.getTowns()

Description: Get all towns.
Parameters: None
Returns: A table containing all towns (userdata).
Example:

-- This prints the names of all towns that exist on the server
local towns = Game.getTowns()
for i = 1, #towns do
	print(towns[i]:getName())
end

[To the top] Added in version: 1.0


Game.getWorldType()

Description: Gets the current world type.
Parameters: None
Returns: Current gamestate on the server.
Example:

print(Game.getWorldType())

[To the top] Added in version: 1.0


Game.loadMap(path)

Description: This loads a new map chunk.
Parameters:

  • path - File path to the map.
Returns: Nothing
Example:
Game.loadMap("data/map/some_other_map.otbm")

[To the top] Added in version: 1.0


Game.setGameState(state)

Description: Sets the current gamestate.
Parameters:

  • state - The new gamestate
Returns: true
Example:
-- Shutdown the server
Game.setGameState(GAME_STATE_SHUTDOWN)

[To the top] Added in version: 1.0


Game.setWorldType(type)

Description: Sets the current world type.
Parameters:

  • type - The new world type
Returns: true
Example:
-- Change to hardcore pvp
Game.setWorldType(WORLD_TYPE_PVP_ENFORCED)

[To the top] Added in version: 1.0


Game.startRaid(raidName)

Description: Starts a raid if one with said name exist.
Parameters:

  • raidName - Name of the raid.
Returns: true if the raid started, nil otherwise.
Example:
if Game.startRaid("test") then
	-- Raid was started.
end

[To the top] Added in version: 1.0

Global

Available methods:

addEvent(callback, delay, ...)
stopEvent(eventid)


addEvent(callback, delay, ...)

Description: This function is used to run other functions at a later time.
Parameters:

  • callback - The function that should run at a later time.
  • delay - Time before the callback runs in milliseconds.
  • ... - This can expand into any amount of variables, they are sent to the callback when it runs. (optional)
Returns: The id associated with this event.
Example:
-- Creates an event that prints "Hello World!" to the console.
addEvent(print, 1000, "Hello World!")

[To the top] Added in version: 1.0


stopEvent(eventid)

Description: This function is used to stop functions that should run later.
Parameters:

  • eventid - The event id to stop.
Returns: true if an event was stopped, false otherwise.
Example:
-- Creates an event that should print "Hello World!" but it's stopped before that can happen.
local event = addEvent(print, 1000, "Hello World!")
stopEvent(event)

[To the top] Added in version: 1.0


os.mtime()

Description: Returns the Unix time (epoch) in milliseconds
Parameters: None
Returns: The Unix time (epoch) in milliseconds
Example:

print(os.mtime() .. " milliseconds have passed since the Unix epoch")

[To the top] Added in version: 1.0


table.create(arrayLength, keyLength)

Description: Creates a new table with specified length.
Parameters:

  • arrayLength - Ordered indexes (1, 2, ...arrayLength)
  • keyLength - Unordered indexes (anything that does not follow the above)
Returns: The created table.
Example:
local t = table.create(5, 0)
for i = 1, #t do
	t[i] = i + 1
end

[To the top] Added in version: 1.0

Clone this wiki locally