Lua脚本代码如何调试?

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
点赞
用户1381216
用户1381216

你把 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)

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

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

我还修改了代码,这样你就可以写 bunnyhop_test 0 来禁用脚本,写 bunnyhop_test 1 来启用脚本。

2016-10-09 07:59:19