如何在目标协程中获取协程的名称?
2021-8-9 8:4:5
收藏:0
阅读:113
评论:1
我想要像这样:
local co1 = coroutine.create(function()
local evt, _, _, nm, arg1 = event.pull("thread_msg", 2)
-- 获取一个 "thread_msg" 事件。
if(nm == coroutine_name)then
print(evt, arg1) -- 打印事件名称和"thread_msg"发送的参数
end
end)
coroutine.resume(co1)
event.push("thread_msg", "co1", "") -- 向协程发送一条消息
我需要协程的名称。"thread_msg"事件发送到所有正在运行的协程,coroutine.send也是如此。我需要在协程内部获取协程的名称。
使用mc版本1.12.2 forge的opencomputers。 CPU的架构是lua 5.3。 谢谢。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
你可以使用
load()
函数加载 coroutine 的代码。因为使用
load()
函数可以给函数起一个名字,并存储在debug.getinfo()
表中的source
中。如果 coroutine /函数出现错误时,也会使用
source
信息显示出错的位置。下面是在 Lua 交互控制台中,使用
coroutine.wrap()
函数构建 coroutine 函数的基本示例…$ /usr/bin/lua Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio > code={} > run={} > code.co=[[return coroutine.wrap(function(...) >> local args={...} -- args 保存传入函数的参数 >> args[0]=debug.getinfo(1).source -- 使用 `load(textcode, 'Name')` 给函数命名 >> print(args[0],'Going to yielding now') >> coroutine.yield(args) >> args=[0]=debug.getinfo(1).source -- 如果在循环中调用更多 coroutines ,则可以在此处更新 args[0] >> print('Going to end:',args[0]) >> print(args[0],'Coroutine goes dead now') >> return args >> end)]] > run[1]=load(code.co,'Megacoroutine')() > run[1]() Megacoroutine Going to yielding now table: 0x565e2890 > run[1]() Going to end: Megacoroutine Megacoroutine Coroutine goes dead now table: 0x565e2890 > run[1]() stdin:1: cannot resume dead coroutine stack traceback: [C]: in field '?' stdin:1: in main chunk [C]: in ? >
编辑:args 必须是local(已更正)