- Joined
- Jun 26, 2020
- Messages
- 1,928
(There is an improved version of this system in the Eikonium's Debug Utils)
I discussed this system in the Advanced Scripting forum, since there is a while I got a feedback, I think is time to share it here.
This adds the function
Example:
Let's imagine this is all the war3map.lua file:
So the screen would show:
Which are the positions of the functions that called the
I discussed this system in the Advanced Scripting forum, since there is a while I got a feedback, I think is time to share it here.
This adds the function
GetStackTrace()
that is something like a replacement of a function that returns the stack trace and is missed in W3.
Lua:
do
local prefixes = {"war3map.lua", "blizzard.j.lua"}
local n = #prefixes
local prefixesLen = {}
for i = 1, n do
prefixesLen[i] = string.len(prefixes[i])
end
local list = {}
local lastMsg = nil
local function getPos(msg, pos)
error(msg, pos)
end
local function store(msg)
lastMsg = msg
end
local function checkPrefixes()
for i = 1, n do
if string.sub(lastMsg, 1, prefixesLen[i]) == prefixes[i] then
return true
end
end
return false
end
---Returns stack trace, but only the position of the called functions, not its names
---@return string
function GetStackTrace()
local stack = ""
local i = 4
local p = 1
while true do
xpcall(getPos, store, "- " .. p, i)
if not checkPrefixes() then break end
table.insert(list, lastMsg)
i = i + 1
p = p + 1
end
for j = #list, 1, -1 do
stack = stack .. list[j] .. "\n"
end
list = {}
return stack
end
end
Example:
Let's imagine this is all the war3map.lua file:
Lua:
local function Fourth() -- 1
print(GetStackTrace()) -- 2
end -- 3
-- 4
local function Third() -- 5
Fourth() -- 6
end -- 7
-- 8
local function Second() -- 9
Third() -- 10
end -- 11
-- 12
local function First() -- 13
Second() -- 14
end -- 15
-- 16
First() -- 17
Code:
war3map.lua:2: - 5
war3map.lua:6: - 4
war3map.lua:10: - 3
war3map.lua:14: - 2
war3map.lua:17: - 1
GetStackTrace
function, you can use this to handle errors, but it has its limits:- You need to know where the error will happen and before of that call the
GetStackTrace
function. - Maybe you can overwrite the error function (that would include edit this system), but there would left the errors provoked by other things, like indexing a nil value or aritmetic operations with string values.
Last edited: