Finding the RGSS Player Window more reliably
This script section I created is a more reliable way of finding the RGSS window handle than the usual method of reading the game's game.ini file. This function is meant to be used in conjunction with other scripts that rely on the RGSS Player window, including mouse systems, screenshot systems... This approach was first used in my Pokemon Essentials project and I hope it will be of use to others.
Specifically, the approach followed by this code is more robust against changes to the window's title bar (for example, when checking the game's FPS), as well as against cases where more than one instance of the game is running; the approach instead finds the window belonging to the process the game belongs in, which is unique to each instance of the game.
The script code follows.
class Win32API
@@RGSSWINDOW=nil
@@GetCurrentThreadId=Win32API.new('kernel32','GetCurrentThreadId', '%w()','l')
@@GetWindowThreadProcessId=Win32API.new('user32','GetWindowThreadProcessId', '%w(l p)','l')
@@FindWindowEx=Win32API.new('user32','FindWindowEx', '%w(l l p p)','l')
def Win32API.pbFindRgssWindow
return @@RGSSWINDOW if @@RGSSWINDOW
processid=[0].pack('l')
threadid=@@GetCurrentThreadId.call
nextwindow=0
begin
nextwindow=@@FindWindowEx.call(0,nextwindow,"RGSS Player",0)
if nextwindow
wndthreadid=@@GetWindowThreadProcessId.call(nextwindow,processid)
if wndthreadid==threadid
@@RGSSWINDOW=nextwindow
return @@RGSSWINDOW
end
end
end until nextwindow==0
raise "Can't find RGSS player window"
return 0
end
end
Calling "Win32API.pbFindRgssWindow" retrieves a handle to the game's window, or raises an exception if the window cannot be found (to prevent this, uncomment the line "raise 'Can't find RGSS player window"; then the function returns 0 if the window cannot be found).
