lua require 是如何工作的
我正在使用一个图形库,可以让你用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Astar 如何添加到示例脚本的
package.loaded
表中,而不是您的代码?当您使用
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?When you run a script using
require('Astar')
, this is what it does:package.loaded['Astar']
is a non-nil value.package.path
(andpackage.cpath
), with '?' replaced with 'Astar', until it finds the first file matching the pattern.package.loaded['Astar']
totrue
.'Astar'
as an argument (accessible as...
in the module script).package.loaded['Astar']
.package.loaded['Astar']
are returned.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).您需要在
Astar.lua
末尾添加return Astar
。You need to add
return Astar
at the end ofAstar.lua
.