Lua格式讲解

firstValue = "This is a string value"; -- 这是一个变量的定义,变量定义不需要任何标记,这个是全局变量
print("helloWorld"); -- 标准输出语句

-- 这是一个方法体,从function开始,到end结束
function firstFun () -- Local标记的是方法内的局部变量,作用域仅在方法体内
local fitstValue = "In firstFun, print the same name only print the local one!";
print(fitstValue); -- 这里打印的fitstValue如果存在局部变量则优先输出局部变量,无局部变量才输出全局变量
end

function second()
local a=10;
local b=20; --局部变量。不可以定义为local a=10,b=20。而local d , f = 5 ,10可以
d , f = 5, 10; --全局变量。等同 global d , f = 5, 10;

print(a+b);
end

-- firstFun(); -- 这里调用fitstFun方法
second();

print(firstValue); -- 输出全局变量fitstValue

原文地址:https://www.cnblogs.com/gd-luojialin/p/10962690.html