Acting on Windows Phone 7 events

Monkey Targets Forums/XNA/Acting on Windows Phone 7 events

Foppy(Posted 2011) [#1]
While working on a Monkey game for Windows Phone 7, there are a few things that I am wondering about, concerning what to do when the game is interrupted. I hope someone has a bit more experience with Windows Phone 7 than I have and can comment on one or more of my questions below:

1) Is it a good idea to use SaveState() and LoadState() to save a hi-score and reload the hi-score when the game is run again?

Another point: I read somewhere that MicroSoft expect a WP7 application to respond in a predictable manner to the use of the Back button. (When on title screen, exit game; during gameplay, pause game and present option to stop playing or resume playing.). This leads me to the following question:

2) How to read out the Back button? Can this still be achieved through Monkey or is this to be done in Visual Studio?

I also read about phone events such as Deactivated and Activated, and that the application should try to restore itself so as to give the impression of it being still in the same session, even though it was actually stopped and restarted. (Since WP7 really only has one active application running at any time, as far as I understand.)

3) Should SaveState() and LoadState() be used for this? Or should this be done in Visual Studio? Or is this to some extend done by the phone itself?

4) Microsoft documentation on this point differentiates between:

a) a "State" object for storing the application state upon Deactivation, so it can be restored upon Activating;

b) "Isolated Storage", where data should also be stored upon Deactivation: if the app is completely terminated, the State object in memory will not be preserved so you need the Isolated Storage to reconstruct the app later on.

I think Monkey is only concerned with the Isolated Storage?

-----------------------------------------------------------------

Edit:

After having thought about it some more, perhaps the best approach would be to use SaveState() and LoadState() to save and restore the current game state (including hi-score, and current score, level, and lives remaining if the player was actually in the middle of a game at that moment) upon game initialization as well as upon Activate and Deactivate events.


dopeyrulz(Posted 2011) [#2]
Foppy,

I'm certainly looking to investigate some of these issues myself very soon. Currently (from memory) I don't know if the Load/Save State stuff is implement yet (need to look through the code again).

I was just about to check the back button myself, but I think you should/could be able to read it through JoyDown/JoyHit (JOY_BACK?). Some commentary I recently read indicated this might be it based on the internal monkey code i've looked through and the XNA code).

The overall 'Tombstone' process i've yet to play around with.


Foppy(Posted 2011) [#3]
Thanks Matt. Currently I can use SaveState/LoadState to store and reload the hi-score. It does work in the emulator as long as I don't close it. When I close the emulator and deploy again, the hi-score is lost. Perhaps this is to be expected, corresponding to resetting the phone's Isolated Storage space.

Now I will try to use this same mechanism, but to store a "snapshot" of the game: hi-score, and (if a game is in progress) information to recreate the state of the game such as score, lives remaining, level. When the game is interrupted (OnSuspend) it will call SaveState with this data. Then when the game is started again (OnCreate or OnResume) it will use LoadState to get the data, and use it to restore the game. (If in the middle of a game, it should start out in a pauzed mode.) I hope Monkey's events correspond to the correct WP7 events. (Or else this would have to be done in the target code.)

I will look into the Back button and the joypad-events suggestion as well.


dopeyrulz(Posted 2011) [#4]
Foppy,

Great - thanks for the update. Glad to hear the state stuff is working!


Foppy(Posted 2011) [#5]
I am at a bit of a loss with this. (Trying to act on the Back button.)

If I add a check for JoyHit(JOY_BACK) in the Monkey code, to return the game to the tile screen if true, this has the effect of actually ending the application upon pressing the Back button when tested in the emulator.

If I do not add that check, pressing the back button in the emulator does nothing. (Which seems strange?)

I was not able to find the code that is "responsible" for ending the application in the XNA translation. Perhaps it is normal behavior for the game to be ended at this point, but then the question would be "how to intercept this event and do something else?". (Such as presenting the option to stop or continue, or cancelling the Back operation.)

I now have a book about WP7 programming that explains how to write such an event, but I can't figure out where in the thousands lines of XNA/C# code I should put this. It is made even more difficult because it has to become part of the game that has the Monkey code as its source. When I try to add the event handler to the generated code, it disappears the next time I build from Monkey.


dopeyrulz(Posted 2011) [#6]
Foppy,
I'm interested in getting this working as well so will take a look later today.

If you want to add your own xna code you will need to write a wrapper in monkey by creating a module. There is some info around the site to help do this.