@lisaisacat
2020-02-01T14:38:02.000000Z
字数 3377
阅读 2117
未分类
在此输入正文
函数名称:pcall 保护模式调用
函数功能:脚本异常处理。
函数方法
bool,msg = pcall(func,....)
| 参数 | 类型 | 说明 |
|---|---|---|
| fun | function | 需要调用的函数 |
| a | - | 需要传入 fun 的参数 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| bool | boolean | true - 无错误,false - 错误 |
| msg | - | bool 为 true 时,返回 fun 的值;bool 为 false 时,返回错误信息 |
脚本实例:
function a(str)return strendbool,msg = pcall(a,"b")
函数名称:require 加载模块库
函数功能:加载函数库及 lua 文件
函数方法
ver = require(name)
| 参数 | 类型 | 说明 |
|---|---|---|
| name | strig | 需要加载的函数库或者 lua 文件名称 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| ver | - | 加载库获取的返回值 |
函数名称:type 判断数据类型
函数功能:判断数据类型
函数方法
ty = type(var)
| 参数 | 类型 | 说明 |
|---|---|---|
| var | - | 需要判断类型的数据 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| ty | string | number - 数字,string - 字符串,nil - 空值,boolean- 布尔值,table - 数组,function - 函数,thread - 线程,userdata - 任意数据类型 |
脚本实例:
ty = type("1")dialog(ty)
函数名称:tonumber 将字符串转换成数字
函数功能:将 string 格式内容转成 number 格式内容
函数方法
num = tonumber(str)
| 参数 | 类型 | 说明 |
|---|---|---|
| str | string | string 格式的内容 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| num | number | 转换后的 number 格式内容 |
脚本实例:
local str="1"local num = tonumber(str)dialog(type(num))
函数名称:tostring 将数字转成字符串
函数功能:将 number 格式内容转成 string 格式内容
函数方法
local str = tostring(num)
| 参数 | 类型 | 说明 |
|---|---|---|
| num | number | number 格式内容 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| str | string | 转换后的 string 格式内容 |
脚本实例:
local num = 1local str = tostring(num)dialog(type(str))
函数名称:io.open 操作文件
函数功能:打开文件
函数方法
file,msg = io.open(path,mode)
| 参数 | 类型 | 说明 |
|---|---|---|
| path | string | 需要操作的文件路径 |
| mode | string | 打开模式 |
mode 参数介绍
| 参数 | 类型 | 说明 |
|---|---|---|
| r | string | 以只读方式打开文件,该文件必须存在 |
| w | string | 打开只写文件,若文件存在则文件长度清为 0,即该文件内容会消失。若文件不存在则建立该文件 |
| a | string | 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾部,即文件原先的内容会被保留 |
| r+ | string | 以读写方式打开,该文件必须存在,保留原有数据 |
| w+ | string | 以读写方式打开,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件 |
| a+ | string | 以读写方式打开,保留原有数据,只能在文件末尾添加,不能在文件中间改写数据 |
| b | string | 二进制模式,如果文件是二进制文件,可以加上 b |
| + | - | 对文件既可以读也可以写 |
| 返回值 | 类型 | 说明 |
|---|---|---|
| file | userdata | 打开成功返回文件句柄,失败返回 nil |
| msg | string | 失败返回失败原因 |
脚本实例:
--文件存在则清空文件内容,文件不存在则创建文件local file,msg = io.open(userPath().."/res/1.txt","w")if file thendialog("打开成功")file:close()elsedialog("打开失败,原因:" .. msg)end
函数名称:io.close 关闭文件句柄
函数功能:关闭文件句柄
函数方法
file:close()
函数名称:io.read 读入默认文件
函数功能:读入默认文件
函数方法
file,msg = io.read(flag)
| 参数 | 类型 | 字段 | 说明 |
|---|---|---|---|
| flag | string | 选填 | 读取模式,不写默认为按行读取 |
| 参数 | 类型 | 说明 |
|---|---|---|
| "*a" | string | 读取全部内容 |
| "/" | string | 按行读入,读取下一个行内容,如果在文件尾部则会返回 nil,默认方式 |
| "n" | string | 读取一个数字,这是唯一返回数字而不是字符串的读取格式 |
| "number" | string | 读取 number 个字符的字符串,如果在文件尾则会返回 nil,如果吧 number = 0,则这个函数不会读取任何内容而返回一个空串 "",在文件尾返回 nil |
| 返回值 | 类型 | 说明 |
|---|---|---|
| file | userdata | 打开成功返回文件句柄,失败返回nil |
| msg | string | 失败返回失败原因 |
脚本实例:
-- 打开文件local file = io.open(userPath().."/res/1.txt","r")if nil == file thendialog("open file readtest.txt fail")end-- 读取数字local year = file:read("*n")local month = file:read("*n")local day = file:read("*n")local hour = file:read("*n")dialog("year = "..year)dialog("month = "..month)dialog("day = "..day)dialog("hour = "..(hour or "nil"))-- 读取行local content = file:read("*l")dialog("\ncontent = "..content)-- 按行读取local content2 = file:read("*l")dialog("content2 = "..content2)-- 读取0个字节local zerobyte = file:read(0)dialog("\nzerobyte = "..zerobyte)-- 读取6个字节local sixbyte = file:read(6)dialog("sixbyte = "..sixbyte)-- 读取所有内容local readall = file:read("*a")dialog("\nreadall = "..readall)-- 文件结尾读取所有内容local readallagain = file:read("*a")dialog("readallagain = "..readallagain)-- 文件结尾读取行local reademptyline = file:read("*l")if reademptyline == nil thendialog("\nread the end of file")end-- 文件尾读取 0 个字节local zerobyteagain = file:read(0)if zerobyteagain == nil thenprint("read the end of file")endfile:close()
函数名称:io.write 写入参数到文件
函数功能:写入参数到文件
函数方法
io.write(mode)
| 参数 | 类型 | 说明 |
|---|---|---|
| mode | string | 需要写入的内容 |
|file|userdata|写入成功返回文件句柄,失败返回 nil|
|msg|string|写入失败返回失败原因|
脚本实例:
-- 打开文件local file = io.open(userPath().."/res/writetest.txt", "w")if nil == file thenprint("open file writetest.txt fail")end-- 输入字符串file:write("test io.write\n");-- 输入数字file:write(2016)-- 输入分隔符file:write(" ")-- 继续输入数字file:write(7)file:write(" ")file:write(23)file:write("\n")-- 继续输入其他类型file:write(tostring(os.date()))file:write("\n")file:write(tostring(file))fileread:close()
函数名称:require 加载模块
函数功能:加载函数库及 lua 文件
函数方法
require()
| 参数 | 类型 | 说明 |
|---|---|---|
| fun | function | 需要调用的函数 |
| 返回值 | 类型 | 说明 |
|---|
脚本实例: