简体   繁体   English

尝试调用全局 function 为 nil,但调试器中未显示 function?

[英]Attempt to call global function is nil, but function is not shown in debugger?

I am using Eclipse LDT for development, using the packaged Lua EE and Interpreter for Lua 5.2.我正在使用 Eclipse LDT 进行开发,使用打包的 Lua EE 和 Lua 5.2 的解释器。 I need to call the commandDatabase() method from my main method, though when I try, I receive the error:我需要从我的 main 方法调用commandDatabase()方法,但是当我尝试时,我收到错误:

"attempt to call global 'commandDatabase' (a nil value)". “尝试调用全局‘commandDatabase’(一个零值)”。

I have looked up this error, and I am, as far as I can tell, defining methods in the right order.我已经查看了这个错误,据我所知,我正在以正确的顺序定义方法。 Lua - attempt to call global 'contains' (a nil value) Lua - 尝试调用全局“包含”(零值)

When I view it in the debugger, the interpreter does not seem to find any methods I define between commandSystem and commandHelp .当我在调试器中查看它时,解释器似乎没有找到我在commandSystemcommandHelp之间定义的任何方法。 It shows each other function in the Variables area as eg ["commandSystem"] = function() but commandDatabase() does not appear它在变量区域中相互显示 function 例如["commandSystem"] = function()commandDatabase()没有出现

I have tried calling a wrapper method like so:我试过像这样调用包装器方法:

function commandDatabaseStep()
  return commandDatabase()
end

... but this did not work either (same error) ...但这也不起作用(同样的错误)

The relevant code:相关代码:


-- System command
function commandSystem()
  ...
end

-- Database command
function commandDatabase()

  if arguments[2] == "no_arg1" then
    print("The 'database' command must have at least one argument: [generate, wipe, dump, delete, get <system_name>]", true)
    return 2
    
  elseif arguments[2] == "generate" then
    local file = io.open(database, "w+")
    file:write("#ssmhub database")
    file:close(file)
    return 1

  elseif arguments[2] == "dump" then
    print("= DUMP START =")
    for line in io.lines(database) do
      print(line)
    end
    print("= DUMP  END  =")
    return 1

  -- 1+
  elseif arguments[2] == "get" then
  
    -- 2+
    if isEmpty(arguments[3]) then
      print("The 'database get' command must have a <name> parameter")
      return 0 
      -- 2-
    else -- 3+
      local file = io.open(database, "r")
      
      for line in io.lines(file) do -- 4+
        local state = ""
        local id = ""
        local dividersFound = 0

        line:gsub(".", function(c) -- 5+
                 
          if c == "|" then -- 6+
            
            if dividersFound == 0 then -- 7+
              state = state .. c
            end -- 7-
            if dividersFound == 1 then -- 8+
              id = id .. c
            end -- 8-
            dividersFound = dividersFound + 1
            
          end -- 6-
          
        end) -- 5-

        io.close(file)

      end -- 4-
    end -- 3-
    else  -- 9+
      print("Illegal argument for command. Use 'help' for a list of commands and arguments.")
      return 0
    end -- 9-
  end -- 2-
end -- 1-

function commandHelp()
  ...
end

-- Main
function main()
  arguments = readProgramArguments()

  commandArgument = arguments[1]

  commandCompleteCode = 0

  -- Process help and system commands

  if commandArgument == "database" then
    commandCompleteCode = commandDatabase()
  end
end main()

As @luther pointed out, I had one-too-many end-s in commandDatabase .正如@luther 指出的那样,我在commandDatabase中有一个太多的结尾。

This wasn't flagged in my IDE because I had not end-ed commandSystem , so commandSystem was nested inside of it.这没有在我的 IDE 中标记,因为我没有结束commandSystem ,所以commandSystem嵌套在其中。

To fix: add an end to commandSystem , and remove the end which I tagged '-- 1-'.修复:在commandSystem添加一个结尾,并删除我标记为“-- 1-”的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM