XNA Creators Club Online
Page 1 of 2 (27 items) 1 2 Next >
Sort Posts: Previous Next

Show MessageBox after getting kicked

Last post 11/10/2007 9:56 by tmegz. 26 replies.
  • 07/10/2007 15:46

    Show MessageBox after getting kicked

    Hi!
    I want to implement a kick function in my game, so that when the server sends "kick" the client exits the game and shows a MessageBox with "You got kicked!".
    I tried it this way:
    A static bool variable in my program.cs, the game()-class changes this bool to true if the client gets the command "kick" and then, after the using (Game1 game = new Game1(gn, gm))... code, if(playerKicked) MessageBox.Show(...);
    But I don't know how to exit the game correctly. If I write this.Exit(); after the kicking code, the game will exit completely, without showing this messagebox. If I write this.Dispose();, the game will hang up.

    What can I do?
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 07/10/2007 18:58 In reply to

    Re: Show MessageBox after getting kicked

    Assuming you're using some kind of state management in your game, you could set a state variable:

     

    //serious pseudo-code :)

    //in the network code

    if( msg == "kick")

        state = GameState.ShowKickMsg

     

    //in the game code
    switch (state)

    GameState.ShowKickMsg - show kick messagebox

    GameState.Kick - exit the game

     

    //in the messagebox code

    state = GameState.Kick

    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 08/10/2007 10:56 In reply to

    Re: Show MessageBox after getting kicked

    Yeah, ok, but I don't want to show this message box in the game. The game window should close, and then I want to show just a MessageBox from the WindowsForms.

    How can I realise that? My problem is only closing the game without closing the whole program, and, as said before, this.Exit() exits the whole program, and this.Dispose() hangs up the game. What else can I do?
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 08/10/2007 12:34 In reply to

    Re: Show MessageBox after getting kicked

    If your Main looks like this:
    public static bool Kicked = false;

    public static void Main()
    {
    using (Game1 game = new Game1())
    {
    game.Run();
    }

    if (Kicked)
    MessageBox.Show("You've been kicked");
    }

    It should work just fine.
  • 08/10/2007 15:22 In reply to

    Re: Show MessageBox after getting kicked

    Yeah, this is what my program.cs looks like, but it doesn't work!! The game exits without calling the MessageBox.Show() method!
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 08/10/2007 16:07 In reply to

    Re: Show MessageBox after getting kicked

    When you call Game.Exit(), it might be shutting down the window, in which case there won't be a message loop.  The dialog box will be created and ignored, the program will close before you see it.  This is just a guess.  I think you can overload the Exit() method, if you can, try placing your message box code in there.
  • 08/10/2007 20:41 In reply to

    Re: Show MessageBox after getting kicked

    multimolti:
    Yeah, this is what my program.cs looks like, but it doesn't work!! The game exits without calling the MessageBox.Show() method!


    Then you have something else going on. I created a new Windows game project, added the show dialog method just like in my last post, ran the game, closed the window, and the message box popped up. I know for a fact that it works under normal circumstances. Can you post your Main method for me to see?
  • 09/10/2007 9:51 In reply to

    Re: Show MessageBox after getting kicked

    When I close the game by clicking at the normal windows close button, the MessageBox appears, but not when I close the game with this.Exit();
    But when the server sends "kick", the player won't click at the X, but the game should close itself AND show the message box. This is my problem.
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 09/10/2007 11:03 In reply to

    Re: Show MessageBox after getting kicked

    If your code isn't working you're going to have to post it so we can see what you're doing.

    edit: Tried a sample and the MessageBox doesn't show if you put it in the program.cs or if you override the OnExiting when you exit the game using the keyboard or gamepad. :(

    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 09/10/2007 12:36 In reply to

    Re: Show MessageBox after getting kicked

    Ok, here my code:

    //program.cs:

    public static bool playerKicked = false;
    ...
    public static void Main()
    {
                using (Game1 game = new Game1(gn, gm))
                {
                   game.Run();
                }
                if (playerKicked)
                {
                    MessageBox.Show("You got kicked from the server!", "Kicked");
                }
    }

    //game1.cs:
    switch(receivedData)
    {
                                case "kick":
                                    Program.playerKicked = true;
                                    connection.Send("logout kick");
                                    connection.Disconnect();
                                    this.Exit();
                                    break;
    }

    But it seems that the "this.Exit();" code exits the whole program, so that the "if(playerKicked)..." never happens.
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 09/10/2007 12:59 In reply to

    Re: Show MessageBox after getting kicked

    So what happens if you add a break somewhere after game.Run, what's the value of playerKicked?  Will the program break on the line, is code being executed beyond game.Run()?  Is playerKicked = true?
  • 09/10/2007 13:06 In reply to

    Re: Show MessageBox after getting kicked

    I think you can see all your questions in the code:

    The value of playerKicked is false, as long as the server didn't send "kick", then I change the value to true.
    The program will break nothing, it will just exit when the server sends "kick" (this.Exit()).
    Beyond game.Run() the MessageBox gode is executed.

    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 09/10/2007 14:16 In reply to

    Re: Show MessageBox after getting kicked

    Maybe I'm just not looking in the right place but even looking at the Game class in Reflector I can't see why it wouldn't show the MessageBox. :(
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 09/10/2007 16:29 In reply to

    Re: Show MessageBox after getting kicked

    That's strange. Here are the exact steps I just took a moment ago to get this all working.

    1) Create a new Windows project.
    2) Add a reference to System.Windows.Forms.
    3) Add line System.Windows.Forms.MessageBox.Show("You have been kicked"); to the end of your Main method. Run game and exit; message box shows up.
    4) Added public static bool Kicked = false; to Program declaration. Added if (Kicked) to show the message box. Run game and exit; message box doesn't show up (it shouldn't).
    5) Go into Game1.cs. Add line Program.Kicked = true; in Game1's constructor. Run game and exit; message box shows up again.

    I truly have no idea why it's not working on your machine, but it works fine with and without the bool test on mine. Here's my entire Program.cs file:

    using System;

    namespace WindowsGame1
    {
      static class Program
      {
        public static bool Kicked = false;
        static void Main(string[] args)
        {
          using (Game1 game = new Game1())
          {
            game.Run();
          }

          if (Kicked)
            System.Windows.Forms.MessageBox.Show("You have been kicked.");
        }
      }
    }

    Then I just make sure to set Program.Kicked to true anywhere in my Game1 class and it will show the message box.
  • 09/10/2007 16:42 In reply to

    Re: Show MessageBox after getting kicked

    My whole program.cs looks like this:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    namespace MMORPGEngine
    {
        static class Program
        {
            public static GameNetwork gn = new GameNetwork();
            public static Files fileManager = new Files();
            public static GameManager gm = new GameManager();
            public static bool playerKicked = false;

            [STAThread]
            static void Main()
            {
                fileManager.Log("---");
                fileManager.Log("Starting WindowsForm");

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Login(gn, gm));
                if (gn.characterChosen)
                {
                    fileManager.Log("Initializing Game...");
                    using (Game1 game = new Game1(gn, gm))
                    {
                        try
                        {
                            game.Run();
                        }
                        catch (Exception e)
                        {
                            fileManager.Log("Game crash, submitting error log");

                            gn.Send("errorlog " + fileManager.ReadFile("gamelog.log").Replace("\r\n", "*#*"));
                            gn.Send("errorlog2 " + e.ToString().Replace("\r\n", "*#*"));
                            gn.Send("logout crash");
                            gn.Disconnect();

                            MessageBox.Show("The game crashed! The error log has been submitted.\r\n(The error log doesn't contain any private information!)", "Error");
                        }
                    }
                }

                if (gm.playerKicked)
                {
                    MessageBox.Show("You got kicked from the server!", "Kicked");
                }
            }
        }
    }

    The gm variable is my GameManager, and because it's static, the changes during the Game1() should be applied, shouldn't they?
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 09/10/2007 16:46 In reply to

    Re: Show MessageBox after getting kicked

    multimolti:

    The gm variable is my GameManager, and because it's static, the changes during the Game1() should be applied, shouldn't they?


    It should. Where are you changing the values in your game? Also, by making it public static, you don't have to pass it to the game. You can just refer to it as Program.gm instead which might clean things up by not storing redundant references.

    Also, and this is probably just something you forgot to take out, but why do you have the playerKicked bool as well as the playerKicked field in GameManager?
  • 09/10/2007 19:08 In reply to

    Re: Show MessageBox after getting kicked

    Just a side note -- I think the larger problem is you are using ASCII words for your client-server packets. A packet sniffer and a clever Winsock/System.Net.Sockets developer could start kicking everyone in the game. Best to use an encryption/decryption method for your packets. Cheers!

    We are gods among avatars.
  • 09/10/2007 19:32 In reply to

    Re: Show MessageBox after getting kicked

    static class Program
    {
      public static GameManager gm = new GameManager();
      public static bool playerKicked = false;
      ...
    }

    switch(receivedData)
    {
       case "kick":
       Program.playerKicked = true;
       ...
    }

    ...

     if (gm.playerKicked)
     {
        MessageBox.Show("You got kicked from the server!", "Kicked");
     }


    Do you have 2 different classes with a playerKicked member, and are you setting one to true, but then checking the other (which I assume is staying false)?
  • 10/10/2007 6:51 In reply to

    Re: Show MessageBox after getting kicked

    Caetris:

    Just a side note -- I think the larger problem is you are using ASCII words for your client-server packets. A packet sniffer and a clever Winsock/System.Net.Sockets developer could start kicking everyone in the game. Best to use an encryption/decryption method for your packets. Cheers!

    What??

    He's only parsing messages from the server (i guess), nobody can send "kick" directly to a player.
    And in this case encryption will only be security by obscurity, if somebody can sniff the packets being sent when you kick a player then they can just send the encrypted string they got from the packetsniffer right away.

    There is no problem in using plain text for messages.

     

    What can become a problem however is if someone recompiles the game and outcomments the line where you call game.Exit(), then you will have an unkickable player. But this can easily be solved by just ignoring messages from that client on the serverside.

  • 10/10/2007 7:48 In reply to

    Re: Show MessageBox after getting kicked

    Nick Gravelyn:
    That's strange. Here are the exact steps I just took a moment ago to get this all working.

    1) Create a new Windows project.
    2) Add a reference to System.Windows.Forms.
    3) Add line System.Windows.Forms.MessageBox.Show("You have been kicked"); to the end of your Main method. Run game and exit; message box shows up.
    4) Added public static bool Kicked = false; to Program declaration. Added if (Kicked) to show the message box. Run game and exit; message box doesn't show up (it shouldn't).
    5) Go into Game1.cs. Add line Program.Kicked = true; in Game1's constructor. Run game and exit; message box shows up again.

    How are you exiting?

    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 10/10/2007 8:52 In reply to

    Re: Show MessageBox after getting kicked

    emachine:

    Do you have 2 different classes with a playerKicked member, and are you setting one to true, but then checking the other (which I assume is staying false)?


    No, I changed the code just a little bit in the past hours. My current code looks like this, and isn't working:
    // program.cs
    static class Program
    {
    public static bool playerKicked = false;
    ...

    if (playerKicked)
    {
    MessageBox.Show("You got kicked from the server!", "Kicked");
    }
    }

    // game1.cs
    switch(receivedata) ...
    case "kick":
    Program.playerKicked = true;
    ...
    this.Exit();
    break;

    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 10/10/2007 11:06 In reply to

    Re: Show MessageBox after getting kicked

    Machaira:
    How are you exiting?


    Hitting the little X in the corner. Is that actually different than calling Exit()? Great, now I get to test that out.

    Edit: It does. Great. I stepped through the debugger. It still hits the MessageBox.Show line but for some reason the message box doesn't show up. I wonder why closing the XNA window by hitting the close button acts differently than Exit().
  • 10/10/2007 11:18 In reply to

    Re: Show MessageBox after getting kicked

    Got it. I don't know why I have to do it this way (something XNA does internally messes up me making more windows apparently), but this does work whether you close the window or call Exit():

      ThreadStart kickedStart = delegate {
        System.Windows.Forms.MessageBox.Show("KICKED!");
      };

      Thread thread = new Thread(
    kickedStart);
      thread.IsBackground = false;
      if (Kicked)
        thread.Start();

    Apparently you just need to run it on a second thread. It's horrible design and ugly as sin, but it does get the job done.
  • 10/10/2007 13:00 In reply to

    Re: Show MessageBox after getting kicked

    Yeah, it works ;-)
    Starting a new thread just for a little messagebox is a little bit copious, but when it's the only solution, I'll use it.

    Thanks to everybody
    MMORPG Engine and Game |My Blog |Lennart Moltrecht's Website
    There are 10 kinds of people: Those who understand binary encoding and those who don't.
  • 10/10/2007 13:24 In reply to

    Re: Show MessageBox after getting kicked

    So I wonder what would happen if you replaced game.exit with a call to CloseWindow WinAPI, using the game.window.handle, to "programmatically click the X"

    (not that it's any better... but just to experiment...)
Page 1 of 2 (27 items) 1 2 Next > Previous Next