lua require 是如何工作的

发布于 2024-11-27 08:50:10 字数 1047 浏览 0 评论 0原文

我正在使用一个图形库,可以让你用 Lua 进行编程。我需要 A* 寻路库,所以我在网上找到了一个。它只有 1 个用于寻路的 lua 文件和 1 个示例文件。在示例文件中,它使用如下对象:

-- Loading the library
local Astar = require 'Astar'
Astar(map,1) -- Inits the library, sets the OBST_VALUE to 1

我运行脚本,一切正常。所以现在我将 Astar.lua 文件添加到图形引擎运行的路径位置并执行相同的操作,然后我在 Astar(map, 1) 行上收到错误:

“尝试调用本地‘AStar’(一个数字)值)

有什么想法为什么当我执行与此 AStar 库附带的示例相同的操作时会出现该错误?

这是 AStar 文件的一些内容

-- The Astar class
local Astar = {}
setmetatable(Astar, {__call = function(self,...) return self:init(...) end})
Astar.__index = Astar

-- Loads the map, sets the unwalkable value, inits pathfinding
function Astar:init(map,obstvalue)
    self.map = map
        self.OBST_VALUE = obstvalue or 1
    self.cList = {}
    self.oList = {}
    self.initialNode = false
    self.finalNode = false
    self.currentNode = false
    self.path = {}
    self.mapSizeX = #self.map[1]
    self.mapSizeY = #self.map
end

所以请注意,当我从图形引擎运行它时,它会返回1、但是运行时从它附带的示例来看,它返回一个表,这就是它应该返回的内容,所以不确定为什么它只会返回 1。

I'm using a graphics library that lets you program in Lua. I have a need for the A* pathfinding library so I found one online. It's just 1 lua file that does the pathfinding and 1 example file. In the example file it uses the object like:

-- Loading the library
local Astar = require 'Astar'
Astar(map,1) -- Inits the library, sets the OBST_VALUE to 1

I run the script and everything works. So now I add the Astar.lua file to the path location where my graphics engine is running and do the same thing and I get the error on the Astar(map, 1) line:

"attempt to call local 'AStar' (a number value)

Any ideas why I would be getting that error when I'm doing the same thing as the example that comes with this AStar lib?

Here is a little of the AStar file

-- The Astar class
local Astar = {}
setmetatable(Astar, {__call = function(self,...) return self:init(...) end})
Astar.__index = Astar

-- Loads the map, sets the unwalkable value, inits pathfinding
function Astar:init(map,obstvalue)
    self.map = map
        self.OBST_VALUE = obstvalue or 1
    self.cList = {}
    self.oList = {}
    self.initialNode = false
    self.finalNode = false
    self.currentNode = false
    self.path = {}
    self.mapSizeX = #self.map[1]
    self.mapSizeY = #self.map
end

So note that when I run this from my graphics engine it's returning 1, but when run from the example that it came with it's returning a table, which is what it should be returning. So not sure why it would only be returning 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-12-04 08:50:10

Astar 如何添加到示例脚本的 package.loaded 表中,而不是您的代码?

LUA 语法糖快速回顾:

  • func 'string' 相当于 func('string')
  • tabl.ident 相当于 tabl['ident']

当您使用 require('Astar') 运行脚本时,它会执行以下操作:

  • 检查 package.loaded['Astar'] 是一个非零值。
    • 如果是,则返回该值。否则,它将继续沿着该列表向下移动。

  • 运行package.path(和package.cpath)中列出的模式的文件名,带有“?”替换为“Astar”,直到找到第一个与该模式匹配的文件。
  • package.loaded['Astar'] 设置为 true
  • 使用 'Astar' 作为参数(可通过 ... 访问)运行模块脚本(通过上面的路径搜索找到 - 在本示例中我们假设它不是 C 模块) 在模块脚本中)。
  • 如果脚本返回一个值,则该值将被放入 package.loaded['Astar'] 中。
  • 返回package.loaded['Astar']的内容。
    • 请注意,脚本可以将包加载到 package.loaded['Astar'] 作为其执行的一部分,并且不返回任何内容。

正如有人在上面的评论中指出的那样,您的问题可能来自使用“AStar”而不是“Astar”加载模块。 Lua 可能正在使用此字符串加载此脚本(因为在不区分大小写的 Windows 上,搜索名为“AStar.lua”的文件将打开名为“Astar.lua”的文件),但该脚本不是进行操作(通过使用硬编码的“Astar”而不是Lua加载脚本的“AStar”)。

How is Astar getting added to the package.loaded table for the example script, as opposed to your code?

QUICK LUA SYNTACTIC SUGAR REVIEW:

  • func 'string' is equivalent to func('string')
  • tabl.ident is equivalent to tabl['ident']

When you run a script using require('Astar'), this is what it does:

  • checks if package.loaded['Astar'] is a non-nil value.
    • If it is, it returns this value. Otherwise it continues down this list.
  • Runs through filenames of the patterns listed in package.path (and package.cpath), with '?' replaced with 'Astar', until it finds the first file matching the pattern.
  • Sets package.loaded['Astar'] to true.
  • Runs the module script (found via path search above- for the sake of this example we'll assume it's not a C module) with 'Astar' as an argument (accessible as ... in the module script).
  • If the script returns a value, this value is placed into package.loaded['Astar'].
  • The contents of package.loaded['Astar'] are returned.
    • Note that the script can load the package into package.loaded['Astar'] as part of its execution and return nothing.

As somebody noted in the comments above, your problem may come from loading the module using 'AStar' instead of 'Astar'. It's possible that Lua is loading this script using this string (since, on the case-insensitive Windows, a search for a file named "AStar.lua" will open a file called "Astar.lua"), but the script isn't operating with that (by using a hard-coded "Astar" instead of the "AStar" Lua is loading the script under).

若水般的淡然安静女子 2024-12-04 08:50:10

您需要在Astar.lua末尾添加return Astar

You need to add return Astar at the end of Astar.lua.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文