Game Development Community

Capturing Close Event (Pro Users)

by William Lee Sims · in Torque Game Builder · 08/02/2010 (2:01 am) · 5 replies

I thought this existed in the forums, but couldn't find it anywhere. Anyway... since I just added it to my game I thought I'd share:

In platformWin32/winWindows32.cc, search for SC_CLOSE (about line 860 or 880). You'll see code like
#ifdef TORQUE_TOOLS
      // Catch Close button press event [1/5/2007 justind]
      case SC_CLOSE:
         if (Con::isFunction("onClosePressed"))
         {
            Con::executef( 1, "onClosePressed" );
            return 0;
         }
         break;
#endif // TORQUE_TOOLS

Simply remove the #ifdef and #endif lines and recompile.

Somewhere in your scripts add
function onClosePressed()
{
  MessageBoxOKCancel( "Game Exit", "Are you sure you want to exit?", "quit();" );
}

If you need to save your game or whatever, you can change the "quit();" to call a function of yours to save before quitting the game.

Later!

#1
08/02/2010 (2:27 pm)
Nice, thanks for this one William!
#2
08/15/2010 (6:55 pm)
This doesn't seem to work for me with the last version of TGB 1.7.5 :/ I had it working fine with the previous version, not sure what's changed to break it...
#3
08/16/2010 (4:32 am)
I'm only using this in 1.7.4. I won't be at my computer for a while, but I do have a 1.7.5 test project that I can test this in tomorrow. I'll let you know what I find.
#4
08/16/2010 (4:43 pm)
I just tried this on a fresh 1.7.5 install and it worked just fine. I guess I can only suggest the regular list of things to try: make sure you've built the correct version (probably "Release") and that you've removed the "#ifdef".
#5
08/19/2010 (4:47 pm)
Hmm it seems I had just been running the wrong TGBgame.exe. This works like a charm now, thanks! =)