Skip to content

LuaAPI Library

itvnnnd87 edited this page Oct 24, 2026 · 2 revisions

Library

System\Library.lua adds 48 ready-made functions written in Lua on top of the GameServer functions (with the 9 Timer functions of System\Timers.lua: 57). They are loaded before your modules, so every module can use them.

Helpers

Function Description
Helpers.MakeItemId(group, index) Item code = group * 512 + index.
Helpers.GetItemType(itemCode) Item group.
Helpers.GetItemIndex(itemCode) Index inside the group.
Helpers.RGB(r, g, b) Colour value.
Item.RandomLevel(min, max) Random item level.

Coins by type

One set of functions for every currency. coinType: COIN_ZEN (0), COIN_WCOIN (1), COIN_GOBLIN_POINT (2), COIN_RUUD (3).

Function Description
Coin.GetByType(aIndex, coinType) Amount the character has.
Coin.Check(aIndex, coinType, amount) true if the character has at least amount.
Coin.Charge(aIndex, coinType, amount) Takes amount if there is enough. true / false.
Coin.Give(aIndex, coinType, amount) Gives amount. true / false.
Coin.GetName(coinType) "Zen", "WCoin", "Goblin Point", "Ruud".
Coin.IsValidType(coinType)
local PRICE_TYPE, PRICE = COIN_WCOIN, 50

if Coin.Charge(aIndex, PRICE_TYPE, PRICE) then
    Item.GiveBag(aIndex, "LuckyBox")
else
    Message.Send(aIndex, 3, "You need " .. PRICE .. " " .. Coin.GetName(PRICE_TYPE))
end

ActionTick

3 slots (0, 1, 2) per character to store a time and a short name, for example cooldowns. Cleared when the character leaves the game.

Function Description
ActionTick.Set(aIndex, slot, tick, name) Stores a value (name up to 15 characters).
ActionTick.Get(aIndex, slot) Stored value, 0 if none.
ActionTick.GetName(aIndex, slot) Stored name.
ActionTick.Clear(aIndex, slot)
-- /reward can be used once every 60 seconds
if Util.GetTick() - ActionTick.Get(aIndex, 0) < 60000 then
    Message.Send(aIndex, 3, "Please wait")
    return 1
end
ActionTick.Set(aIndex, 0, Util.GetTick(), "reward")

Scheduler

Starts and ends your own events from an XML schedule.

<EventScheduler>
    <Event Type="1" Name="Boss Hunt" NoticePeriod="300" Duration="1800">
        <Start Year="-1" Month="-1" Day="-1" DayOfWeek="-1" Hour="20" Minute="0" Second="0"/>
        <Start DayOfWeek="6" Hour="14" Minute="30"/>
    </Event>
</EventScheduler>
Attribute Meaning
Type Your event number, passed to the schedule events.
Name Display name (Scheduler.GetEventName).
NoticePeriod Seconds before the start to send onScheduleNotice. 0 = no notice.
Duration Seconds after the start to send onScheduleEnd. 0 = end it yourself with Scheduler.StopEvent.
Start One line per start time. Missing or -1 = any. DayOfWeek 0 = Sunday. Without Second, the schedule is checked once per minute.
Function Description
Scheduler.LoadFromXML(path) Loads the schedule (path like File.ReadXml). true / false.
Scheduler.GetEventCount() Number of events.
Scheduler.GetEventName(type)
Scheduler.IsEventActive(type) true between start and end.
Scheduler.GetElapsedTime(type) Seconds since the start, -1 if not active.
Scheduler.GetRemainingTime(type) Seconds until the end, -1 if not active or no duration.
Scheduler.GetProgress(type) 0..100, -1 if not active or no duration.
Scheduler.StopEvent(type) Ends the event now (sends onScheduleEnd).
Scheduler.HasSecondPrecisionEvents() true if a Start line uses Second (the schedule is then checked every second).
Scheduler.CheckEventNotices() Events whose notice time is now: { { type, secondsRemaining }, ... }. Used by the scheduler itself; call it only if you run your own loop.
Scheduler.CheckEventStarts() Events starting now (marks them active): { type, ... }. Same note.
Scheduler.CheckEventEnds() Active events whose Duration has passed (marks them ended): { type, ... }. Same note.

The scheduler runs by itself every second (System\Timers.lua). Receive its events with a module:

Scheduler.LoadFromXML("Includes\\BossHunt\\Schedule.xml")

function BossHunt_Notice(eventType, secondsLeft)
    Message.SendAll(0, Scheduler.GetEventName(eventType) .. " starts in " .. math.floor(secondsLeft / 60) .. " minutes")
end

function BossHunt_Start(eventType)
    EventMonsterTracker.SpawnWaveAndRegister(eventType, 275, 10, MAP_DEVIAS, 180, 80, 220, 120)
end

function BossHunt_End(eventType)
    EventMonsterTracker.CleanupEvent(eventType)
end

BridgeFunctionAttach("OnScheduleNotice", "BossHunt_Notice")
BridgeFunctionAttach("OnScheduleStart", "BossHunt_Start")
BridgeFunctionAttach("OnScheduleEnd", "BossHunt_End")

The scheduler state is cleared on Lua API reload: an event that was running is not ended by onScheduleEnd.

EventMonsterTracker

Remembers which monsters and NPCs belong to each event so they can be counted and removed together. filter: OBJECT_TYPE_MONSTER (2), OBJECT_TYPE_NPC (3), or nothing for both.

Function Description
EventMonsterTracker.SpawnAndRegister(eventType, class, map, x1, y1, x2, y2, element, dir) Spawns one monster at a random point of the area and tracks it. Returns its index or -1.
EventMonsterTracker.SpawnWaveAndRegister(eventType, class, count, map, x1, y1, x2, y2, element, dir) Spawns count monsters (up to 500). Returns the list of indexes.
EventMonsterTracker.Register(eventType, index, class, type) Tracks a monster you spawned yourself.
EventMonsterTracker.RegisterNPC(eventType, index, class)
EventMonsterTracker.Unregister(eventType, index) Stops tracking (does not remove it).
EventMonsterTracker.IsTracked(eventType, index)
EventMonsterTracker.GetObjectInfo(eventType, index) { class, type }.
EventMonsterTracker.GetMonsterClass(eventType, index)
EventMonsterTracker.GetMonsters(eventType, filter) List of indexes.
EventMonsterTracker.GetCount(eventType, filter)
EventMonsterTracker.CleanupEvent(eventType, filter) Removes the tracked objects that still exist. Returns how many.
EventMonsterTracker.CleanupMonsters(eventType), CleanupNPCs(eventType)
EventMonsterTracker.CleanupDead(eventType, filter) Stops tracking the ones that died or disappeared (removes nothing).
EventMonsterTracker.GetActiveEvents() Events that still have tracked objects.
EventMonsterTracker.ClearAll() Forgets everything (removes nothing).

Tracked monsters are removed automatically when the Lua API is reloaded or the server shuts down.

function BossHunt_MonsterDie(monsterIndex, killerIndex, class, map, x, y)
    if EventMonsterTracker.Unregister(1, monsterIndex) and EventMonsterTracker.GetCount(1) == 0 then
        Message.SendAll(0, "All bosses are dead!")
        Scheduler.StopEvent(1)
    end
end

BridgeFunctionAttach("OnMonsterDie", "BossHunt_MonsterDie")

OptionBonus

Several modules can give option table bonuses to the same character without overwriting each other. Each module sets only its own part; the parts are added together, written to the option table and the stats are recalculated.

Function Description
OptionBonus.Set(aIndex, source, values) Sets the bonuses of source (any name, usually the module name). values = { AddDamage = 100, MulMaxHP = 5 }; nil or {} removes them. Returns true if something changed (nothing is recalculated when the values are the same).
OptionBonus.Get(aIndex, source) Copy of the bonuses of source.

The bonuses are cleared on logout and on Lua API reload: set them again in onPlayerLogin.

function GuildBonus_OnLogin(aIndex)
    if Player.GetGuildNumber(aIndex) > 0 then
        OptionBonus.Set(aIndex, "GuildBonus", { AddDefense = 50 })
    end
end

BridgeFunctionAttach("OnCharacterEntry", "GuildBonus_OnLogin")

Use OptionBonus instead of Player.SetOptionTable whenever more than one module may give bonuses.

Player object

Player.GetObject(aIndex) gives field access to a character (p.Level, p.Money = 100 ...). See Player functions.


Next: Config files

Clone this wiki locally