The idea
Instead of each module writing its own onPlayerLogin function (functions with the same name overwrite each other), a module writes its function under its own name and then attaches it to the event. The system in Data\LuaAPI\System\Modules.lua calls the attached functions one after another, each function running in its own pcall: if module A errors, it is logged and module B still runs.
function WelcomeMessage_OnLogin(aIndex)
Message.Send(aIndex, 1, "Hello " .. Player.GetName(aIndex))
end
BridgeFunctionAttach("OnCharacterEntry", "WelcomeMessage_OnLogin")
- Parameter 1: the event name (see the table below).
- Parameter 2: the name of the global function as a string (not the function itself). The function must be global (not
local). - Attaching the same function twice to one event: the second one is ignored (it does not run twice).
- Wrong event name: the log
[Modules] BridgeFunctionAttach: unknown event '...'is written and it is ignored. - The first event attached makes GameServer start sending that event to Lua. Events nobody listens to use no resources.
Names used when attaching
You can use the full event name (onItemGet), capitalize the first letter (OnItemGet), or use one of the short names:
| Short name | Event |
|---|---|
OnReadScript | onGameServerStart |
OnShutScript | onScriptUnload |
OnTimerThread | onTimerProcessSecond |
OnCharacterEntry | onPlayerLogin |
OnCharacterClose | onPlayerLogout |
OnUserDie | onPlayerDie |
OnSQLAsyncResult | onDBQueryResult |
OnUserRespawn | onPlayerRespawn |
OnUserItemPick | onItemPick |
OnUserItemDrop | onItemDrop |
OnUserItemMove | onItemMove |
OnCommandDone | onUseCommand |
OnUserGiveExp | onPlayerGiveExp |
OnUserTeleport | onMapMoveRequest |
OnUserBuyitem | onShopBuy |
OnUserSellitem | onShopSell |
OnUserItemUse | onItemUse |
OnUserEquipItem | onItemEquip |
OnUserUnEquipItem | onItemUnEquip |
In addition there are two special names:
OnCommandManager: receives chat commands declared inLuaCommands.xml(the function receivesaIndex, cmdtype, code, arg, andcmdtypeis always16).OnAdminCommandManager: this name exists inModules.luabutSystem\Callbacks.luadoes not call it, so currently no function is triggered. Do not attach modules to this name.
Parameter order of OnNpcTalk
When attaching with exactly the name OnNpcTalk, the function receives parameters in the order (npcIndex, aIndex) (NPC first, player second), the opposite of the event onNpcTalk(aIndex, npcIndex). The sample modules (CumulativeRecharge, RechargeGift, RandomQuestSystem) all follow this order. Returning 0 means the module has handled it so the default NPC dialog is not opened; return 1 or return nothing to let other modules / the default dialog proceed.
How the results of multiple functions are merged
Each event belongs to one of 4 kinds. The functions run in the order they were attached.
| Kind | Applies to | Rule |
|---|---|---|
all | Notification events (onGameServerStart, onScriptUnload, onTimerProcess*, onPlayerLogin, onPlayerLogout, onPlayerDie, onItemGet, onMonsterDie... ) | Runs every function, ignoring return values. |
veto | Blocking events (onNpcTalk, onItemUse, onShopBuy, onMapMoveRequest...) | If any function returns 0 or false, the action is blocked and the later functions no longer run. Returning nil or any other value means allow. |
command | OnCommandManager | The first function that returns 1 (or true / a non-0 number) is considered to have handled it and stops. Returning 0, false or nothing (nil) = not its own command, pass it to the next function. A function that forgets return or errors also cannot "swallow" another module's command. You should return 1 when handled and 0 when it is not your command. |
sql | OnSQLAsyncResult | Every function receives every result; each function checks its own label. A function that returns 1 for every result does not take away another module's results. Any function that returns 2 = run the query again. |
The list of events of the veto kind in Modules.lua: onUseCommand, onNpcTalk, onTradeRequest, onTradeAnswer, onTradeAccept, onCanEnterEvent, onItemUse, onItemPick, onItemDrop, onItemMove, onItemEquip, onItemUnEquip, onShopBuy, onShopSell, onMossMerchantUse, onCashShopBuy, onCashShopGift, onCashShopItemUse, onMapMoveRequest, onGateEnter, onCheckUserKiller, onCheckUserTarget, onUseSkill. The remaining events are all.
onItemMove, onItemEquip, onItemUnEquip are called twice for the same operation: the 1st time with state = 0 (before doing it, can block) and the 2nd time with state = 1 (already done, notification only). Your function should check state before returning 0.Errors in attached functions
- Function does not exist: the log
function does not existis written. - Function raises an error: the log
[event name] function name: error messageis written, and the other functions still run. - So that the log is not flooded when an error repeats every second, each function logs only its first 5 errors (the 5th line has the note
repeating error, no more logs for this function). - A blocking event that errors is treated as "allow".
BridgeDispatch
BridgeDispatch(event, ...) runs the functions attached to event and returns the merged result according to the table above. Use it when you write the onXxx function yourself but still want the attached modules to be called:
function onItemUse(aIndex, sourceSlot, targetSlot, useType)
-- your own code here
return BridgeDispatch("onItemUse", aIndex, sourceSlot, targetSlot, useType)
end