Lua 如何在1小时后停止脚本到C堆栈溢出?
嗨,我是新来的脚本制作!我为一个游戏制作了一个脚本,它在大约1小时后停止工作。错误代码是“C堆栈溢出”。我使用Visual Studio…我试图在代码中找到任何错误,但一切似乎都很好!但1小时后,我仍然得到我的错误!我在论坛上看到过,但我在这方面真的是个新手。所以我发布了我的代码,我希望能找到能帮助我解决问题的人 我找不到解决办法我需要有人解释我的错误! 谢谢你的帮助Lua 如何在1小时后停止脚本到C堆栈溢出?,lua,stack-overflow,Lua,Stack Overflow,嗨,我是新来的脚本制作!我为一个游戏制作了一个脚本,它在大约1小时后停止工作。错误代码是“C堆栈溢出”。我使用Visual Studio…我试图在代码中找到任何错误,但一切似乎都很好!但1小时后,我仍然得到我的错误!我在论坛上看到过,但我在这方面真的是个新手。所以我发布了我的代码,我希望能找到能帮助我解决问题的人 我找不到解决办法我需要有人解释我的错误! 谢谢你的帮助 -- Auteur : Max412 GATHER = {} OPEN_BAGS = true AUTO_DELETE = {
-- Auteur : Max412
GATHER = {}
OPEN_BAGS = true
AUTO_DELETE = {}
MAX_MONSTERS = 8
MIN_MONSTERS = 1
FORBIDDEN_MONSTERS = {}
FORCE_MONSTERS = {}
function hiboux()
npc:npc(522,3)
npc:reply(-1)
exchange:putAllItems()
global:leaveDialog()
end
function GatherFight()
for index, actualMap in pairs(mapsWithChangeMap) do
if (map:onMap(actualMap.map)) then
if actualMap.gather and actualMap.fight then
return "both"
elseif actualMap.gather then
return "gather"
elseif actualMap.forcegather then
return "forcegather"
elseif actualMap.fight then
return "fight"
elseif actualMap.forcefight then
return "forcefight"
end
return "path"
end
end
return false
end
------------------
function move()
if banque then
for index, actualMap in pairs(mapsWithChangeMap) do
if (type(actualMap.path) == "table") then
if (type(actualMap.toggle) == "table") then
for i = 1, #(actualMap.toggle) do
actualMap.toggle[i] = false
end
end
end
end
banque = false
end
if GatherFight() == "both" then
return {
{map = map:currentMap(), custom = processMap, gather = true, fight = true}
}
elseif GatherFight() == "gather" then
return {
{map = map:currentMap(), custom = processMap, gather = true}
}
elseif GatherFight() == "forcegather" then
return {
{map = map:currentMap(), custom = processMap, forcegather = true}
}
elseif GatherFight() == "fight" then
return {
{map = map:currentMap(), custom = processMap, fight = true}
}
elseif GatherFight() == "forcefight" then
return {
{map = map:currentMap(), custom = processMap, forcefight = true}
}
elseif GatherFight() == "path" then
return {
{map = map:currentMap(), custom = processMap}
}
else
global:printError("Aucune action sur la map : " .. map:currentMap() .. " | mapID : " .. map:currentMapId())
global:disconnect()
end
end
mapsWithChangeMap = {
{map = "9,8", path = {"bottom", "left"}, gather = true},
{map = "9,9", path = {"top"}, gather = true},
{map = "8,8", path = {"left"}, gather = true},
{map = "7,8", path = {"left"}, gather = true},
{map = "6,8", path = {"left", "top"}, gather = true},
{map = "5,8", path = {"left"}, gather = true},
{map = "4,8", path = {"left"}, gather = true},
{map = "3,8", path = {"left"}, gather = true},
{map = "2,8", path = {"left"}, gather = true},
{map = "1,8", path = {"left"}, gather = true},
{map = "0,8", path = {"bottom"}, gather = true},
{map = "0,9", path = {"right"}, gather = true},
{map = "1,9", path = {"right"}, gather = true},
{map = "2,9", path = {"right"}, gather = true},
{map = "3,9", path = {"right"}, gather = true},
{map = "4,9", path = {"right"}, gather = true},
{map = "5,9", path = {"right"}, gather = true},
{map = "6,9", path = {"top"}, gather = true},
{map = "6,7", path = {"left"}, gather = true},
{map = "5,7", path = {"top", "left"}, gather = true},
{map = "4,7", path = {"right"}, gather = true},
{map = "6,6", path = {"right"}, gather = true},
{map = "5,6", path = "top", gather = true},
{map = "5,5", path = "top", gather = true},
{map = "5,4", path = "top", gather = true},
{map = "5,3", path = "top", gather = true},
{map = "5,2", path = "right", gather = true},
{map = "6,2", path = "bottom", gather = true},
{map = "6,3", path = "bottom", gather = true},
{map = "6,4", path = "bottom", gather = true},
{map = "6,5", path = "bottom", gather = true},
{map = "7,6", path = "top", gather = true},
{map = "7,5", path = "top", gather = true},
{map = "7,4", path = "top", gather = true},
{map = "7,3", path = "top", gather = true},
{map = "7,2", path = "top", gather = true},
{map = "7,1", path = "right", gather = true},
{map = "8,1", path = "bottom", gather = true},
{map = "8,2", path = "bottom", gather = true},
{map = "8,3", path = "bottom", gather = true},
{map = "8,4", path = "bottom", gather = true},
{map = "8,5", path = "bottom", gather = true},
{map = "8,6", path = "right", gather = true},
{map = "9,6", path = "top", gather = true},
{map = "9,5", path = "right", gather = true},
{map = "10,5", path = "right", gather = true},
{map = "11,5", path = "bottom", gather = true},
{map = "11,6", path = "bottom", gather = true},
{map = "11,7", path = "bottom", gather = true},
{map = "11,8", path = "left", gather = true},
{map = "10,8", path = "left", gather = true},
{map = "2,-2", path = "bottom"},
{map = "2,-1", path = "bottom"},
{map = "2,0", path = "bottom"},
{map = "2,1", path = "bottom"},
{map = "2,2", path = "bottom"},
{map = "2,3", path = "bottom"},
{map = "2,4", path = "bottom"},
{map = "2,5", path = "bottom"},
{map = "2,6", path = "bottom"},
{map = "2,7", path = "right"},
{map = "3,7", path = "right"},
}
function processMap()
for index, actualMap in pairs(mapsWithChangeMap) do
if (map:onMap(actualMap.map)) then
if (type(actualMap.path) == "table") then
if (not actualMap.toggle) then
actualMap.toggle = {}
for i = 1, #(actualMap.path) do
table.insert(actualMap.toggle, false)
end
else
toggleTemp = false
for i = 1, #(actualMap.toggle) do
toggleTemp = actualMap.toggle[i]
end
if (toggleTemp) then
for i = 1, #(actualMap.toggle) do
actualMap.toggle[i] = false
end
end
end
for i, action in pairs(actualMap.path) do
if (not actualMap.toggle[i]) then
if (type(action) == "string") then
actualMap.toggle[i] = true
return map:changeMap(action)
elseif (type(action) == "number") then
actualMap.toggle[i] = true
return map:door(action)
elseif (type(action) == "function") then
actualMap.toggle[i] = true
return action()
end
end
end
else
if (actualMap.path) then
return map:changeMap(actualMap.path)
elseif (actualMap.door) then
if (type(actualMap.door) == "string") then
return map:door(tonumber(actualMap.door))
elseif (type(actualMap.door) == "number") then
return map:door(actualMap.door)
end
elseif (actualMap.custom) then
return actualMap.custom()
end
end
end
end
end
function bank()
banque = true
return {
{map = "2,7", path = "top"},
{map = "2,6", path = "top"},
{map = "2,5", path = "top"},
{map = "2,4", path = "top"},
{map = "2,3", path = "top"},
{map = "2,2", path = "top"},
{map = "2,1", path = "top"},
{map = "2,0", path = "top"},
{map = "2,-1", path = "top"},
{map = "11,5", path = "left"},
{map = "10,5", path = "left"},
{map = "11,6", path = "left"},
{map = "11,7", path = "left"},
{map = "11,8", path = "left"},
{map = "10,8", path = "left"},
{map = "10,7", path = "left"},
{map = "10,6", path = "left"},
{map = "9,5", path = "left"},
{map = "9,6", path = "left"},
{map = "9,7", path = "left"},
{map = "9,8", path = "left"},
{map = "8,8", path = "left"},
{map = "8,7", path = "left"},
{map = "8,6", path = "left"},
{map = "8,5", path = "left"},
{map = "8,4", path = "left"},
{map = "8,3", path = "left"},
{map = "8,2", path = "left"},
{map = "8,1", path = "left"},
{map = "7,1", path = "left"},
{map = "7,2", path = "left"},
{map = "7,3", path = "left"},
{map = "7,4", path = "left"},
{map = "7,5", path = "left"},
{map = "7,6", path = "left"},
{map = "7,7", path = "left"},
{map = "7,8", path = "left"},
{map = "6,8", path = "left"},
{map = "6,7", path = "left"},
{map = "6,6", path = "left"},
{map = "6,5", path = "left"},
{map = "6,4", path = "left"},
{map = "6,3", path = "left"},
{map = "6,2", path = "left"},
{map = "5,2", path = "left"},
{map = "5,3", path = "left"},
{map = "5,4", path = "left"},
{map = "5,5", path = "left"},
{map = "5,6", path = "left"},
{map = "5,7", path = "left"},
{map = "5,8", path = "left"},
{map = "5,9", path = "left"},
{map = "6,9", path = "left"},
{map = "4,9", path = "left"},
{map = "4,8", path = "left"},
{map = "4,7", path = "left"},
{map = "4,2", path = "left"},
{map = "4,3", path = "left"},
{map = "4,4", path = "left"},
{map = "4,5", path = "left"},
{map = "4,6", path = "left"},
{map = "3,9", path = "left"},
{map = "3,8", path = "left"},
{map = "3,7", path = "left"},
{map = "3,6", path = "left"},
{map = "3,5", path = "left"},
{map = "3,4", path = "left"},
{map = "3,3", path = "left"},
{map = "3,2", path = "left"},
{map = "1,8", path = "right"},
{map = "1,9", path = "right"},
{map = "0,9", path = "right"},
{map = "0,8", path = "right"},
{map = "2,9", path = "top"},
{map = "2,8", path = "top"},
{map = "6,1", path = "left"},
{map = "5,1", path = "left"},
{map = "4,1", path = "left"},
{map = "3,1", path = "left"},
{map = "9,9", path = "top"},
{map = "20,1", path = "bottom"},
{map = "191104002", door = "288"}, --Devant banque Astrub--
{map = "192415750", path = "409", custom = hiboux}, --Banque Astrub--
{map = "54172457", door = "358"}, --Devant banque Frigost--
{map = "54534165", path = "424", npcBank = true}, --Banque Frigost--
{map = "147254", door = "383"}, --Devant banque Bonta--
{map = "2885641", path = "424", npcBank = true}, --Banque Bonta--
{map = "88081177", door = "216"}, --Devant banque Amakna--
{map = "99095051", path = "410", npcBank = true}, --Banque Amakna--
{map = "144931", door = "248"}, --Devant banque Brakmar--
{map = "8912911", path = "424", npcBank = true}, --Banque Brakmar--
{map = "90703872", door = "302"}, --Devant banque Sufokia --
{map = "91753985", path = "494", npcBank = true}, --Banque Sufokia--
{map = "155157", door = "355"}, --Devant banque Ottomaï--
{map = "86511105", door = "452", npcBank = true}, --Banque Ottomaï--
{map = "12580", door = "284"}, --Devant banque Pandala--
{map = "8129542", path = "409", npcBank = true}, --Banque Pandala--
{map = "73400323", door = "330"}, --Devant banque Montagne Koalak--
{map = "84935175", path = "425", npcBank = true}, --Banque Montagne Koalak--
}
end
function phenix()
return {
}
end
首先,让我解释一下堆栈溢出的含义 解释如下:
recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
在软件中,如果调用堆栈指针超出堆栈界限,则会发生堆栈溢出。调用堆栈可能由有限的地址空间组成,通常在程序开始时确定。调用堆栈的大小取决于许多因素,包括编程语言、机器体系结构、多线程和可用内存量。当程序试图使用比调用堆栈上可用空间更多的空间时(即,当它试图访问超出调用堆栈边界的内存时,这本质上是缓冲区溢出),堆栈被称为溢出,通常导致程序崩溃
换句话说:脚本中的某些内容不断添加到调用堆栈中,直到溢出为止
这通常发生在使用递归函数时。您似乎遗漏了部分代码(尤其是Map类实现),因此很难判断是哪行代码导致了问题。但我认为,一旦你理解了为什么会发生这种情况,你自己就能找到它
想象这样一个脚本:
recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
本地函数打印()
打印“Foobar”
结束
打印()
打印()
输出将是:
"Foobar"
"Foobar"
"Foobar"
"Foobar"
"Foobar"
"Foobar"
...
Stack-Overflow Error
但更重要的是,调用堆栈不会增加,因为一旦处理了第一个print函数,它就会从调用堆栈中删除。然后第二个被称为
现在将其与此函数进行比较:
local函数recursivePrint()
打印“Foobar”
recursivePrint()--函数调用自身,开始递归。
结束
递归打印()
输出将是:
"Foobar"
"Foobar"
"Foobar"
"Foobar"
"Foobar"
"Foobar"
...
Stack-Overflow Error
为什么会这样?每次调用recursivePrint时,都会将其添加到调用堆栈,并将Foobar
打印到控制台。但不是从调用堆栈中删除,而是添加并执行下一个recursivePrint
您可以想象调用堆栈如下所示:
recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
calls -> recursivePrint()
只要未达到堆栈边界,这种情况就会继续。一旦超过该值,就会抛出错误
那么如何修复代码呢?
通过通读代码或使用调试器,在代码中查找递归调用。对于初学者来说,直接调试可能有点难,所以我建议使用易于理解的方法
还要确保使用正确的工具。该页面实际上描述了一个与您类似的游戏(如果我正确解释了您的代码):
作为一个例子,让我们考虑一个简单的迷宫游戏。迷宫有几个房间,每个房间最多有四扇门:北、南、东、西。在每一步中,用户输入一个移动方向。如果该方向有门,则用户进入相应的房间;否则,程序将打印一条警告。目标是从最初的房间到最后的房间
更重要的是:
如果没有适当的尾部调用,每个用户移动都会创建一个新的堆栈级别。经过一定数量的移动后,将出现堆栈溢出。通过适当的尾部调用,用户可以进行的移动次数没有限制,因为每个移动实际上执行到另一个函数的转到,而不是常规调用
我很确定这是您的实际问题。根据脚本结构,您正在使用SnowBot或其衍生物。 我不知道它是如何工作的,但我有一些不知道背景的观察结果 我假设这部分代码强制递归:
elseif(actualMap.custom)然后
返回actualMap.custom()
结束
actualMap.custom
参数在某些情况下计算为processMap
,而custom
可能是API的一部分,引擎在其他情况下调用此函数
要跟踪这是否会产生一些问题,请在processMap
函数的开头添加:
global:printError(“跟踪进程映射:…..调试.回溯())
它必须显示堆栈调用列表,如果每次都较长,则此函数会给您带来问题
您可能可以注释掉return actualMap.custom()
并检查它是否有用,然后注释掉其他部分,依此类推
在您的位置,我会添加更多的检查点global:printError('Check…')
,以检查它是否按预期运行,或者可能会多次执行某些操作
PS
存在逻辑错误:
toggleTemp=false
对于i=1,#(实际映射切换)do
toggleTemp=actualMap.toggle[i]
结束
toggleTemp
始终是最后一个元素的值堆栈溢出错误主要发生在递归调用上,例如,如果map:changeMap()
执行processMap()
。在您的情况下,我会使用luadebug
模块来跟踪lua状态堆栈。是否可以添加一个命令来防止堆栈溢出错误的发生?我以前从未运行过调试,所以我不知道如何运行!只有解决问题才能防止堆栈溢出。您可以添加print('TRACK1:'…debug.traceback())
或使用f=io.open(…,'a')写入文件;f:写(…);f:在某些地方关闭()
,并监视堆栈增长的位置。很抱歉再次问您Darius!你能在这方面再帮我一点忙吗!我没有所有的知识来修复我的脚本,我真的需要更多的帮助或一个例子,谢谢你的支持!它将帮助我这么多,为我的其他脚本!