当前位置:网站首页>Lua 快速入门(四)——多脚本执行
Lua 快速入门(四)——多脚本执行
2022-08-11 05:31:00 【canon_卡农】
前言
Lua是由标准C实现的解释型语言,它的执行效率比较高,通常作为脚本嵌入到应用程序中。
由于支持热更新,Lua在游戏开发领域应用普遍。作为从业人员,不得不来填这个大坑。
本文是Lua入门学习笔记,来源是泰课的一个Lua教程。这里主要记录Lua的语法,以及实现
简单的OOP。
Lua 快速入门(一)——基础语法
Lua 快速入门(二)——函数(Function)
Lua 快速入门(三)——表(Table)
Lua 快速入门(四)——多脚本执行
Lua 快速入门(五)——协程(thread)
Lua 快速入门(六)——基础OOP
变量作用域
Lua中使用 local标识的变量为局部变量。
局部变量的作用域为从声明位置开始到所在语句块结束。
除局部变量,其他变量都是全局变量。
print("**********全局变量和本地变量************")
--全局变量
a = 1
b = "123"
for i = 1,2 do
c = "Canon"
end
print(c) --Canon
--局部变量的关键字 local
for i = 1,2 do
local d = "Canon"
print("循环中的d"..d)
end
print(d) --nil
fun = function()
local tt = "123123123"
end
fun()
print(tt) --nil
local tt2 = "555"
print(tt2) --555
多脚本执行
1.require关键字
Lua通过require关键字加载指定的模块。
加载了该模块,那么就可已使用模块中的全局函数和全局数据(如表等等)
-- Test.lua
testA = "123"
local testLocalA = "456"
print(testLocalA)
-- 默认返回 true 可以手动返回其他值
return testLocalA
require实现代码
function require(name)
if not package.loaded[name] then
local loader = findloader(name)
if loader == nil then
error("unable to load module" .. name)
end
package.loaded[name] = true
local res = loader(name)
if res ~= nil then
package.loaded[name] = res
end
end
return package.loaded[name]
end
2.package.loaded
如果该模块自身有返回值,且模块加载成功,那么require 的返回值就是该模块的返回值
如果模块没有返回值,如果require加载模块成功,就返回ture
require 内部将返回值储存在:package.loaded表中。(即:package.loaded[xxx]中)
print("**********多脚本执行************")
--关键字 require("脚本名") require('脚本名')
require('Test') --456
print(testA) --123
print(testLocalA) --nil
--如果是require加载执行的脚本 加载一次过后不会再被执行
require("Test")
--package.loaded["脚本名"]
--返回值为true/nil 代表该脚本是否被执行
print(package.loaded["Test"]) --true
--卸载已经执行过的脚本
package.loaded["Test"] = nil
print(package.loaded["Test"]) --nil
--require 执行一个脚本时 可以在脚本最后返回一个外部希望获取的内容
local testLA = require("Test")
print(testLA) --456 (Test.lua的返回值)
3._G表
_G是一张表,保存了lua所用的所有全局函数和全局变量
注:实际上require “xxx”后,会将xxx中的全局函数和数据放到表_G中,所以也就能访问了。
print("**********大G表************")
--_G表是一个总表(table) 他将我们申明的所有全局的变量都存储在其中
for k,v in pairs(_G) do
print(k,v)
end
--本地变量 加了local的变量时不会存到大_G表中
require的搜索模块的路径
1.搜索lua文件的路径存放在变量package.path中,当lua启动后便以环境变量LUA_PATH的值来初始化这个变量,如果没有找到,则使用2中的的默认路径。如果lua还是无法找到与模块命符合的lua文件,那么它会开始找c程序库,路径储存在package.cpath中,这个变量通过环境变量LUA_CPATH来初始化。
2.require的默认路径在:luaconf.h中,我们打开该文件搜索:LUA_PATH_DEFAULT ,便可以看到
#deine LUA_PATH_DEFAULT
LUA_LDIR”?.lua;” LUA_LDIR”?\init.lua;”
LUA_CDIR”?.lua;” LUA_CDIR”?\init.lua;”
LUA_SHRDIR”?.lua;” LUA_SHRDIR”?\init.lua;”
“.?.lua;” “.?\init.lua;”
LUA_CDIR”…\testLua\Lua?.lua;” –这句是我自己加的
#deine LUA_CPATH_DEFAULT
LUA_CDIR”?.dll;”
LUA_CDIR”…\lib\lua\” LUA_VDIR “?.dll;”
LUA_CDIR”loadall.dll;” “.?.dll”
注:
1.修改了luaconf.h中的路径后,我们需要重新生成新的lua静态lib库,然后我们的程序使用新的静态lib,这样才能起效
2.可以看到默认的路径的格式为: ?.lua , 所以我们require “xxx”,而不是require “xxx.lua”
总结
Lua 通过require加载其他脚本,加载成功返回值存在package.loaded表中。可以通过 _G表获取
自身和已加载脚本中所有全局变量。
参考:https://blog.csdn.net/qweewqpkn/article/details/49050507
边栏推荐
- 品优购项目实战笔记
- JS案例练习(pink老师经典案例)
- The mount command - mounted read-only, solution
- Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
- mk file introduction
- 【剑指offer系列】面试题日记(前期题)
- USB URB
- C语言-6月12日-字符替换问题,将一个‘ ’替换为2个‘#’
- Day 83
- Asis2016 books null off by one
猜你喜欢
gerrit configure SSH Key and account, email information
C语言-7月18日-二维数组的学习
The whole process of Tinker access --- configuration
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
JS事件循环机制
C语言预处理
Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
C语言-6月12日-字符替换问题,将一个‘ ’替换为2个‘#’
stack stack
随机推荐
厂商推送平台-华为接入
【LeetCode-69】x的平方根
IO流和序列化与反序列化
Tinker's self-introduction
Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
JS advanced web page special effects (pink teacher notes)
Matplotlib找不到字体,打印乱码
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
第六届蓝帽杯 EscapeShellcode
轻松理解进程与线程
mysql basic summary
自己动手写RISC-V的C编译器-01实现加减法
mk file introduction
Unity两种VR环境配置方法
【LeetCode-56】合并区间
Day 79
USB in NRZI to encode the data