F12 Reset Fix

Discussion in 'RGSS3 Scripts (RMVX Ace)' started by Acezon, Jan 7, 2013.

  1. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A

    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

    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
     
    Last edited by a moderator: Oct 10, 2016
    #1
  2. ShinGamix

    ShinGamix DS Style 4Ever! Veteran

    Messages:
    3,906
    Likes Received:
    448
    Location:
    Alien Ship
    First Language:
    April Fools
    Primarily Uses:
    N/A
    i needed this badly!
     
    #2
  3. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    UPDATE:

    It appears that it doesn't work on some events, so I decided to execute a new Game.exe and removed the former.
     
    #3
  4. Rafael_Sol_Maker

    Rafael_Sol_Maker Almighty God Jabu Veteran

    Messages:
    122
    Likes Received:
    36
    Location:
    Brazil
    First Language:
    Portuguese
    Primarily Uses:
    RMMV
    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).
     
    #4
  5. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    EDIT: I just changed the thread's title since the script can be a general-purpose F12 reset fix.
     
    #5
  6. Chaos17

    Chaos17 Dreamer Veteran

    Messages:
    1,253
    Likes Received:
    420
    Location:
    France
    First Language:
    French
    Thank you, it helped me a lot! :D
     
    #6
  7. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    Really nice. Would it perhaps be possible to also recreate  the console window? Right now you lose the console after pressing [F12].
     
    #7
  8. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    @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
     
    Last edited by a moderator: Feb 12, 2013
    #8
  9. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    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?
     
    #9
  10. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    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 
     
    #10
  11. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    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.
     
    Last edited by a moderator: Feb 12, 2013
    #11
  12. Chaos17

    Chaos17 Dreamer Veteran

    Messages:
    1,253
    Likes Received:
    420
    Location:
    France
    First Language:
    French
    Is it normal that the screen is reduced ?

    I mean like if I : ALT+TAB.
     
    #12
  13. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    @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.
     
    Last edited by a moderator: Feb 12, 2013
    #13
    Napoleon likes this.
  14. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    UPDATED to version 2.0. Read the first post for changes.
     
    #14
  15. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    Can we use these in commercial projects?
     
    #15
  16. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    Sure. But some credit would be great. :D
     
    #16
  17. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    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
     
    #17
  18. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    Your latest version crashes right away for me. Tries to call new on nil class line 22 in SceneManager.run
     
    #18
  19. Acezon

    Acezon Om Nom Nom Veteran

    Messages:
    131
    Likes Received:
    25
    Location:
    Philippines
    First Language:
    English
    Primarily Uses:
    N/A
    I misplaced a line in the script; my bad. It's fixed now, thank you for pointing that out.
     
    #19
  20. estriole

    estriole Veteran Veteran

    Messages:
    1,030
    Likes Received:
    335
    First Language:
    indonesian
    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?
     
    #20

Share This Page