gpt4 book ai didi

lua - 访问其他文件上的 Lua 函数

转载 作者:行者123 更新时间:2023-12-03 01:25:52 27 4
gpt4 key购买 nike

我想将我的 Lua 文件分成两个文件:一个保存我的函数,另一个可以调用它们。我对此做了很多研究,但我找到的所有资源都没有非常深入地解释这个过程。他们通常会说使用:

require "subsystem.lua"

我将其放在保存到同一目录的新 Lua 文件的顶部,但无法访问任何内容。

有需要修改的配置文件吗?它在哪里?

最佳答案

需要模块

require函数会在一系列位置中查找以查找模块。可以通过调整全局表package中的字段来广泛自定义地点的确切列表。

了解在查找模块时使用了多少位置和名称的最简单方法是查看 require 失败时生成的错误消息。例如,在我的电脑上,Lua 5.1 是这样说的:

C:\Users\Ross>luaLua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio> require "xyzzy"stdin:1: module 'xyzzy' not found:        no field package.preload['xyzzy']        no file '.\xyzzy.lua'        no file 'C:\Program Files (x86)\Lua\5.1\lua\xyzzy.lua'        no file 'C:\Program Files (x86)\Lua\5.1\lua\xyzzy\init.lua'        no file 'C:\Program Files (x86)\Lua\5.1\xyzzy.lua'        no file 'C:\Program Files (x86)\Lua\5.1\xyzzy\init.lua'        no file 'C:\Program Files (x86)\Lua\5.1\lua\xyzzy.luac'        no file '.\xyzzy.dll'        no file '.\xyzzy51.dll'        no file 'C:\Program Files (x86)\Lua\5.1\xyzzy.dll'        no file 'C:\Program Files (x86)\Lua\5.1\xyzzy51.dll'        no file 'C:\Program Files (x86)\Lua\5.1\clibs\xyzzy.dll'        no file 'C:\Program Files (x86)\Lua\5.1\clibs\xyzzy51.dll'        no file 'C:\Program Files (x86)\Lua\5.1\loadall.dll'        no file 'C:\Program Files (x86)\Lua\5.1\clibs\loadall.dll'stack traceback:        [C]: in function 'require'        stdin:1: in main chunk        [C]: ?> 

After looking internally, the first "real" place it looks for xyzzy is in the file named .\xyzzy.lua. Then it tries a number of folders and names in the folder where lua.exe was found. Finally, it looks for a DLL that might offer it. The list of folders it searches for a .lua file is controlled by the string value in package.path. (The comparable list for DLLs in is package.cpath.) In that value, require will replace each ? with the module name, and then attempt to read the file. The first one to succeed is used.

(The story here is slightly more complicated; you can create "searchers" that require will use to look in different places, and even change the order of the built-in searchers, but that is an advanced topic.)

So just putting modules in Lua files in the current directory should work just fine, and adjusting package.path before calling require for your own modules can cover most quirks you will encounter.

Create a module to require

At its simplest, a module is just something that can be stored in package.loaded. Which is what require will do with it once it has been found, so that multiple calls to require will only search once and always return the same value.

The traditional answer is for that "something" to be a table, usually mostly populated by functions that can be called, and occasionally having values. The math module is a good example: it provides lots of functions like sin and cos, along with the useful value math.pi and math.huge.

Aside from being stored in a table, there is nothing special about a function in a module. Like any other function, it takes parameters and return zero or more values. The only real rule is that a module should not change or add global variables.

So a very minimal module file can be as simple as:

return { 
addtwo = function(a, b) return a+b end,
subtwo = function(x) return x-2 end,
}

如果存储为 example.lua 可以这样使用:

local example = require "example"
print(example.addtwo(2,2)) -- 4
print(example.subtwo(42)) -- 40

更好的模块骨架

在大多数实际情况下,将所有代码粘贴在单个表声明中是行不通的。它的扩展性不好,并且很难清楚地表达共享状态的函数之间的关系。

-- simple modules

-- capture the name searched for by require
local NAME=...

-- table for our functions
local M = { }

-- A typical local function that is also published in the
-- module table.
local function addtwo(a,b) return a+b end
M.addtwo = addtwo

-- Shorthand form is less typing and doesn't use a local variable
function M.subtwo(x) return x-2 end

return M

为什么不调用 module() 函数?

Lua 5.1 包含一个名为 module() 的标准函数,旨在在模块实现的顶部使用。它的使用从来都不是必需的,而且很快就形成了共识,认为它并不像希望的那样有用。此后它已被弃用。

因此,我上面展示的简单框架没有使用它,并且可以移植到自 5.1 以来的所有 Lua 版本。

关于lua - 访问其他文件上的 Lua 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com