【发布时间】:2020-09-28 21:48:24
【问题描述】:
如何才能使用这个模块?
文件:Player.lua,我的模块
local Player = {}
local function funcPlayer()
local player = {}
local self = {name="", points = 0}
local setPoints = function(newPoints)
self.points = newPoints
end
local getPoints = function ()
return self.points
end
local setName = function(newName)
self.name = newName
end
local getName = function()
return self.name
end
local f__tostring = function()
return "(" .. self.name .. ", " .. self.points .. ")"
end
setmetatable(player, {
__tostring = f__tostring
})
player.getPoints = getPoints
player.setPoints = setPoints
player.getName = getName
player.setName = setName
return player
end
Player = funcPlayer()
return Player
请求模块的文件:Players.lua
local tabPlyer = require("Player")
错误信息:
[Running] lua "c:\Users\tct9\Documents\Lua\ScriptLua\Games\Players.lua"
lua: c:\Users\tct9\Documents\Lua\ScriptLua\Games\Players.lua:1: module 'Player' not found:
no field package.preload['Player']
no file 'c:\Users\tct9\Documents\Lua\lua\Player.lua'
no file 'c:\Users\tct9\Documents\Lua\lua\Player\init.lua'
no file 'c:\Users\tct9\Documents\Lua\Player.lua'
no file 'c:\Users\tct9\Documents\Lua\Player\init.lua'
no file '.\Player.lua'
no file 'c:\Users\tct9\Documents\Lua\Player.dll'
no file 'c:\Users\tct9\Documents\Lua\loadall.dll'
no file '.\Player.dll'
no file 'c:\Users\tct9\Documents\Lua\Player52.dll'
no file '.\Player52.dll'
stack traceback:
[C]: in function 'require'
c:\Users\tct9\Documents\Lua\ScriptLua\Games\Players.lua:1: in main chunk
[C]: in ?
如何才能使用这个模块?
重要提示
我正在使用 VSCode。
PATH 变量:...; C:\Users\tct9\Documents\Lua
变量LUA_PATH:C:\Users\tct9\Documents\Lua
月球路径:C:\Users\tct9\Documents\Lua
脚本路径:C:\Users\tct9\Documents\Lua\ScriptLua\Games
【问题讨论】:
-
Player.lua 保存在哪里?
-
我做到了。跟着书《Programming in Lua 4th edition》中的章节,报错是一样的。
-
如果
Player.lua与Players.lua位于同一文件夹中,您也可以使用dofile"Player.lua"代替require。 -
我使用了dofile,但错误仍然存在。 “无法打开播放器:没有这样的文件或目录”。