F12 Reset Fix


F12 Reset Fix
​


 
Introduction
F12 reset can sometimes be a pain in the arse. This script fixes that by spawning a new Game.exe. It can also be used if you want to use the editor whilst play-testing it. You can also configure to show the console on test play.


Features
- Saves an RM user from death by F12 reset.

Screenshots
Not needed.

Script

Spoiler
Code:
#==============================================================================
#   ** F12 Reset Fix
#   Author: Acezon
#   Date: 2 June 2013
#------------------------------------------------------------------------------
#   Version 2.1
#   - Fixed issue where game window is not in focus
#     when Console_on is set to false
#   - Now compatible with Tsuki's Test Edit script
#   Version 2.0
#   - Console option added
#   - Automatically focuses window after pressing F12
#   Version 1.1
#   - Respawning the exe was better
#   Version 1.0
#   - Initial Release
#------------------------------------------------------------------------------
#   Just credit me. Free to use for commercial/non-commercial games.
#   Thanks to Tsukihime and Cidiomar for the console scriptlet
#==============================================================================

$imported = {} if $imported.nil?
$imported["Acezon-F12ResetFix"] = true

#==============================================================================
# ** START Configuration
#==============================================================================
module Config
  Console_on = false       # duh
end
#==============================================================================
# ** END Configuration
#==============================================================================

alias f12_reset_fix rgss_main
def rgss_main(*args, &block)
  f12_reset_fix(*args) do
    if $run_once_f12
      pid = spawn ($TEST ? 'Game.exe test' : 'Game')
      # Tell OS to ignore exit status
      Process.detach(pid)
      sleep(0.01)
      exit
    end
    $run_once_f12 = true
    # Run default rgss_main
    block.call
  end
end

module SceneManager
  class << self
    alias :acezon_f12_first :first_scene_class
  end

  def self.first_scene_class
    focus_game_window
    acezon_f12_first
  end

  def self.focus_game_window
    # Just to prevent re-spawning the console since
    # Tsuki uses this same part in his Test Edit script
    if !$imported["TH_TestEdit"]
      # Get game window text
      console_w = Win32API.new('user32','GetForegroundWindow', 'V', 'L').call
      buf_len = Win32API.new('user32','GetWindowTextLength', 'L', 'I').call(console_w)
      str = ' ' * (buf_len + 1)
      Win32API.new('user32', 'GetWindowText', 'LPI', 'I').call(console_w , str, str.length)

      if Config::Console_on
        # Initiate console
        Win32API.new('kernel32.dll', 'AllocConsole', '', '').call
        Win32API.new('kernel32.dll', 'SetConsoleTitle', 'P', '').call('RGSS3 Console')
        $stdout.reopen('CONOUT$')
      end

      # Sometimes pressing F12 will put the editor in focus first,
      # so we have to remove the program's name
      game_title = str.strip
      game_title.sub! ' - RPG Maker VX Ace', ''

      # Set game window to be foreground
      hwnd = Win32API.new('user32.dll', 'FindWindow', 'PP','N').call(0, game_title)
      Win32API.new('user32.dll', 'SetForegroundWindow', 'P', '').call(hwnd)
    end
  end
end



Log


Version 2.1


- Fixed issue where game window is not in focus
when Console_on is set to false
- Now compatible with Tsuki's Test Edit script
Version 2.0
- Console option added
- Automatically focuses window after pressing F12
Version 1.1
- Respawning the exe was better
Version 1.0
- Initial Release


Credit and Thanks


- Read the script header
i needed this badly!
UPDATE:

It appears that it doesn't work on some events, so I decided to execute a new Game.exe and removed the former.
It might be useful to correct some bugs related to F12 that are irritating me at this moment. Thanks!

(These problems are related to the RGSS3 "reinitializing" the game window without reloading the scripts, I'm sure).
EDIT: I just changed the thread's title since the script can be a general-purpose F12 reset fix.
Thank you, it helped me a lot! 😀
Really nice. Would it perhaps be possible to also recreate  the console window? Right now you lose the console after pressing [F12].
@Napoleon:

I think you can add this below the script. This is from Tsukihime's Test Play Script. Just set the Console_on to false if you want to export your project.

Code:
module Config  Console_on = trueendmodule SceneManager  def self.first_scene_class    if Config::Console_on      Win32API.new('kernel32.dll', 'AllocConsole', '', '').call      Win32API.new('kernel32.dll', 'SetConsoleTitle', 'P', '').call('RGSS3 Console')      $stdout.reopen('CONOUT$')    end    $BTEST ? Scene_Battle : Scene_Title  endend
Great that works. I use $TEST instead of Console_on but that's more or less the same.

But the console does steal the focus from the main window. Can a winapi or something set the focus back to the game window?
I don't think so. The Game.exe must execute first before initializing the console, as indicated in the script. I think the reason why it's in the back is the editor launches it first before the Game.exe 
But we have the process id (can be used to retrieve the hwnd (window handle)) and I saw that you also knew how to use the win32api. This api has the setfocus() function http://msdn.microsoft.com/en-us/library/windows/desktop/ms646312%28v=vs.85%29.aspx so it should be possible in theory I think. But I'm new to Ruby so I don't yet know how to call these things.

I couldn't find a full working solution via Google to set the focus for a window through Ruby.
Is it normal that the screen is reduced ?

I mean like if I : ALT+TAB.
@Napoleon - Here's your fix:
 

module SceneManager def self.first_scene_class if $TEST # Get game window text console_w = Win32API.new('user32','GetForegroundWindow', 'V', 'L').call buf_len = Win32API.new('user32','GetWindowTextLength', 'L', 'I').call(console_w) str = ' ' * (buf_len + 1) Win32API.new('user32', 'GetWindowText', 'LPI', 'I').call(console_w , str, str.length) # Initiate console Win32API.new('kernel32.dll', 'AllocConsole', '', '').call Win32API.new('kernel32.dll', 'SetConsoleTitle', 'P', '').call('RGSS3 Console') $stdout.reopen('CONOUT$') # Sometimes pressing F12 will put the editor in focus first, # so we have to remove the program's name game_title = str.strip game_title.sub! ' - RPG Maker VX Ace', '' # Set game window to be foreground hwnd = Win32API.new('user32.dll', 'FindWindow', 'PP','N').call(0, game_title) Win32API.new('user32.dll', 'SetForegroundWindow', 'P', '').call(hwnd) end $BTEST ? Scene_Battle : Scene_Title endend@Chaos17 - It doesn't happen to me. Maybe you have a script that changes the resolution.
UPDATED to version 2.0. Read the first post for changes.
Can we use these in commercial projects?
Sure. But some credit would be great. 😀
UPDATE

 Version 2.1

 - Fixed issue where game window is not in focus

   when Console_on is set to false

 - Now compatible with Tsuki's Test Edit script
Your latest version crashes right away for me. Tries to call new on nil class line 22 in SceneManager.run
I misplaced a line in the script; my bad. It's fixed now, thank you for pointing that out.
i manage to fix the return to title command (it have the same bugs as f12 reset fix on some scripts).

by installing this script then changing this line in the SceneEnd:

SceneManager.goto(SceneTitle)to

      pid = spawn ($TEST ? 'Game.exe test' : 'Game')      # Tell OS to ignore exit status      Process.detach(pid)      sleep(0.01)      exit      SceneManager.run
but i have a problem. i have a script that can launch Scene_Load in game. but when loading the script break some other script (it's new jet mouse system.. because it's old exe. same problem with f12 and return to title).

is there a way to make child class of Scene_Load that when loading... close the game exe. made new game.exe. then loading the game?

Original thread: https://forums.rpgmakerweb.com/threads/f12-reset-fix.8000/