游戏开发学习笔记——lua脚本语言——第一天(基本用法、变量、数据类型、运算符)

游戏开发学习笔记——lua脚本语言——第一天(基本用法、变量、数据类型、运算符)
FOR THE SIGMA
FOR THE GTINDER
FOR THE ROBOMASTER

简介:

第一次——参考易百教程——lua教程快速学习 (学习内容建立在阅读者具备基础的C或是C++知识)

操作系统版本:Windows10
软件:SciTE

更新:

2019.10.28 更新已知的几种注释方法

内容:

令牌标记与注释

Lua程序由各种标记组成,标记可以是关键字,标识符,常量,字符串文字或符号。 例如,以下Lua语句由三个标记组成

#三个标记组成的关联
io.write("Hello world, from ",_VERSION,"!\n")

在这里插入图片描述
可以看出来的是后面的标记可以跟关键字、标识符、常量、字符串文字或符号。

在注释上Lua跟C、C++、Python都不一样,它们通常以–[[ 开始 ,以–]] 结束,如:
在这里插入图片描述

--[ 则是第一种,仅限用于一段 --]
--[[ 则是第二种,可以用于一段或者回车另起一行 --]]
--[[ 则是第三种,功能同上 ]]
标识符、关键字、空白符

Lua标识符是用于标识变量,函数或任何其他用户定义项的名称。在规则上lua的标识符与C/C++几乎一样(包括区分大小写等),唯一不同的是lua不允许标识符中出现标点符号(如@、$、%等)

lua关键字几乎跟C/C++一样,但是多了一个then

andbreakdoelse
elseifendfalsefor
functionifinlocal
nilnotorrepeat
returnthentrueuntil
while

lua空白符只包含空格(可能带有注释)的行称为空行,Lua解释器完全忽略它,其跟C/C++完全一样
例如 a = b + c

变量范围与定义声明

变量是程序可以操作的存储区域的名称。 它可以包含不同类型的值,包括函数和表。
变量的名称可以由字母,数字和下划线字符组成。 它必须以字母或下划线开头。 大写和小写字母是不同的,因为Lua区分大小写。Lua中有八种基本类型的值。
在Lua中,虽然没有可变数据类型,但根据变量的范围有三种类型(C++里也有)

  • 全局变量 —— 所有变量都被视为全局变量,除非明确声明为局部变量
  • 局部变量 —— 当为变量指定类型为local时,其范围受函数范围限制
  • 表字段 —— 一种特殊类型的变量,可以保留除nil以外的任何内容,包括函数

变量定义告诉解释器为变量创建存储的位置和数量,甚至可以在其上面赋值也行,常见模板

--[[type variable_list--]]
local i , j --[[解析器创建i与j变量,并将范围限制在局部
local i , j = 5 , 10 --[[i为5 , j为10--]]

注意:对于没有初始化程序的定义,若是具有静态储存持续时间的变量使用nil来进行隐式初始化

变量声明,当变量定义后需要在下面中进行声明才能实现

--[[ 定义--]]
local a, b
--[[初始化--]]
a = 10
b = 30
print("value of a:", a)
print("value of b:", b)

--[[变量的交换--]]
b, a = a, b
print("value of a:", a)
print("value of b:", b)

f = 70.0/3.0
print("value of f", f)

lua左值与右值与C++一样,左值通常是一个变量,而右值通常是一个用于赋值的数值或是一段字符串,例如

g , l = 2030 

即将20分给g,将30分给l

数据类型

Lua是一种动态类型语言,因此变量没有类型,只有值具有类型。 值可以存储在变量中,作为参数传递并作为结果返回。
在Lua中,虽然没有可变数据类型,但有值的类型。 值的数据类型列表如下。

值类型描述
nil用于区分值与某些数据或没有(nil)数据
boolean包括true和false作为值,通常用于检测
number表示实数(双精度浮点)
string表示字符数组
function表示用C或是Lua编写的方法
userdate表示任意C语言数据
thread表示独立的执行线程,它用于实际相同程序
table表示普通数组,符号表,集合,记录,图形,树等,并实现关联数组。 它可以保存任何值(除了nil)

类型函数

print(type("What is my type"))   --> string
t = 10

print(type(5.8*t))               --> number
print(type(true))                --> boolean
print(type(print))               --> function
print(type(nil))                 --> nil
print(type(type(ABC)))           --> string

默认情况下,在分配值或初始化之前,所有变量都将指向nil。 在Lua中,在条件检查的情况下,零和空字符串认为是:true。 因此,使用布尔运算时必须小心。

运算符
  • 算数运算符
a = 21
b = 10
c = a + b

print("Line 1 - Value of c is ", c )
c = a - b
print("Line 2 - Value of c is ", c )
c = a * b
print("Line 3 - Value of c is ", c )
c = a / b
print("Line 4 - Value of c is ", c )
c = a % b
print("Line 5 - Value of c is ", c )
c = a^2
print("Line 6 - Value of c is ", c )
c = -a
print("Line 7 - Value of c is ", c )
  • 关系运算符
a = 21
b = 10

if( a == b )
then
   print("Line 1 - a is equal to b" )
else
   print("Line 1 - a is not equal to b" )
end

if( a ~= b )
then
   print("Line 2 - a is not equal to b" )
else
   print("Line 2 - a is equal to b" )
end

if ( a < b )
then
   print("Line 3 - a is less than b" )
else
   print("Line 3 - a is not less than b" )
end

if ( a > b ) 
then
   print("Line 4 - a is greater than b" )
else
   print("Line 5 - a is not greater than b" )
end

-- Lets change value of a and b
a = 5
b = 20

if ( a <= b ) 
then
   print("Line 5 - a is either less than or equal to  b" )
end

if ( b >= a ) 
then
   print("Line 6 - b is either greater than  or equal to b" )
end
  • 逻辑运算符
a = 5
b = 20

if ( a and b )
then
   print("Line 1 - Condition is true" )
end

if ( a or b )
then
   print("Line 2 - Condition is true" )
end

--lets change the value ofa and b
a = 0
b = 10

if ( a and b )
then
   print("Line 3 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end

if ( not( a and b) )
then
   print("Line 4 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end
  • 其他运算符
    以上算数、关系、逻辑均与C++里面的一样

lua语言支持连接与长度

编号描述示例
连接两个字符串如果a为Hello,b为World,a…b将返回Hello World。
#返回字符串或表长度的一元运算符#“Hello” 将返回 5
a = "Hello "
b = "World"

print("Concatenation of string a with b is ", a..b )
print("Length of b is ",#b )
print("Length of b is ",#"Test" )

总结:个人感觉lua更像是C/C++与Python的结合,既有C的结构,又有PY的简洁,例如尾部冒号的类的可以不用谢,若先入主C的话,看lua就跟看Python一样

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值