Keeping the Game.Exe 'In Focus' (yeah, gonna get a lot of Nopes here)

But the thread does say it all. And not only would this be something I would like see come to fruition, but many others. This would certainly be enjoyed by those who like to synchronize music in their games.

I expect that this would be by way of the Win32DLL. I once wrote a piece of software over a decade ago that kept a Visual Basic window running constantly, even if I was operating another windows application. So I do feel that this would be a good request, regardless of the nay-sayers.

This could quite well be a cross-platform system, for RPGMaker XP, VX and VXAce. seeing as this would be a module that affects the engine by way of Ruby code whilst accessing the Win32DLL. It might be just a standalone script, or one that brings up a temporary TSR (Terminate Stay Resident) background program. But I believe that an endeavor like this would be appreciated.

OR... is there already a feature in play (other than RMMV).
Both ForeverZer0 and I have made these scripts some years ago. Attached ZIP of the latest version I made (you can thank @Jaiden for that).
This is actually a feature I added to XPA. This is a revised, stand-alone version of that script.
Credit is necessary. Commercial use is allowed.
*takes a look, sees a DLL* When I read the title, the first thing that came to my mind was "If you intercept and block WM_ACTIVATE and WM_FOCUSCHANGE, this should already do the trick". xD C/C++ FTW. Funny how I probably would have given a NOPE like 7 years ago.
Yeah, I can vouch for KK20's "Prevent Deactivation". I have been using it in my project pretty much since I started working on it. Works great. It makes music timing for cutscenes perfect.

I mean, there isn't much to thank me for, I just bothered him about implementing a "toggle" so I could enable/disable it in my options menu 😛

Screen_Shot_2019-03-22_at_10.46.30_AM.png
I once wrote a piece of software over a decade ago that kept a Visual Basic window running constantly, even if I was operating another windows application

form.show vbmodal

it's a property of windows,.... no mystery there lol.
Credit is necessary. Commercial use is allowed.
Thanks for the file. Taking out most of the game_system and Game_temp makes it work for vx ace.
Just can't toggle it. Which I'm good with that.
Much Appreciated!

However, I have a bit of a quandry. I am getting an error:
Script ' ' line 39: NoMethodError occurred.
undefined method 'getbyte' for "\001":String


This being a brand new project, copying the contents of the rb file into the script editor, and having the MessageIntercept.dll beign within the project's root folder.

Just in case, the line in question is
Code:
update_if_game_in_focus if MessageIntercept::Flag_Deactivate.getbyte(0) == 1
and while I detect that the getbyte statement is in the newest versions of Ruby, I fear it is not used in RMXP's 1.8 🙁 ... Hopefully, there is a script which adds the method???

Also, I checked ForeverZer0's 2014 version at Chaos. *sigh* It crashes my browser, and actively doesn't function.

@Jaiden. (And off-topic): Your config controls SFX Audio. Likely, it doesn't affect Battle Animation audio. I have a Battle Animation Audio fix. 😉 Yeah, it's old....

Scr-XP.png
AUDIO FIX - Battle Animations
by DerVVulfman (May 14, 2006)
This module adjusts the volume of the Sound being played in the Battle Animations based on an editable value stored IN Game_System. Without it, you will only adjust the volume of the music and sound effects OTHER than battle animations.​
Oh right, forgot about that. You just use String#[] to get the byte. So it should be
Code:
update_if_game_in_focus if MessageIntercept::Flag_Deactivate[0] == 1
@Jaiden. (And off-topic): Your config controls SFX Audio. Likely, it doesn't affect Battle Animation audio. I have a Battle Animation Audio fix. 😉 Yeah, it's old....

Scr-XP.png
AUDIO FIX - Battle Animations
by DerVVulfman (May 14, 2006)
This module adjusts the volume of the Sound being played in the Battle Animations based on an editable value stored IN Game_System. Without it, you will only adjust the volume of the music and sound effects OTHER than battle animations.​
Thanks, but I actually got battle animations covered. They too are reduced with the SFX controls. 😛
WOOT! That keeps it active, much like some background programs I made years ago (with Visual Basic ... 4!).

Due credit, and commercial use is fine, right? Sounds a lot like me. I think this one needs to be spread out to other sites (like me posting it in Save-Point with clear due credit of course).

Guess next step is "keeping on top" in a manner so no other windows cover the screen (an option of course). But this works! 😀

And I hope Jaiden likes the script suggestion 😉 XD Oh, yeah. Ya beat me to the reply. Hehehe!!!
think this one needs to be spread out to other sites (like me posting it in Save-Point with clear due credit of course).

As I stated.... I'd post it, and with 100% clear due credits. Here's the thread itself. And the thread points back here if anyone had any ambiguities on rights or terms..
*thumbs up*
Though, the Ruby 2.0 version (which is actually supposed to be 1.9.2, used only in VXA) does not work. I don't mess around with VXA much, but here's what I came up with
Code:
=begin
================================================================================
 Prevent Deactivation
 Author: KK20
 Version: 1.1
--------------------------------------------------------------------------------
 [ Description ]
 When the game window is no longer the active window, it normally freezes the
 game from updating. This script will allow games to still run in the background.

 [ Instructions ]
 You can enable / disable this feature in-game using the script call:
 
              $game_system.keep_game_active = true / false
              
 This is useful for custom menu options so that the player can choose their
 preference of whether the game runs in the background or not.
 
 Script should be placed as low as possible in your script list, but still
 above Main.

 It is highly advised to not modify this script unless you know what you are
 doing.
================================================================================
=end

module MessageIntercept
  Start = Win32API.new('MessageIntercept', 'Init_Deactivate', 'pp', 'i')
  Toggle = Win32API.new('MessageIntercept', 'KeepGameActive', 'i', '')
  Flag_Deactivate = [1].pack('C*')
end

module Input
  class << self
    alias update_if_game_in_focus update
  end
  # Only accept player input if the game window is in focus
  def self.update
    update_if_game_in_focus if MessageIntercept::Flag_Deactivate.getbyte(0) == 1
  end
end

class Game_System
  attr_reader :keep_game_active
 
  alias init_for_no_deactivate initialize
  def initialize
    @keep_game_active = true
    init_for_no_deactivate
  end
 
  def keep_game_active=(bool)
    @keep_game_active = bool
    MessageIntercept::Toggle.call(@keep_game_active ? 1 : 0)
  end
end

module DataManager
  class << self
    alias load_no_deactivate_settings extract_save_contents
    alias init_no_deactivate_settings setup_new_game
  end
 
  def self.setup_new_game
    $game_system.keep_game_active = true
    init_no_deactivate_settings
  end

  def self.extract_save_contents(contents)
    load_no_deactivate_settings(contents)
    Scene_Base.modify_update
  end
end


class Scene_Base
  # When loading a save, need to make DLL call to enable/disable keeping the
  # game active
  alias original_update_no_deactivate update
  def self.modify_update(revert = false)
    if revert
      alias update original_update_no_deactivate
    else
      alias update no_deactivate_update
    end
  end
 
  def no_deactivate_update
    MessageIntercept::Toggle.call($game_system.keep_game_active ? 1 : 0)
    original_update_no_deactivate
    Scene_Base.modify_update(true)
  end

end


if MessageIntercept::Start.call('.\\Game.ini', MessageIntercept::Flag_Deactivate) == -1
  raise 'ERROR: Prevent Deactivation failed to set new window procedure!'
end

As for VX, I don't own it, so I can't test it.
*THUMBS UP* And Correcting in the thread for VXAce version.

As for VX, I don't own it, so I can't test it.
I can. It works fine. 😀
Warning. F12 Stack error bug detected.... and remedy known.

Line 36 is an alias statement altering the update method within Input. It's ALWAYS tricky, and tends to cause stack overflows when aliasing anything within these classes and modules without adequate protection.

Replace
Code:
    alias update_if_game_in_focus update
with
Code:
    if @intercept_f12_stack.nil?
      alias update_if_game_in_focus update
      @intercept_f12_stack = true
    end

This method of wrapping the alias was taught to me by SephirothSpawn, and will ensure it only aliases the statement once. And with that, no need for scripts that disable the F12 key.
Right, but I figure a lot of scripts don't do this safeguard anyways, so everyone just ends up throwing in a fix-all solution like https://forum.chaos-project.com/index.php/topic,13629.0.html or disabling F12 as you mentioned.

Since I work with RGSS3 + XP, I can just use rgss_main and never have to worry about it.

Original thread: https://forums.rpgmakerweb.com/threads/keeping-the-game-exe-in-focus-yeah-gonna-get-a-lot-of-nopes-here.107021/