问几个lua上的protobuf问题

message PbRole
{
required int32 id = 1;
required string name = 2;
}

message PbTeam
{
optional int32 id = 1;
optional PbRole role_player = 2;
repeated int32 item_list = 3;
repeated PbRole role_list = 4;
}

消息解析:
local msg= pb.PbTeam()
msg:ParseFromString(str)
问题1:如何得知 msg里的 id 和 role_player 是否存在
问题2:如果 role_player 存在,如何获得 PbRole 的对象
问题3:如何获得item_list 和 role_list 的个数
问题4:如何获得item_list 和 role_list 的数组元素

消息发送:
local msg= pb.PbTeam()
问题5:如何给msg添加 role_player 对象
问题6:如果给msg添加 item_list 和 role_list

希望大家能帮忙解答一下,网上搜来的全是关于lua配置pb的文章,找不到这些使用方法。或者发我一个能解决问题的连接也好。

问题 2,3,4,6,都猜出来了。。

只剩问题1 ,5 了,麻烦高人给解答一下呗

答案1:
message PbTeam
{
optional int32 id = 1 ;
optional PbRole role_player = 2;
repeated int32 item_list = 3;
repeated PbRole role_list = 4;
}

if msg.id and msg.id ~= -1 then
print("收到了消息,ID - ", msg.id)
end
if msg.role_player then
print("收到了消息,player = ", msg.roleplayer)
end

答案5:
local msg= pb.PbTeam()
local player = pb.PbRole()
player.id, player.name = 1, “abc”
msg.role_player = player

楼主把几个答案全部贴出来,以便后来的人了解,我也很感兴趣