Ejemplo: interfaz y comunicación del Main

Category: LuaScript | Subcategory: Added Scripts


¿Para qué sirve?

Muestra un panel y envía una solicitud de prueba al servidor al presionar F8.

Ejemplo de uso

-- Guardar en Data/KGData/Lua/Manager/Interface/KGGuideClient.lua.
-- Demostración: F8 muestra un panel y envía un ping al servidor.
-- Elegir una tecla y un subcódigo libres EN AMBOS lados antes de instalar.
-- No copiar las referencias comentadas como reemplazo de las APIs del motor.
if KGGuideClient and KGGuideClient.installed then return end
KGGuideClient = { installed = true, visible = false, nonce = 0,
    key = 0x77, subcode = 0x7E, prefix = "KG_GUIDE_PING-", status = "Sin respuesta" }
local G = KGGuideClient

function G.SendPing()
    local name = UserGetName()
    if type(name) ~= "string" or name == "" then return end
    G.nonce = (G.nonce % 65535) + 1
    local packetName = G.prefix .. name
    ClearPacket(packetName)
    CreatePacket(packetName, G.subcode)
    SetBytePacket(packetName, 1) -- versión del ejemplo
    SetWordPacket(packetName, G.nonce)
    SendPacket(packetName) -- cliente: no lleva aIndex
    ClearPacket(packetName)
    G.status = "Esperando respuesta"
end

function G.OnKey(key)
    if key ~= G.key then return end
    G.visible = not G.visible
    if G.visible then G.SendPing() end
end

function G.OnPacket(subcode, packetName)
    if subcode ~= G.subcode or packetName ~= G.prefix .. UserGetName() then return false end
    local version = GetBytePacket(packetName, -1)
    local nonce = GetWordPacket(packetName, -1)
    ClearPacket(packetName)
    if version == 1 and nonce == G.nonce then
        G.status = "Cliente y servidor conectados"
        KGLog(G.status)
    end
    return true -- este callback ya consumió su propio paquete
end

function G.Draw()
    if not G.visible then return end
    local x = 20 + ReturnWideScreenX()
    SetFontType(0)
    SetTextBg(0, 0, 0, 160)
    SetTextColor(255, 255, 255, 255)
    RenderText(x, 80, "Lua: " .. UserGetName(), 240, 0)
    RenderText(x, 95, "Mapa: " .. tostring(UserGetMap()), 240, 0)
    RenderText(x, 110, G.status, 240, 0)
    SetTextBg(0, 0, 0, 0)
end

InterfaceController.MainProc(G.Draw)
InterfaceController.MainProcWorldKey(G.OnKey)
InterfaceController.ClientProtocol(G.OnPacket)
Return