• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OTClient Learning

Gicu

Well-Known Member
Joined
Feb 26, 2011
Messages
187
Reaction score
52
Hello!
1. I am looking for useful links and valuable content related to OTC learning.
1.1 Some Modules, bars health/mana, adding new graphic like new HP bars etc.
2. And by the way, how does the table work in lua.
Thanks ! :)
 
Code:
tablename = {
something = {x = 1, y = 2},
somethingelse = {x = 3, y = 4}
}
tablename.something.x equals 1
tablename.something.y equals 2

tablename.somethingelse.x equals 3
tablename.somethingelse.y equals 4




tablename = {
[1] = {something = {x = 1, y = 2}, somethingelse = {x = 3, y = 4}},
[2] = {something = {x = 5, y = 6}, somethingelse = {x = 7, y = 8}}
}
tablename[1].something.x equals 1
tablename[1].something.y equals 2
tablename[1].somethingelse.x equals 3
tablename[1].somethingelse.y equals 4


tablename[2].something.x equals 5
tablename[2].something.y equals 6
tablename[2].somethingelse.x equals 7
tablename[2].somethingelse.y equals 8
 
Something I posted somewhere, can't remember which thread that was. If you want to exchange data between your server and the client. Updated with sending client > server and server > client.

Because extended op codes are accepting only strings, using JSON is very helpful to send more data.
Download and drop json.lua file into otclient/modules/corelib, then open corelib.otmod in that folder and after
Code:
    dofile 'outputmessage'
add
Code:
    dofile 'json'

Now go to forgottenserver\data\lib\core and drop that file there, open core.lua and add
Lua:
dofile('data/lib/core/json.lua')

Usage:
In OTC modules
Lua:
function init()
  ProtocolGame.registerExtendedOpcode(14, onExtendedOpcode)
end

function terminate()
  ProtocolGame.unregisterExtendedOpcode(14, onExtendedOpcode)
end

function sendMyCode()
  local myData = {
    a = "string",
    b = 123,
    c = {
      x = "string in table",
      y = 456
    }
  }

  local protocolGame = g_game.getProtocolGame()
  if protocolGame then
    protocolGame.sendExtendedOpcode(14, json.encode(myData))
  end
end

function onExtendedOpcode(protocol, code, buffer)
  local json_status, json_data =
    pcall(
    function()
      return json.decode(buffer)
    end
  )

  if not json_status then
    g_logger.error("[My Module] JSON error: " .. json_data)
    return false
  end

  print(json_data.foo)
  print(json_data.bar)
end

TFS
Lua:
function onExtendedOpcode(player, opcode, buffer)
    if opcode == 14 then
        local json_status, json_data =
            pcall(
            function()
                return json.decode(buffer)
            end
        )

        if not json_status then
            g_logger.error("[My Module] JSON error: " .. json_data)
            return
        end

        print(json_data.a)
        print(json_data.b)
        print(json_data.c.x)
        print(json_data.c.y)

        local response = {
            foo = "Hello",
            bar = "World"
        }
        player:sendExtendedOpcode(14, json.encode(response))
    end
end

Good luck!
 

Attachments

Last edited by a moderator:
Code:
tablename = {
something = {x = 1, y = 2},
somethingelse = {x = 3, y = 4}
}
tablename.something.x equals 1
tablename.something.y equals 2

tablename.somethingelse.x equals 3
tablename.somethingelse.y equals 4




tablename = {
[1] = {something = {x = 1, y = 2}, somethingelse = {x = 3, y = 4}},
[2] = {something = {x = 5, y = 6}, somethingelse = {x = 7, y = 8}}
}
tablename[1].something.x equals 1
tablename[1].something.y equals 2
tablename[1].somethingelse.x equals 3
tablename[1].somethingelse.y equals 4


tablename[2].something.x equals 5
tablename[2].something.y equals 6
tablename[2].somethingelse.x equals 7
tablename[2].somethingelse.y equals 8
Also this works too.
Lua:
tablename = {
  something = {x = 1, y = 2}
}
tablename["something"].x
tablename["something"].y
Imagine if you have table like this
Lua:
local mySkills = {
  melee = 123,
  distance = 456
}
And you have function with parameters skillName - string, ex. "melee", newValue - number, new value for given skill.
Instead of doing this
Lua:
function skillChange(skillName, newValue)
  if skillName == "melee" then
    mySkills.melee = newValue
  elseif skillName == "distance" then
    mySkills.distance = newValue
  end
end
You can do this
Lua:
function skillChange(skillName, newValue)
  mySkills[skillName] = newValue
end
 
so how to add another stat bar above/around creatures?

I tried to figure that out myself but it seems to be both hardcoded and quite complicated.
 
so how to add another stat bar above/around creatures?

I tried to figure that out myself but it seems to be both hardcoded and quite complicated.
Follow this and adjust for your needs.
 
Back
Top