如何在 Lua 中显示调试信息或 console.log 等效项
How to display debug info or console.log equivalent in Lua
我正在使用 Lua 和 LOVE2D 创建许多游戏,但每当我实现一个新功能并想测试它,或者只是想知道 Lua 中变量的值时,我要么在游戏屏幕上显示它,要么只是希望它能正常工作。
现在我的问题是……
有没有办法在终端或其他地方显示一些信息,例如变量值或其他东西?就像 console.log
in javascript 在浏览器的 javascript 控制台中显示一些内容。那么,有没有办法做到这一点 Lua?使用 LOVE2D?
我正在使用 Mac,所以我有 terminal
而不是命令提示符。有没有办法在那里显示一些内容?其他地方也可以,我只需要看看这些值是否符合预期。
使用 conf.lua
文件来启用控制台,那么您应该可以使用标准的 print()
。您可以阅读 wiki 条目 here.
您只需将 conf.lua
文件添加到 main.lua
所在的相同位置。你的文件可能就这么简单:
function love.conf(t)
t.console = true
end
但请随意复制上面的整个配置文件 link 并编辑您需要的内容。
我不能完全确定这一点,因为我无权访问 Mac,但默认情况下禁用控制台,即使在 Windows 上,也不会显示打印,直到您打开
或者您也可以在游戏本身中显示调试信息,就像某些游戏那样。
我喜欢做的是添加类似 debugVariable = {}
的内容来记录每个循环中发生的事件,并添加 debugPermanent = {}
来记录很少发生的事件。可能添加用于写入变量的便利函数:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
现在一个绘制调试信息的函数:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
我们只需在 love.draw()
的末尾调用此绘制函数,文本就会出现。
显然,这种方法可以进一步改进,几乎可以无限地改进,显示特定变量,为其他一些变量添加图表以阐明您想要显示的信息,但这超出了问题的范围。
最后随时检查here用户提交的调试库。
我正在使用 Lua 和 LOVE2D 创建许多游戏,但每当我实现一个新功能并想测试它,或者只是想知道 Lua 中变量的值时,我要么在游戏屏幕上显示它,要么只是希望它能正常工作。
现在我的问题是……
有没有办法在终端或其他地方显示一些信息,例如变量值或其他东西?就像 console.log
in javascript 在浏览器的 javascript 控制台中显示一些内容。那么,有没有办法做到这一点 Lua?使用 LOVE2D?
我正在使用 Mac,所以我有 terminal
而不是命令提示符。有没有办法在那里显示一些内容?其他地方也可以,我只需要看看这些值是否符合预期。
使用 conf.lua
文件来启用控制台,那么您应该可以使用标准的 print()
。您可以阅读 wiki 条目 here.
您只需将 conf.lua
文件添加到 main.lua
所在的相同位置。你的文件可能就这么简单:
function love.conf(t)
t.console = true
end
但请随意复制上面的整个配置文件 link 并编辑您需要的内容。
我不能完全确定这一点,因为我无权访问 Mac,但默认情况下禁用控制台,即使在 Windows 上,也不会显示打印,直到您打开
或者您也可以在游戏本身中显示调试信息,就像某些游戏那样。
我喜欢做的是添加类似 debugVariable = {}
的内容来记录每个循环中发生的事件,并添加 debugPermanent = {}
来记录很少发生的事件。可能添加用于写入变量的便利函数:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
现在一个绘制调试信息的函数:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
我们只需在 love.draw()
的末尾调用此绘制函数,文本就会出现。
显然,这种方法可以进一步改进,几乎可以无限地改进,显示特定变量,为其他一些变量添加图表以阐明您想要显示的信息,但这超出了问题的范围。
最后随时检查here用户提交的调试库。