如何从另一个文件中为 windows 和 linux 包含脚本(变量、函数等)?

How to include a script (variables, functions etc.) from another file for windows and linux?

名为 first.lua 的第一个文件具有:

var1 = 1
var2 = 2
var3 = 3

function first(var4)
print(var4)
return true
end

名为 second.lua 的第二个文件应具有:

if var1 == 1 and var2 == 2 and var3 == 3 then
   first('goal')
end

如何在 windows 和 linux 上包含这些变量和函数?在 PHP 中,我使用 require 或 include 路径文件,但在 lua 中?谢谢

假设这两个文件在同一个目录下,你要访问的变量应该是全局的或者returned

第一种方式

文件 1 名为 first.lua

      variable1 = "YOU CAN SEE ME !"
local variable2 = "YOU CANNOT !"

文件 2 名为 second.lua

require("first") --> If file first.lua is in directory directName existing in the same area
                 --> as the file second.lua, use "directName/first"
                 --> if it's in the folder outside the current one, you can 
                 --> do ../first and so on it goes
print(variable1) --> This will print : YOU CAN SEE ME
print(variable2) --> This will print nil because variable2 does not exist in file2, it is local

第二种方式

使用这个,你可以简单地return一个table在文件的末尾

文件 1 名为 first.lua

local to_access           = {} 
      to_access.variable1 = "HALLALOUJA"
local impossible_to_get   = "Hello"
return to_access               --> Note this return is very important

文件 2 名为 second.lua

accessed = require("first")    --> or the path you use, without having .lua ofcourse
print    ( accessed.variable1 )--> This will print "HALLALOUJA"!
print    ( impossible_to_get  )--> Dear ol' nil will be printed