Send info from XNA-game
-
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?
-
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?
-
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?
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.
-
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.
-
You're welcome.