如何在声明后访问 ProtoField 的名称?
2018-8-27 23:0:19
收藏:0
阅读:201
评论:2
我如何在声明 ProtoField 后访问其名称?
例如,类似于以下内容:
myproto = Proto("myproto", "My Proto")
myproto.fields.foo = ProtoField.int8("myproto.foo", "Foo", base.DEC)
print(myproto.fields.foo.name)
我得到输出:
Foo
点赞
用户2755698
一种更简洁的备用方法:
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* myproto")
print(string.sub(fieldString, i + 2, j - (1 + string.len("myproto")))
编辑:或者一种更简单的适用于 任何 协议的解决方案:
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* ")
print(string.sub(fieldString, i + 2, j - 1))
当然,第二种方法仅在字段名称中没有空格的情况下起作用。由于这不一定总是成立,第一种方法更为健壮。以下是将第一种方法封装为应该能够被任何解剖仪使用的函数:
-- The field is the field whose name you want to print.
-- The proto is the name of the relevant protocol
function printFieldName(field, protoStr)
local fieldString = tostring(field)
local i, j = string.find(fieldString, ": .* " .. protoStr)
print(string.sub(fieldString, i + 2, j - (1 + string.len(protoStr)))
end
... 在这里使用:
printFieldName(myproto.fields.foo, "myproto")
printFieldName(someproto.fields.bar, "someproto")
2018-08-28 16:05:34
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在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 代码?

好的,这很不稳定,肯定不是“正确”的方法,但它似乎起作用。
我在查看输出后发现了这一点
这似乎会输出ProtoField的每个成员的值,但是我无法找出正确的访问方式。因此,我决定解析字符串。此函数将返回' Foo',但也可以调整以返回其他字段。
function getname(field) --首先,将字段转换为字符串 --这将导致长字符串 --一堆我们不需要的信息 local fieldString = tostring(field) -- fieldString看起来像: -- ProtoField(188403):Foo myproto.foo base.DEC 0000000000000000 00000000(null) --在“。”上拆分字符串 a,b = fieldString:match“([^。] *)。 (。*)” --在先前结果(a)的前半部分上拆分“:”字符 a,b = a:match“([^。] *):(。*)” --此时,b将等于“Foo myproto”, --我们要去掉那个缩写“abvr”部分 --计算字符串中空格出现次数 local spaceCount = select(2,string.gsub(b,“”,“”)) --声明一个计数器 local counter = 0 --声明我们要返回的名称 local constructedName ='' --按空格分隔(b)中的每个单词按顺序进行步骤 for word in b:gmatch“%w +” do --如果我们已到达最后一个空格,请继续返回 if counter == spaceCount-1 then return constructedName end --向我们的名称添加当前单词 constructedName = constructedName .. word ..“” --增加计数器 counter = counter + 1 end end