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

时间:2016-07-03 17:14:23

标签: function variables lua include require

名为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或包含路径文件,但在lua?感谢。

1 个答案:

答案 0 :(得分:0)

假设这两个文件位于同一目录中,您要访问的变量应为全局或返回

第一种方式

FILE 1名为first.lua

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

FILE 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

第二种方式

使用这个,您只需返回文件末尾的表格

FILE 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