I have a message structure in Proto3 where i want to be able to send a number of items with the same type definition. Take the following example
syntax = "proto3";
message Player {
uint32 id = 1;
uint32 x_pos = 2;
uint32 y_pos = 3;
PlayerColour colour = 4;
}
enum PlayerColour {
colour_unspecified = 0;
colour_green = 1;
colour_white = 2;
colour_gold = 3;
}
message AllPlayers {
Player player_1 = 1;
Player player_2 = 2;
Player player_3 = 3;
Player player_4 = 4;
}
As you can see, this structure predefines the number of players that can be communicated in a single message. I am curious, is there a way that the number of instances can be variable?
i.e. if only one player has had data about them change, then the message sent only contains one instance of a player parameter. Or if there are only 2 players playing at a given moment, bandwidth isn't then spent sending packets of unused data (blank player_3 and player_4 instances) This would massively reduce the total bandwidth needed to communicate this data.
Any advice?
Yes. Firstly, note that it already is variable; if you don't assign a value to an AllPlayers.player_2
field, then there is nothing to send - literally zero bytes for that player. Alternatively, you could use repeated Player players = 1;
and add as many or as few as you like, using the .id
to resolve which player that is at the receiver. There is effectively no difference in bandwidth between individual Player
fields and a repeated Player
field - until you get a decent number of individual Player
entries, when that becomes more expensive (larger field numbers take more bytes to send; fields up to 15 take 1 byte for their field header)