Script Structure¶
ScriptCore.lua — The Event Dispatcher¶
ScriptCore.lua is loaded automatically by the GameServer. It manages all event handlers through a central registry.
-- Internal event handler registry (inside ScriptCore.lua)
local EventHandlers = {
OnReadScript = {},
OnShutScript = {},
OnTimerThread = {},
OnCommandManager = {},
OnCharacterEntry = {},
OnCharacterClose = {},
OnNpcTalk = {},
OnMonsterDie = {},
OnUserDie = {},
OnUserRespawn = {},
OnCheckUserTarget = {},
OnCheckUserKiller = {},
OnPacketRecv = {},
OnConnectDataBase = {},
OnItemGet = {},
OnItemDrop = {},
OnGremoryCaseReady = {},
}
BridgeFunctionAttach()¶
Registers a callback into the event dispatcher.
eventName(string) — Must match one of the events inEventHandlershandlerFunction(function) — Your callback function
Multiple Handlers¶
You can attach multiple handlers to the same event. They execute in registration order:
-- File: Welcome.lua
BridgeFunctionAttach("OnCharacterEntry", function(aIndex)
GCNoticeSend(aIndex, 1, "Welcome!")
end)
-- File: DailyReward.lua
BridgeFunctionAttach("OnCharacterEntry", function(aIndex)
-- Give daily login reward
ItemGive(aIndex, 100)
end)
-- Both handlers run when a player logs in
Available Events¶
| Event | Parameters | Return | Description |
|---|---|---|---|
OnReadScript |
— | — | Scripts loaded/reloaded |
OnShutScript |
— | — | Server shutting down |
OnTimerThread |
— | — | Timer tick (~1/sec) |
OnConnectDataBase |
— | — | Database connected |
OnCharacterEntry |
aIndex |
— | Player enters game |
OnCharacterClose |
aIndex |
— | Player disconnects |
OnGremoryCaseReady |
aIndex |
— | Gremory data loaded |
OnUserDie |
aIndex, bIndex |
— | Player dies |
OnUserRespawn |
aIndex, killerType |
— | Player respawns |
OnMonsterDie |
aIndex, bIndex |
0/1 | Monster dies |
OnCommandManager |
aIndex, code, arg |
0/1 | Custom command used |
OnNpcTalk |
aIndex, bIndex |
0/1 | NPC interaction |
OnCheckUserTarget |
aIndex, bIndex |
0/1 | PvP target validation |
OnCheckUserKiller |
aIndex, bIndex |
0/1 | PK penalty check |
OnItemGet |
aIndex, index, item |
0/1 | Item pickup |
OnItemDrop |
aIndex, slot, item |
0/1 | Item drop |
OnPacketRecv |
aIndex, head, packet |
0/1 | Custom packet received |
Recommended File Structure¶
LuaScript/
System/
ScriptCore.lua # Event dispatcher (don't modify)
ScriptDefine.lua # Constants (CLASS_DW, MAP_LORENCIA, etc.)
ScriptReader.lua # File parser utility
Welcome.lua # Your custom scripts
DailyReward.lua
CustomCommands.lua
BossEvent.lua
Script Lifecycle¶
Server Start
├─► OnReadScript() — Initialize your data
├─► OnConnectDataBase() — Set up SQL connections
│
│ [Game Running]
├─► OnTimerThread() — Every ~1 second
├─► OnCharacterEntry() — Player joins
├─► OnGremoryCaseReady() — Gremory loaded (safe to give items)
├─► OnNpcTalk() — NPC clicked
├─► OnMonsterDie() — Monster killed
├─► OnUserDie() — Player killed
├─► OnCharacterClose() — Player leaves
│
└─► OnShutScript() — Server stopping, clean up