Lua的API函数
1. 基础库
我们在整个教程中使用了各种主题下的基本库。 下表提供了相关页面的链接,并列出了本Lua教程各部分所涵盖的功能。
编号 |
库/方法 |
作用 |
1 |
错误处理 |
包括错误处理函数,如断言, 错误,如Lua错误处理中所述。 |
2 |
内存管理 |
包括与垃圾收集相关的自动内存管理功能, 如Lua垃圾收集中所述。 |
3 |
|
它打开文件并以块的形式执行文件的内容。 |
4 |
|
因此是保存全局环境的全局变量(即 |
5 |
|
返回函数使用的当前环境。 |
6 |
|
如果 |
7 |
|
此函数获取表的索引和值。 |
8 |
|
使用函数 |
9 |
|
与 |
10 |
|
与 |
11 |
|
允许程序遍历表的所有字段。 |
12 |
|
暂停正在运行的协同程序。 |
13 |
|
打印给定的参数值。 |
14 |
|
检查 |
15 |
|
获取 |
16 |
|
将 |
17 |
|
如果 |
18 |
|
设置给定函数使用的环境。 |
19 |
|
设置给定表的元表。 |
20 |
|
尝试将参数转换为数字。 |
21 |
|
接收任何类型的参数并将其转换为合理格式的字符串。 |
22 |
|
返回唯一参数的类型,编码为字符串。 |
23 |
|
返回给定表中的元素。 |
24 |
|
包含当前解释器版本的字符串的全局变量(不是函数)。 |
25 |
协同程序 |
包括Lua协同程序中解释的协程操作功能。 |
2. Lua数学库
编号 |
库或方法 |
描述 |
1 |
|
返回 |
2 |
|
返回 |
3 |
|
返回 |
4 |
|
返回 |
5 |
|
返回 |
6 |
|
返回大于或等于 |
7 |
|
返回 |
8 |
|
返回 |
9 |
|
以度为单位返回角度 |
10 |
|
返回值 |
11 |
|
返回小于或等于 |
12 |
|
返回 |
13 |
|
返回 |
14 |
|
|
15 |
|
返回 |
16 |
|
返回 |
17 |
|
返回 |
18 |
|
返回参数中的最大值。 |
19 |
|
返回参数中的最小值。 |
20 |
|
返回两个数字, |
21 |
|
|
22 |
|
返回 |
23 |
|
以弧度为单位返回角度 |
24 |
|
此函数是ANSI C提供的简单伪随机生成器函数rand的接口。 |
25 |
|
将 |
26 |
|
返回 |
27 |
|
返回 |
28 |
|
返回 |
29 |
|
返回 |
30 |
|
返回 |
三角函数
使用三角函数的简单示例如下所示-
radianVal
=math
.rad(math
.pi
/2)
io
.write(radianVal
,"\n")
-- Sin value of 90(math.pi / 2) degrees
io
.write(string
.format("%.1f ",math
.sin(radianVal
)),"\n")
-- Cos value of 90(math.pi / 2) degrees
io
.write(string
.format("%.1f ",math
.cos(radianVal
)),"\n")
-- Tan value of 90(math.pi / 2) degrees
io
.write(string
.format("%.1f ",math
.tan(radianVal
)),"\n")
-- Cosh value of 90(math.pi / 2) degrees
io
.write(string
.format("%.1f ",math
.cosh(radianVal
)),"\n")
-- Pi Value in degrees
io
.write(math
.deg(math
.pi
),"\n")
当运行上面的程序时,将得到以下输出 -
0.0274155677808040.01.0
0.0
1.0
180
其他常见的数学函数
使用常见数学函数的简单示例如下所示-
-- Floor
io
.write("Floor of 10.5055 is ",math
.floor(10.5055),"\n")
-- Ceil
io
.write("Ceil of 10.5055 is ",math
.ceil(10.5055),"\n")
-- Square root
io
.write("Square root of 16 is ",math
.sqrt(16),"\n")
-- Power
io
.write("10 power 2 is ",math
.pow(10,2),"\n")
io
.write("100 power 0.5 is ",math
.pow(100,0.5),"\n")
-- Absolute
io
.write("Absolute value of -10 is ",math
.abs(-10),"\n")
--Random
math
.randomseed(os
.time())
io
.write("Random number between 1 and 100 is ",math
.random(),"\n")
--Random between 1 to 100
io
.write("Random number between 1 and 100 is ",math
.random(1,100),"\n")
--Max
io
.write("Maximum in the input array is ",math
.max(1,100,101,99,999),"\n")
--Min
io
.write("Minimum in the input array is ",math
.min(1,100,101,99,999),"\n")
当运行上面的程序时,将得到以下输出 -
Floor of 10.5055 is 10
Ceil of 10.5055 is 11
Square root of 16 is 4
10 power 2 is 100
100 power 0.5 is 10
Absolute value of -10 is 10
Random number between 1 and 100 is 0.22876674703207
Random number between 1 and 100 is 7
Maximum in the input array is 999
Minimum in the input array is 1
3. Lua操作系统工具
编号 |
库或方法 |
描述 |
1 |
|
返回程序使用的CPU时间(以秒为单位)的近似值。 |
2 |
|
返回包含日期和时间的字符串或表,根据给定的字符串格式进行格式化。 |
3 |
|
返回从时间 |
4 |
|
此功能相当于ANSI C功能系统。 它传递要由操作系统shell执行的命令。 如果命令成功终止,则第一个结果为 |
5 |
|
调用ANSI C函数出口以终止宿主程序。 如果 |
6 |
|
返回进程环境变量 |
7 |
|
使用给定名称删除文件(或POSIX系统上的空目录)。 如果此函数失败,则返回 |
8 |
|
将名为 |
9 |
|
设置程序的当前区域设置。 |
10 |
|
返回不带参数调用的当前时间,或表示给定表指定的日期和时间的时间。 此表必须包含字段年,月和日,并且可能包含字段小时(默认值为 |
11 |
|
返回一个文件名,该文件名可用于临时文件。 文件必须在使用前显式打开,并在不再需要时显式删除。 |
常见的OS功能
使用常见数学函数的简单示例如下所示 -
-- Date with format
io
.write("The date is ",os
.date("%m/%d/%Y"),"\n")
-- Date and time
io
.write("The date and time is ",os
.date(),"\n")
-- Time
io
.write("The OS time is ",os
.time(),"\n")
-- Wait for some timefori
=1,1000000doend
-- Time since Lua started
io
.write("Lua started before ",os
.clock(),"\n")
当运行上面的程序时,将得到类似的输出如下 -
The date is 01/25/2018
The date and time is 01/25/18 07:38:40
The OS time is 1490615720
Lua started before 0.013
上面的例子只是一些常见的例子,可根据自己的需要使用OS库,建议尝试使用所有的功能以便更加熟悉。像remove
这样的函数有助于删除文件,执行有助于于执行OS命令。
--------------------------------------------------
转载于:https://www.yiibai.com/lua/lua_operating_system_facilities.html
Lua的API函数的更多相关文章
- lua API函数大全
Lua5.1中的API函数 lua_State* luaL_newstate()Lua脚本的编译执行是相互独立的,在不同的线程上执行.通过luaL_newstate()函数可以申请一个虚拟机,返回指针 ...
- VC和VS调用Lua设置以及Lua C API使用。
通过c++调用lua 脚本, 环境VC++6.0 lua sdk 5.1.4 在调用前先认识几个函数.1.调用lua_open()将创建一个指向Lua解释器的指针.2. luaL_ope ...
- Lua常用API
转自:http://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 1. 建一个新表 void lua_createtable (lua ...
- Lua C API的正确用法
http://blog.codingnow.com/2015/05/lua_c_api.html http://blog.csdn.net/oilcode/article/details/510861 ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...
- lua实现私有函数
本文是原创文章,如需转载,请注明文章出处 要用lua实现私有函数,关键就是使用metatable的特性来实现. Test.lua: local v = {};v.x = 100;v.y = 200; ...
- C#中可直接调用WIN32的API函数--USER32.DLL
Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...
- Appium常用的API函数
在学习应用一个框架之前,应该了解一下这个框架的整体结构或是相应的API函数.这篇文章还不错:http://blog.sina.com.cn/s/blog_68f262210102vzf9.html,就 ...
- mfc 调用Windows的API函数实现同步异步串口通信(源码)
在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信.串口通信方便易行,应用广泛. 一般情况下,工控机和各智能仪表通过RS485总线进行通信.RS485的通信方式是半 ...
随机推荐
- Lombok Pojo默认初始值问题
Lombok以注解形式来简化java代码,提高开发效率.比如我们常用的@Builder.@Data.@AllArgsConstructor.@NoArgsConstructor.@ToString等. ...
- css 能改变input type radio和checkbox 圆圈或方框的大小
把input隐藏,外面套label,再里面加个span,样式写在span上,让label覆盖在span上面,js去改active的class <label for="remember& ...
- 查一张表占多少空间Bytes
SELECT SUM(BYTES)/1024/1024||'MB' 占用空间 FROM dba_segments WHERE segment_name = '表名' AND owner = '用户名' ...
- Mac下idea启动H5报错:xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Deve
1. 执行“ xcodebuild -showsdks ”,报错如下“xcode-select: error: tool 'xcodebuild' requires Xcode, but active ...
- ubuntu 18.04 64bit下如何启动向日葵远程控制端软件?
一. 背景 从向日葵官网下载了linux版向日葵远程控制端软件,解压后直接执行Sunlloginremote发现以下错误: jello@jello:~/sunlogin_remote_linux$ . ...
- idea使用generatorconfig生成
在maven工程中的resource中创建generatorConfigxml配置generatorConfigxml的配置pomxml生成对象的两种方式方式一使用idea的maven插件直接快速生成 ...
- php支持多个地址跨域访问
//跨域访问的时候才会存在此字段 $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : ''; $allow_ori ...
- 用事件队列来处理pixi中的场景元素入场
在pixi中,添加一个精灵元素,你可能需要,先将贴图load进来,然后才能添加到场景中去,所以一般会这么操作 Loader.add("tree","static/imag ...
- Spring Boot默认日志logback配置解析
前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...
- C++返回对象和返回引用
我们发现,在C++中,有些成员函数返回的是对象,而有些函数返回的又是引用. 返回对象和返回引用的最主要的区别就是函数原型和函数头. Car run(const Car &) //返回对 ...