XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

Last post 26/10/2009 16:22 by Shawn Hargreaves. 4 replies.
  • 25/10/2009 21:20

    Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

    I have no problems with CreateSession() and JoinSession() when the host starts the server. I get the exception mentioned in the subject when another network gamer tries to join the session. Specifically in the GamerJoinedEventHandler inside the else statement during the cast. Am I going about this the wrong way? All gamers are on separate machines.


    private LocalNetworkGamer server; 
    private LocalNetworkGamer localGamer; 
     
    public void CreateSession() 
        try 
        { 
            networkSession =                              
                         NetworkSession.Create(NetworkSessionType.SystemLink, 
                                               NetworkGlobals.MAX_LOCAL_GAMERS,    
                                               NetworkGlobals.MAX_GAMERS); 
                     
            networkSession.GamerJoined += GamerJoinedEventHandler; 
            networkSession.SessionEnded += SessionEndedEventHandler; 
        } 
        catch (System.Exception e) 
        { 
            // Error handling removed for brevity 
        } 
     
    public void JoinSession() 
        try 
        { 
            // Search for sessions. 
            using (AvailableNetworkSessionCollection availableSessions = 
                       NetworkSession.Find(NetworkSessionType.SystemLink, 
                                           NetworkGlobals.MAX_LOCAL_GAMERS, null)) 
            { 
                if (availableSessions.Count == 0) 
                { 
                    throw new System.Exception("No sessions found"); 
                } 
                // Join the first session we find. 
                networkSession = NetworkSession.Join(availableSessions[0]); 
                networkSession.GamerJoined += GamerJoinedEventHandler; 
                networkSession.SessionEnded += SessionEndedEventHandler; 
            } 
        } 
        catch (System.Exception e) 
        { 
            throw e; 
        } 
     
    private void GamerJoinedEventHandler(object sender, GamerJoinedEventArgs e) 
        if (e.Gamer.IsHost) 
        { 
            server = (LocalNetworkGamer) e.Gamer; 
        } 
        else 
        { 
            localGamer = (LocalNetworkGamer) e.Gamer; 
        } 
        Player player = new Player(e.Gamer); 
        e.Gamer.Tag = player; 

  • 26/10/2009 0:30 In reply to

    Re: Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

    Had a slight weapons malfunction, but everything's perfectly alright now. We're fine, we're all fine, here, now, thank you. How are you? -- Han Solo

    I believe I have found my problem. I'm still learning to use the debug features of VS and it took me this long to do it old school with sysouts. Copy and paste is a b... east! I duplicated the  event handler code in JoinSession. I removed

    // Join the first session we find. 
    networkSession = NetworkSession.Join(availableSessions[0]); 
    networkSession.GamerJoined += GamerJoinedEventHandler; 
    networkSession.SessionEnded += SessionEndedEventHandler; 
     

    and replaced it with
    networkSession = NetworkSession.Join(availableSessions[0]); localGamer = networkSession.LocalGamers[0];
  • 26/10/2009 0:38 In reply to

    Re: Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

    Yes you are doing this wrong. First off you do not need to store the variables server or localGamer. The entire gamerJoinedEventHandler should just be on line setting the gamer tag as a new player.

    e.Gamer.Tag = new Player(e.Gamer);

    You can not convert from NetworkGamer to LocalNetworkGamer if the network Gamer is not actually Local and by your description there not going to be. To find the server you just use the networkSession.IsHost during your update method. When you need to send packets to the server just use networkSession.Host. This is all explained in the Client Server Game Sample. I am not an expert but i think this is all correct.

    EDIT: You should go back to your first code block and look at the client server sample instead.
  • 26/10/2009 4:50 In reply to

    Re: Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

    Thank you for your response. The Client Server example updates everyone with everything. I do not have that same requirement. I need to be more selective. The 'want' or need for me to store localGamer and server saves me from having to cast in multiple areas of my code. If I want the server to send a message, then I cannot simply use networkSession.Host.Send.... as I will have to cast the Host to a LocalNetworkGamer. It's working just fine for me now such that I can prove my concept. If I ever actually do something with the code, then you can bet I'll be back with more questions. Thanks again.
  • 26/10/2009 16:22 In reply to

    Re: Unable to cast object of type 'Microsoft.Xna.Framework.Net.NetworkGamer' to type 'Microsoft.Xna.Framework.Net.LocalNetworkGamer'.

    The problem is you are assuming that all gamers will be LocalNetworkGamer instances. That isn't true as soon as you have more than one machine in the session!  You can only cast to LocalNetworkGamer if that gamer is indeed local, so you need to check that before doing the cast.  For instance the host gamer will only also be a LocalNetworkGamer if this code is running on the machine which is hosting the session. On any other machines that join this session, the host will not be local.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (5 items) Previous Next