模块未找到 - "xxx模块未找到" - Lua langue

问题描述 投票:0回答:1

怎么做才能使用这个模块?

文件: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 (用户) Documents (文件) Documents (文件)

变量LUA_PATH:C:用户/ct9/文件/Lua。

月球之路。C: \ Users * Documents * Lua

脚本路径。C: \ Users (用户) Documents (文件) Documentation (游戏)

module lua require
1个回答
2
投票

[Running] lua "c:\Users\tct9/Documents/Lua\ScriptLua/Games/Players.lua" lua:c:\Users\tct9/Documents/Lua\ScriptLua/Games/Players. lua:1: 模块'玩家'没有找到:没有字段package.preload['玩家']没有文件'c:Users/tct9/Documents/Lua/lua/Player.lua' 没有文件'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/Playerinit.lua' no file '.Player.lua' no file 'c:Users/tct9/Documents/Lua/Player. dll" 没有文件 'c:Userstct9/Documents/Lua/loadall.dll' 没有文件 '.Player.dll' 没有文件 'c:Userstct9/Documents/Lua/Player/52.dll' 没有文件 '.Player52.dll

是Lua试图找到的文件列表。如果您的Player.lua不在其中,您必须确保该文件在列出的路径中,或者您将包含您的文件的路径添加至 package.path 在需要它之前。

可以从您的脚本中手动添加,也可以将其添加到 LUA_PATH


0
投票

我做了。

print (package.path)

我发现 "C:Users\tct9\Documents\Lua?"

然后我添加到package.path。

package.path = package.path .. "; C:\\Users\\tct9\\Documents\\Lua\\ScriptLua
\\Games\\?. lua"

然后模块就正常工作了!

© www.soinside.com 2019 - 2024. All rights reserved.