Playerlist also has methods:
const RepeatedPtrField<Player>& player() const;
RepeatedPtrField<Player>* mutable_player();

RepeatedPtrField is documented here:

It has methods which allow you to add an existing object without copying
(look under "Advanced memory management).  Of course, if you use these, then
the Player objects will become property of the Playerlist.  You will have to
be careful to take ownership back from the list later by repeatedly calling

Also note that every protocol message has a CopyFrom() method which can be
used to copy an entire message object, so you would not have to call
set_id(), set_x(), etc. manually.

On Wed, Sep 24, 2008 at 8:01 AM, <[EMAIL PROTECTED]> wrote:

> Hello,
> I have a question concerning the repeated fields. I'll start with an
> example:
> message Player {
>  required int32 id = 1;
>  required int32 x =2;
>  required int32 y=3;
> }
> message Playerlist{
>  repeated Player player= 1;
> }
> I have a server and get Player's from different clients and hold them
> in a map (this map is updated all the time). After a certain time I
> want to get all Players, create a Playerlist and send it to all
> clients. The Playerlist however has just a method
> Player* add_player();
> I will have to copy all the Player's I've already created :
> Player* newP = playerlist.add_player();
> newP->set_id( playerFromMap->id() );
> Isn't there a better way to do this?
> Regards
> >

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at

Reply via email to