【问题标题】:How to debug Lua script code?如何调试 Lua 脚本代码?
【发布时间】:2016-10-09 17:42:18
【问题描述】:
local function CreateCvar(cvar, value)
    CreateClientConVar(cvar, value)
end
--cvars
CreateCvar("bunnyhop_test", 0)
CreateCvar("bunnyhop_test_off", 0)

if CLIENT then
    function ReallyHiughJumpoBHOP()
    --concommand.Add("+bhop",function()
    if GetConVarNumber("bunnyhop_test") then
    hook.Add("Think","hook",function()
    RunConsoleCommand(((LocalPlayer():IsOnGround() or LocalPlayer():WaterLevel() > 0) and "+" or "-").."jump")
    end
end)


    function ReallyHiughJumpoBHOPoff()
--concommand.Add("-bhop",function()
    if GetConVarNumber("bunnyhop_test_off") then
    RunConsoleCommand("-jump")
    hook.Remove("Think","hook")
end)

这是为游戏“Garry's mod”制作的 lua 脚本。这应该重复跳跃。我已经编辑了有效的基本代码,现在我的代码不再有效。

尝试使用 createcvars 使其工作。我确实让它没有显示任何错误,但在游戏中当我在控制台中输入“bunnyhop_test 1”时它不起作用。

以下是我开始使用的原始代码:

if CLIENT then
    concommand.Add("+bhop",function()
        hook.Add("Think","hook",function()
            RunConsoleCommand(((LocalPlayer():IsOnGround() or LocalPlayer():WaterLevel() > 0) and "+" or "-").."jump")
        end)
    end)

    concommand.Add("-bhop",function()
        RunConsoleCommand("-jump")
        hook.Remove("Think","hook")
    end)
end

【问题讨论】:

  • 是的,我有点杀了它^.^
  • 好的,我可以看到您想使用自己的控制台变量更改脚本。我更新了代码,以便您可以编写 bunnyhop_test 1 来启用和 bunnyhop_test 0 来禁用脚本。当然,您需要使用+bhop 启动它。

标签: lua garrys-mod


【解决方案1】:

您搞砸了 end 关键字顺序。一些if 语句没有正确关闭,一些函数声明没有正确关闭end

从编辑中,我只能猜测这是您想要做的:

local function CreateCvar(cvar, value)
    CreateClientConVar(cvar, value)
end

--cvars
CreateCvar("bunnyhop_test", 0)

if CLIENT then
    concommand.Add("+bhop",function()
            hook.Add("Think","hook",function()
                if GetConVarNumber("bunnyhop_test") == 1 then
                    RunConsoleCommand(((LocalPlayer():IsOnGround() or LocalPlayer():WaterLevel() > 0) and "+" or "-").."jump")
                end
            end)
        end
    end)

    concommand.Add("-bhop",function()
        RunConsoleCommand("-jump")
        hook.Remove("Think","hook")
    end)
end

看,当一个函数被声明为内联时,称为闭包,你必须用关键字end来匹配它,以表示它的结束。另外,请注意您将这些内联函数作为参数传递给另一个函数.Add,该函数以( 开头,必须以) 结尾。 if 语句,还需要有一个end 关键字来表示if 的结尾。这些都是基本的编程原则,尝试阅读更多代码以熟悉如何编写更多代码,也许从lua documentation开始。

我还修改了代码,以便您可以编写 bunnyhop_test 0 来禁用脚本,编写 bunnyhop_test 1 来启用脚本。

【讨论】:

  • 谢谢我现在没有错误了。代码确实按预期工作,但我不明白的是这个。我想通过添加 convar,我可以打开/关闭它。当我进入控制台并输入“+bhop”时,它工作正常!!!但是,如果我尝试通过“bunnyhop_test 1”打开它,它什么也不做。我不明白那部分。
  • GetConVarNumber 会返回你设置的值,但是因为它返回 0,lua 会把它当作true,只有false 或者nil 是假的。你必须在那里检查平等,比如GetConVarNumber("bunnyhop_test_off") == 1
  • 所以我可能需要这样的东西? ------------------------------------如果 GetConVarNumber("bunnyhop_test") > 0 那么
  • 是的,GetConVarNumber("bunnyhop_test_off") == 1GetConVarNumber("bunnyhop_test_off") > 0 可以工作,除非返回值是字符串。
  • 那么我猜返回值是一个字符串,因为它不会用 cmd“bunnyhop_test_off”关闭,+bhop 和 -bhop 可以很好地打开和关闭它。
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 2014-11-29
  • 2011-06-17
  • 2013-09-11
  • 2016-07-24
  • 2013-07-26
  • 2014-02-14
  • 1970-01-01
相关资源
最近更新 更多