Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Finding the RGSS Player Window more reliably

poccil

Sponsor

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.

Code:
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).
 
For me, the shortest and better way to obtain the current RGSS window hwnd id is with:
Code:
GetActiveWindow = Win32API.new('user32', 'GetActiveWindow', '', 'L')
current_window = GetActiveWindow.call
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top