Ir para o conteúdo

Primeiros Passos

O GameServer inclui um motor de scripting Lua 5.4. Scripts registram manipuladores de eventos através do BridgeFunctionAttach() e são carregados automaticamente na inicialização via ScriptCore.lua.

Primeiro Script

Crie um arquivo chamado Welcome.lua no diretório de scripts:

-- Welcome.lua - Send a welcome message when players log in

local function OnPlayerLogin(aIndex)
    local player = GetUser(aIndex)
    if player == nil then return end

    GCNoticeSend(aIndex, 1, "Welcome to the server, " .. player.Name .. "!")
    LogAdd(LOG_GREEN, "[Welcome] " .. player.Name .. " has logged in.")
end

-- Register the handler
BridgeFunctionAttach("OnCharacterEntry", OnPlayerLogin)

Conceitos Principais

Arquitetura Orientada a Eventos

Manipuladores são registrados em eventos via BridgeFunctionAttach():

BridgeFunctionAttach("OnMonsterDie", function(aIndex, bIndex)
    -- This runs every time a monster dies
    -- aIndex = monster, bIndex = killer
end)

A Função GetUser()

Manipuladores recebem um aIndex (índice do objeto). GetUser() retorna o objeto completo:

local player = GetUser(aIndex)
if player then
    -- Access properties
    print(player.Name)      -- Character name
    print(player.Level)     -- Current level
    print(player.Map)       -- Current map
    print(player.Class)     -- Character class
end

Valores de Retorno em Hooks

Alguns hooks usam valores de retorno para controlar o comportamento do jogo:

Retorno Significado
0 Bloquear a ação padrão
1 Permitir a ação padrão
BridgeFunctionAttach("OnItemDrop", function(aIndex, slot, item)
    if item:IsSetItem() then
        GCNoticeSend(aIndex, 1, "You cannot drop Ancient items!")
        return 0  -- Block the drop
    end
    return 1  -- Allow normally
end)

Próximos Passos