4

I have recently noticed that sometimes the XNA property Game.IsActive does not always correctly reflect the state of the game window.

More specifically, Game.IsActive = true even if the game window is clearly not active (e.g. the top window bar is colored as inactive). I can reproduce this behaviour with an active Firefox window slightly overlapping the game window.

What could be the issue here?

As requested, this picture shows the problem: enter image description here

The game window is in the background, the browser (showing Stack Overflow) is in the foreground (and active), yet the property Game.IsActive is true (as you see in the visual studio output (magenta "circle") which is being written out every Update().

Could it be a problem, that I create a static reference of the XNA class Game in my core game class and use that?

ares_games
  • 1,019
  • 2
  • 15
  • 32
  • I've used `Game.IsActive` myself and I have not gotten any issues with this. Chances are that the issue is external. Could you show a picture? (Or .gif record) about how you reproduce it? – Steven Nov 16 '17 at 07:23
  • I have added the screenshot as requested. – ares_games Nov 16 '17 at 14:20
  • Really weird, I've been trying to reproduce it with my own game with XNA, but can't check the values in real-time. Though it does hit the `Game.isactive` everytime. What might eventually help is to bring a if-statement that only checks when the `Game.isactive` is false (or `!game.isactive=true` . In case you want to use a pause function. I've also defined the `!Game.isactive` at the very top of the `Update()` statement. – Steven Nov 18 '17 at 16:15
  • 2
    Game always starts with IsActive = true (framework drawback). Is the scenario present when you switch in between windows? – Pontus Magnusson Nov 27 '17 at 14:17
  • Thank you for that comment. This could actually be the problem. I experience the issue only at the very beginning right after the start of the application. Once I switched between windows, this never occurs again. Is there any way to force a "refresh" of the IsActive property? – ares_games Nov 27 '17 at 18:11

1 Answers1

2

As mentioned in the comments, the IsActive-Property has it's weaknesses.

You should subscribe to the events Game.Activated and Game.Deactivated. With an additional focus to your window on startup, you'll be fine.

IMHO: I don't like such "magic" properties like IsActive. Events are much more precise..

Example:

public class MyGame : Game
{
   public MyGame()
   {
      // Should move to Initialize-method..
      this.Activated += ActivateMyGame;
      this.Deactivated += DeactivateMyGame;
      this.IAmActive = false;
      // do you init-stuff

      // bring you window to front. After this point the "Game.IsActive"
      // will be set correctly, while IAmActive is correct from the beginning.
   }

   public bool IAmActive { get; set; }

   public void ActivateMyGame(object sendet, EventArgs args)
   {
      IAmActive = true;
   }

   public void DeactivateMyGame(object sendet, EventArgs args)
   {
      IAmActive = false;
   }
}

Everytime the Game gets or looses focus the methods ActivateMyGame and DeactivateMyGame are called. The Property IAmActive is false by default, which is the difference to IsActive.

kara
  • 3,205
  • 4
  • 20
  • 34
  • Could you please explain that in a bit more detail? What do you mean with "additional focus to the window startup"? How do I subscribe to "Game.Activated" ? Thanks! – ares_games Dec 01 '17 at 12:54
  • 1
    If i understand you correct, you problem is, that your game can start without being in front and focus of the user. If you're running in an Form you can perform 'BringToFront();' to force this. After this your IsActive-Property is correct. – kara Dec 01 '17 at 13:02
  • 1
    If you did't work with events you should read this: https://learn.microsoft.com/en-us/dotnet/standard/events/ – kara Dec 01 '17 at 13:14
  • 1
    Thank you for your answer but unfortunately the "Events"-approach doesn't work either. This issue is easily reproducible: if you change the active window away from the game window after you double-clicked the executable but before the window has actually opened (or within a few seconds) the game window will be considered active (the ActivateMyGame event is triggered) but actually it will not be the active window. Forcing the game window to be active would work, but I do not use forms. – ares_games Dec 02 '17 at 02:20
  • 1
    If you don't use Forms you can use the win32-API: https://stackoverflow.com/questions/6228089/how-do-i-bring-an-unmanaged-application-window-to-front-and-make-it-the-active After bringing it to front the property "IsActive" will work correctly. If you want to make sure, that nothing happens before the window is in the very front, you have to make your own activity-flag, as shown in the example. – kara Dec 04 '17 at 07:59