Skip to content

Tutorial Receiving and sending opcodes

Joao Pasqualini Costa edited this page Jan 18, 2024 · 12 revisions

Receiving custom opcode from the server

First go to:

/modules/gamelib/const.lua

Search for

ExtendedIds = {
    Activate = 0,
    Locale = 1,
    Ping = 2,
    Sound = 3,
    Game = 4,
    Particles = 5,
    MapShader = 6,
    NeedsUpdate = 7
}

Add your custom extended opcode there (can't be greater than 255):

ExtendedIds = {
    Activate = 0,
    Locale = 1,
    Ping = 2,
    Sound = 3,
    Game = 4,
    Particles = 5,
    MapShader = 6,
    NeedsUpdate = 7

    MyCustomOpcode = 100,
}

Now on your module add the proper register and unregister method

function init()
    ProtocolGame.registerExtendedOpcode(ExtendedIds.MyCustomOpcode, onExtendedLocales)
end

function terminate()
    ProtocolGame.unregisterExtendedOpcode(ExtendedIds.MyCustomOpcode)
end

function onExtendedLocales(protocol, opcode, buffer)
    print(buffer)
end

Sending custom opcodes to the server

On your module add this function to send some data to the server

function sendOpcode(someData)
    local protocolGame = g_game.getProtocolGame()
    if protocolGame then
        protocolGame:sendExtendedOpcode(ExtendedIds.MyCustomOpcode, localeName)
        return true
    end
    return false
end