TalkAction [TFS 1.x / LuaJIT] Debug LUA scripts with talkaction!

Gesior.pl

Mega Noob&LOL 2012
Joined
Sep 18, 2007
Messages
2,014
Best answers
19
Reaction score
1,012
Location
PLand
Credits to Cykotitan, based on TFS 0.x version TalkAction - Debug LUA scripts with talkaction!

Talkaction that let you find problems with LUA scripts on GOD character.
You can also use it to do some admin actions in game. Fix small map bugs, make new spawns - without server restart.

XML data/talkactions/talkactions.xml:
PHP:
<talkaction words="/lua" separator=" " script="lua_debug.lua" />
<talkaction words="!lua" separator=" " script="lua_debug.lua" />
LUA data/talkactions/scripts/lua_debug.lua:
PHP:
function sendToPlayerLuaCallResult(player, ...)
   local n = select('#', ...)
   result = setmetatable({ ... }, {
      __len = function()
         return n
      end,
   })

   local t = {}
   for i = 2, #result do
      local v = tostring(result[i])
      if v:len() > 0 then
         table.insert(t, v)
      end
   end

   if #t > 0 then
      player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, table.concat(t, ', '))
   end
end

function onSay(player, words, param)
   if player:getGroup():getId() <= 4 then
      return false
   end

   if player:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
      return false
   end

   sendToPlayerLuaCallResult(player, pcall(loadstring(
         'local cid = ' .. player:getId() .. ' ' ..
         'local player = Player(cid) ' ..
         'local pos = player:getPosition() ' ..
         'local position = pos ' ..
         param
   )))

   return false
end
Predefined variables:
cid
- GOD character 'cid'
player - GOD character Player object
pos - position of GOD character
position - position of GOD character

Examples:

1. Get current position of player 'Adsad':

PHP:
!lua p = Player('Adsad'):getPosition() return p.x, p.y, p.z
Result:
Local Chat in Tibia client said:
32368, 32224, 7
You can also split code to few lines:
PHP:
!lua var1 = Player('Adsad')
!lua var2 = var1:getPosition()
!lua return var2.x, var2.y, var2.z
2. Spawn monster Demon next to GOD:
PHP:
!lua Game.createMonster("Demon", pos)
3. Spawn monster Demon 20 times every 5 seconds on position on which is GOD in moment of saying !lua:
PHP:
!lua function spawnDemon(pos, countLeft) if countLeft > 0 then addEvent(spawnDemon, 5000, pos, countLeft - 1) end Game.createMonster("Demon", pos) end spawnDemon(pos, 20)
4. Set GOD storage value with key 123 to 456:
PHP:
!lua player:setStorageValue(123, 456)
Set player 'Adsad' storage value with key 123 to 456:
PHP:
!lua Player('Adsad'):setStorageValue(123, 456)
5. Get GOD storage value with key 123:
PHP:
!lua return player:getStorageValue(123)
Get player 'Adsad' storage value with key 123:
PHP:
!lua return Player('Adsad'):getStorageValue(123)
Result:
Local Chat in Tibia client said:
 

pepsiman

---
Joined
Nov 17, 2017
Messages
101
Best answers
5
Reaction score
43
hey this is really useful. Thanks for sharing :D
 

slawkens

Developer
Premium User
Joined
Aug 27, 2007
Messages
2,870
Best answers
60
Reaction score
383
Location
Germany
For TFS 0.3.6+ (as a mod): slawkens/tfs-mods

Usage: (same as above, the only difference is Lua functions that are used in 0.3.6 that diverse from 1.x)

I am making a short tutorial currently, that I will update with time (currently just a few examples).

Link to tutorial with examples (work in progress): slawkens/tfs-mods
 
OP
Gesior.pl

Gesior.pl

Mega Noob&LOL 2012
Joined
Sep 18, 2007
Messages
2,014
Best answers
19
Reaction score
1,012
Location
PLand
Can some moderator change in my first post code:
PHP:
result = setmetatable({ ... }, {
to:
PHP:
local result = setmetatable({ ... }, {
#globalscope
@slawkens
 
Top