Saltar a contenido

Estructura de Scripts

ScriptCore.lua — El Despachador de Eventos

ScriptCore.lua se carga automáticamente por el GameServer. Administra todos los manejadores de eventos a través de un registro central.

-- 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()

Registra un callback en el despachador de eventos.

BridgeFunctionAttach(eventName, handlerFunction)
  • eventName (string) — Debe coincidir con uno de los eventos en EventHandlers
  • handlerFunction (function) — Tu función callback

Múltiples Manejadores

Puedes adjuntar múltiples manejadores al mismo evento. Se ejecutan en orden de registro:

-- 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

Eventos Disponibles

Evento Parámetros Retorno Descripción
OnReadScript Scripts cargados/recargados
OnShutScript Servidor apagándose
OnTimerThread Tick del temporizador (~1/seg)
OnConnectDataBase Base de datos conectada
OnCharacterEntry aIndex Jugador entra al juego
OnCharacterClose aIndex Jugador se desconecta
OnGremoryCaseReady aIndex Datos de Gremory cargados
OnUserDie aIndex, bIndex Jugador muere
OnUserRespawn aIndex, killerType Jugador reaparece
OnMonsterDie aIndex, bIndex 0/1 Monstruo muere
OnCommandManager aIndex, code, arg 0/1 Comando personalizado usado
OnNpcTalk aIndex, bIndex 0/1 Interacción con NPC
OnCheckUserTarget aIndex, bIndex 0/1 Validación de objetivo PvP
OnCheckUserKiller aIndex, bIndex 0/1 Verificación de penalización PK
OnItemGet aIndex, index, item 0/1 Recoger item
OnItemDrop aIndex, slot, item 0/1 Soltar item
OnPacketRecv aIndex, head, packet 0/1 Paquete personalizado recibido

Estructura de Archivos Recomendada

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

Ciclo de Vida del Script

Inicio del Servidor
  ├─► OnReadScript()         — Inicializar datos
  ├─► OnConnectDataBase()    — Configurar conexiones SQL
  │   [Juego en Ejecución]
  ├─► OnTimerThread()        — Cada ~1 segundo
  ├─► OnCharacterEntry()     — Jugador se une
  ├─► OnGremoryCaseReady()   — Gremory cargado (seguro para dar items)
  ├─► OnNpcTalk()            — NPC clickeado
  ├─► OnMonsterDie()         — Monstruo eliminado
  ├─► OnUserDie()            — Jugador eliminado
  ├─► OnCharacterClose()     — Jugador se va
  └─► OnShutScript()         — Servidor deteniéndose, limpiar