Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Send info from XNA-game

Send info from XNA-game

Scheduled Pinned Locked Moved C#
game-devquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    larsp777
    wrote on last edited by
    #1

    I have a winform and a picturebox. In the picturebox I´m running a XNA-game. In the winform I have:

    Game1 game = new Game1();

    public Game1 Game
    {
    get
    {
    return game;
    }
    set
    {
    game = value;
    }
    }

    In the XNA-form I try

    public Form1 form = new Form1();
               
    
        public Form1 Form
        {
            get
            {
                return form;
            }
            set
            {
                form = value;
            }
        }
    

    I can via methods send info to the game using: game.StartBoat(); where StartBoat is a method in my XNA-game. I would like to send info to the winform also. But I get a stackoverflow exception indicating an infinite loop. How should I do it if possible?

    L P 2 Replies Last reply
    0
    • L larsp777

      I have a winform and a picturebox. In the picturebox I´m running a XNA-game. In the winform I have:

      Game1 game = new Game1();

      public Game1 Game
      {
      get
      {
      return game;
      }
      set
      {
      game = value;
      }
      }

      In the XNA-form I try

      public Form1 form = new Form1();
                 
      
          public Form1 Form
          {
              get
              {
                  return form;
              }
              set
              {
                  form = value;
              }
          }
      

      I can via methods send info to the game using: game.StartBoat(); where StartBoat is a method in my XNA-game. I would like to send info to the winform also. But I get a stackoverflow exception indicating an infinite loop. How should I do it if possible?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That code does not make much sense, in both cases the get and set would seem redundant.

      L 1 Reply Last reply
      0
      • L larsp777

        I have a winform and a picturebox. In the picturebox I´m running a XNA-game. In the winform I have:

        Game1 game = new Game1();

        public Game1 Game
        {
        get
        {
        return game;
        }
        set
        {
        game = value;
        }
        }

        In the XNA-form I try

        public Form1 form = new Form1();
                   
        
            public Form1 Form
            {
                get
                {
                    return form;
                }
                set
                {
                    form = value;
                }
            }
        

        I can via methods send info to the game using: game.StartBoat(); where StartBoat is a method in my XNA-game. I would like to send info to the winform also. But I get a stackoverflow exception indicating an infinite loop. How should I do it if possible?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        If one of those is responsible for creating the other, don't new up the original one in the new one. So, if Form1 creates Game1, don't do new Form1() in Game1. That will clear your stack overflow. Now, as to the second part of your problem. There are a few things you could do to get around this. The first method is to pass a reference to Form1 into the1 Game constructor (or explicitly set it at some point before you try to use Game).

        public class Game1 : Game
        {
        private Form1 form;
        public Game1(Form1 form)
        {
        this.form = form;
        }
        }

        public class Form1 : Form
        {
        private Game1 game;
        public Form1()
        {
        game = new Game1(this);
        }
        }

        The other way to do this, and the way I tend to prefer, is to use events in Game1 to signal that Form1 should do something. By using events, you decouple Game1 from having to know anything at all about Form1 - which means you could swap it into other projects far easier. Basically, after you instantiate Game1 in Form1, you can subscribe to the events that you're interested in and use those as appropriate.

        L 1 Reply Last reply
        0
        • L Lost User

          That code does not make much sense, in both cases the get and set would seem redundant.

          L Offline
          L Offline
          larsp777
          wrote on last edited by
          #4

          Richard MacCutchan wrote:

          That code does not make much sense, in both cases the get and set would seem redundant.

          Well, I´m not a superexpert in C# and certainly not XNA so I have got a lot of help via forums.

          1 Reply Last reply
          0
          • P Pete OHanlon

            If one of those is responsible for creating the other, don't new up the original one in the new one. So, if Form1 creates Game1, don't do new Form1() in Game1. That will clear your stack overflow. Now, as to the second part of your problem. There are a few things you could do to get around this. The first method is to pass a reference to Form1 into the1 Game constructor (or explicitly set it at some point before you try to use Game).

            public class Game1 : Game
            {
            private Form1 form;
            public Game1(Form1 form)
            {
            this.form = form;
            }
            }

            public class Form1 : Form
            {
            private Game1 game;
            public Form1()
            {
            game = new Game1(this);
            }
            }

            The other way to do this, and the way I tend to prefer, is to use events in Game1 to signal that Form1 should do something. By using events, you decouple Game1 from having to know anything at all about Form1 - which means you could swap it into other projects far easier. Basically, after you instantiate Game1 in Form1, you can subscribe to the events that you're interested in and use those as appropriate.

            L Offline
            L Offline
            larsp777
            wrote on last edited by
            #5

            Thanks, it worked!

            P 1 Reply Last reply
            0
            • L larsp777

              Thanks, it worked!

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You're welcome.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups