0
\$\begingroup\$

I am using lidgren as a networking library for a small XNA game. I am using a client/server architecture for the game. Currently, I have the client and server connecting but I would like to be able to pass an enum with a character type when the client requests to join the server. Here is a sample of my code:

while ((inMsg = networkManager.ReadMessage()) != null)
        {

            switch (inMsg.MessageType){
                case NetIncomingMessageType.StatusChanged:
                    switch ((NetConnectionStatus)inMsg.ReadByte()){
                        case NetConnectionStatus.Connected:
                            if (!this.isHosting)
                            {
                              //Extract player info from message and create a local player                             
                            }break;
                        case NetConnectionStatus.RespondedAwaitingApproval:
                            pType = (PlayerType)inMsg.ReadByte();
                            playerManager.AddPlayer(pType);
                            //Then I will send the player information to the client
                            inMsg.SenderConnection.Approve();
                            break;
                    }
                    break;

            }

            this.networkManager.Recycle(inMsg);
        }

When server gets a message with RespondedAwaitingApproval flag, I would like to be able to read the message, extract the type and create an instance of a player of that type. How can I accomplish that?

\$\endgroup\$
1
\$\begingroup\$

What's wrong with the simple approach? Something like:

Player CreatePlayerOfType (PlayerType type) {
  switch (type) {
    case PlayerType.Red:
      return new RedPlayer();
    case PlayerType.Blue:
      return new BluePlayer();
    default:
      throw new InvalidOperationException("Unexpected player type.");
  }
}

It's not clear from your question whether different player type enum values correspond to actual different player classes or just instances with different data (which is probably the approach I'd vote for, but I don't know anything else about the nature of your game). However, whichever it is, the above approach will work fine.

If construction of your players becomes complicated or you have a lot of player types or permutations, the above code can present a maintainability problem as it scales. To handle that, you can build your implementation towards an abstract factory pattern, which can afford you some interesting options for data-driving your player creation, or at least keeping the maintenance overhead down.

\$\endgroup\$
  • \$\begingroup\$ Im talking about the client letting the server know what type of player they would like to create. The server then approves/disapproves the request. If it approves, it creates a player instance of that type with a player id for control and passes it back to the client. This is probably wrong though.. \$\endgroup\$ – John Dec 3 '13 at 21:53
  • \$\begingroup\$ You probably don't want to "pass the player back to the client," you just want the client to create the appropriate player representation locally (after the server validation was successful). If you have further questions about implementing that actual validation process, you should probably ask a new question. \$\endgroup\$ – Josh Dec 3 '13 at 22:06
0
\$\begingroup\$

I have just solved the question by doing the following:

I pass in the player type with the netClient.Connect() method.

NetOutgoingMessage hailMsg = netClient.CreateMessage();
hailMsg.Write((byte)playerType);
netClient.Connect(ipEndPoint,hailMsg);

Then, on the server side, I did the following:

case NetConnectionStatus.RespondedAwaitingApproval:
     PlayerTypesTypes playerType = (PlayerTypes)inMsg.SenderConnection.RemoteHailMessage.ReadByte();
     //Do whatever you want with the playertype
     inMsg.SenderConnection.Approve();
     break;

Basically, I just realised that you could send additional information along with the clients hail message in the netClient.Connect method and that you can extract that from the hail message by specifying SenderConnection.RemoteHailMessage.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.