pcall

用途: 类似 try catch, 返回 retcode, retvalue 参考:

_G

用途: 存储全局变量, 以 table 类型存储数据 参考: lua _G 表_程序员黄老师的博客-CSDN博客

table

[] inside table

参考: What is the function of square brackets around table keys in lua? - Stack Ove…

作用: 通过 [] 指定长 key, 类似 python 中 dict 的 ":"。

正确用法:

1
2
3
4
5
6
7
8
variable = "a key"
variable2 = "another key"

mytable = {
  ["long key"] = 23,
  [variable] = 22,
  variable2 = 21,
}

错误用法:

1
2
3
4
5
6
7
mytable = {
  "key" = 3,
}

mytable2 = {
  my key = 4,
}

module 模块

格式: 可以使用 "/" 也可以使用 "." 分割

1
2
3
require('other_modules.anothermodule')
-- or
require('other_modules/anothermodule')

string 类型

参考: Strings Tutorial

字符串格式:

  • ''
  • ""
    • 特点

      • 允许输入: ', "
      • 允许转义
      • 允许换行
      • 允许嵌套 [[, ]]

        1
        2
        3
        4
        5
        6
        7
        8
        
        > = [[one [[two]] one]]        -- bad
        stdin:1: nesting of [[...]] is deprecated near '['
        > = [=[one [[two]] one]=]      -- ok
        one [[two]] one
        > = [===[one [[two]] one]===]  -- ok too
        one [[two]] one
        > = [=[one [ [==[ one]=]       -- ok. nothing special about the inner content.
        one [ [==[ one

拼接

通过 ".." 符号