Help needed XNA and Window Forms
-
Hi! I have a XNA Project with a XNA-game contained inside a winform. My program.cs looks like this:
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
form.Game = game; //The new line
game.Run();I have a setter and a getter in the winfom like this:
Game1 game = new Game1();
public Game1 Game
{
get
{
return game;
}
set
{
game = value;
}
}So now I can send values from my form to my Game1 in XNA but not the other way around. How can I send values from XNA to the WinForm? I have used som solutions I have found on the internet so I don't fully understand everything.
-
Hi! I have a XNA Project with a XNA-game contained inside a winform. My program.cs looks like this:
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
form.Game = game; //The new line
game.Run();I have a setter and a getter in the winfom like this:
Game1 game = new Game1();
public Game1 Game
{
get
{
return game;
}
set
{
game = value;
}
}So now I can send values from my form to my Game1 in XNA but not the other way around. How can I send values from XNA to the WinForm? I have used som solutions I have found on the internet so I don't fully understand everything.
Create custom events in your
Game1
class and subscribe to those events from thegame
instance in yourform
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Create custom events in your
Game1
class and subscribe to those events from thegame
instance in yourform
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
form.Game = game; //The new line
game.GameEvent += HandleGameEvent;
game.Run();// Inside Form1
private void HandleGameEvent(object sender, EventArgs e)
{
//
}public class Game1
{
public event EventHandler GameEvent;protected vitual void OnGameEvent(EventArgs e)
{
EventHandler eh = GameEvent;
if(eh != null)
eh(this, e);
}
}Check out Basic Event Creation in C#[^] and Events Made Simple[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
How to raise and consume Events[^]
Clean-up crew needed, grammar spill... - Nagy Vilmos
-
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
form.Game = game; //The new line
game.GameEvent += HandleGameEvent;
game.Run();// Inside Form1
private void HandleGameEvent(object sender, EventArgs e)
{
//
}public class Game1
{
public event EventHandler GameEvent;protected vitual void OnGameEvent(EventArgs e)
{
EventHandler eh = GameEvent;
if(eh != null)
eh(this, e);
}
}Check out Basic Event Creation in C#[^] and Events Made Simple[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thanks, it starting to be clear but it isn´t all that simple to me... :) So, suppose something happends in my XNA-game. There is a update-method were I check if the enemy is alive and within range of Another object:
if (!(enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)))
isActivated = false;if (enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)) {
if ((depthChargeX >= enemyX - 50 && depthChargeX <= enemyX + 50) || (depthChargeY >= enemyY - 50 && depthChargeY <= enemyY + 50) && isActivated == true)
{
enemy.IsAlive = false;
hit.Play();
wreck2.IsAlive = true;wreck2.Update(gameTime); } }
If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button. Would I then send that bool to the OnGameEvent or must I create a new class like in one in one of the examples. I am a little confused still I must admit. But I suppose the HandleGameEvent in the form listens to the event raised. And
game.GameEvent += HandleGameEvent;
Is where you subscribe to the event raised by the game. I also get: The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing.
-
Thanks, it starting to be clear but it isn´t all that simple to me... :) So, suppose something happends in my XNA-game. There is a update-method were I check if the enemy is alive and within range of Another object:
if (!(enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)))
isActivated = false;if (enemy.IsAlive && (player.X <= enemy.X + 100) && (player.Y <= enemy.Y + 50 || player.Y >= enemy.Y + 50)) {
if ((depthChargeX >= enemyX - 50 && depthChargeX <= enemyX + 50) || (depthChargeY >= enemyY - 50 && depthChargeY <= enemyY + 50) && isActivated == true)
{
enemy.IsAlive = false;
hit.Play();
wreck2.IsAlive = true;wreck2.Update(gameTime); } }
If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button. Would I then send that bool to the OnGameEvent or must I create a new class like in one in one of the examples. I am a little confused still I must admit. But I suppose the HandleGameEvent in the form listens to the event raised. And
game.GameEvent += HandleGameEvent;
Is where you subscribe to the event raised by the game. I also get: The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing.
larsp777 wrote:
The name 'HandleGameEvent' does not exist in the current context but I suppose something is missing
You are missing the event handler itself. Assuming that it just passed across EventArgs, the handler would look like this:
private void HandleGameEvent(object sender, EventArgs e)
{
}larsp777 wrote:
If it isn´t in range I want to send a bool or something to my form so I can e.g. enable or disable a button.
A simple event args class that looks like this should help:
public class GameRangeEventArgs : EventArgs
{
public GameRangeEventArgs(bool isInRange) : base()
{
IsInRange = isInRange;
}
public bool IsInRange { get; private set; }
}Now, in your Game, you would declare this as:
public event EventHandler<GameRangeEventArgs> GameRangeEvent;
To use this from your game code, I would typically have a helper method that looks like this:
private void RaiseGameRangeEvent(bool isInRange)
{
var handler = GameRangeEvent;
if (handler != null)
{
handler(this, new GameRangeEventArgs(isInRange));
}
}Now, in your code you simply call this method and the event is raised if there is an event handler attached to it in the form. So, what would this look like in the form? Well, you will have something like this:
game.GameRangeEvent += HandleGameRangeEvent;
...
private void HandleGameRangeEvent(object sender, GameRangeEventArgs e)
{
}