Always on Top

AOT(always on top)
by Neo-Bahamut aka Wurstinator
Version: 1.1

Hi,
one of the problems with RMXP is the fact, that when the RGSS Player (the window in which your game is running) is not selected the music continues while the game stops. To prevent this, I wrote this code which disables this.

Requirements
  • the script above main of course
  • the aot.exe in your project folder
  • Windows, I think but as I only could test on XP and 7 I am not sure about this

Screenshots / Video
Screenshots would be not very useful. I uploaded a video some time ago:
[youtube]http://www.youtube.com/watch?v=bqQ9klIwmLM[/youtube]

Script
  1. # RGSS Player running always (not only when selected):
  2. # Copy the aot.exe into your project directory and place this script into your
  3. # script editor:
  4.  
  5. IO.popen('aot.exe 0.001')
  6. # The number behind aot.exe represents the sleep time between a check.
  7. # For example 0.05 means the programm will check if the RGSS Player is not on top
  8. # every 50 milliseconds. I recommend something like 0.001
  9.  
  10. # Thanks to Skade for the idea to use IO.popen
  11.  
  12. # There may occure problems with print windows. Usually you won't be able to close
  13. # them with pressing "ok" but the close button (red X) should work.
  14.  
  15. # Should only work on Windows, sorry :(
Expand to see the code.


Download
aot.exe: http://www.multiupload.com/FC9IVWLU2H
Demo project: http://www.multiupload.com/5C2ZADLAG0

Source
Since aot.exe is compiled here you can see its source code:
  1. require 'win32api'
  2.  
  3. Smallest_Float = 0.00000000000000001
  4.  
  5. Readini = Win32API.new('kernel32', 'GetPrivateProfileStringA', 'pppplp', 'l')
  6. Findwindow = Win32API.new('user32', 'FindWindowA', 'pp', 'l')
  7.  
  8. def get_rgss_handel(ini = ".\\Game.ini")
  9.     if !FileTest.exist?(ini)
  10.         exit
  11.     end
  12.   game_name = "\0" * 256
  13.   Readini.call('Game', 'Title', '', game_name, 255, ini)
  14.   game_name.delete!("\0")
  15.   return Findwindow.call('RGSS Player', game_name)
  16. end
  17.  
  18. GetAct = Win32API.new('user32.dll', 'GetActiveWindow', '', 'i')
  19. SetAct = Win32API.new('user32.dll', 'SetActiveWindow', 'i', 'i')
  20. SetFor = Win32API.new('user32.dll', 'SetForegroundWindow', 'i', 'i')
  21. IsWnd = Win32API.new('user32.dll', 'IsWindow', 'i', 'i')
  22.  
  23. $handel = get_rgss_handel
  24.  
  25. cores = ENV['NUMBER_OF_PROCESSORS'].to_i
  26. sleeptime = ARGV[0].to_f
  27.  
  28. loop do
  29.     is_window = IsWnd.call($handel)
  30.     if is_window == 0
  31.         exit
  32.     end
  33.     act = GetAct.call
  34.     if act != $handel
  35.     #   SetAct.call($handel)
  36.         SetFor.call($handel)
  37.     end
  38.     sleep(sleeptime)
  39. end
Expand to see the code.


Bugs / Problems
Depending on the sleep time it may come to a conflict with the RMXP window which is shown when using the print/puts/p methods. You may not be able to close it with its "Ok" button so try the close button (red X).
Hey not bad. This problem never really bugged me too much, but its nice someone made a fix for it.
But why did you use a .exe instead of a .dll? .Exes are clunkier and do the same things as a dll (and are also fairly obtrusive).
Looks potentially useful. I know a few peeps complaining about music synched with cutscenes that get messed up when you clicked outside the window; now this will force players to stay ingame. Grats on figuring this out.
@ Untra: The source of aot.exe is written in Ruby and afaik there is no possibility to convert a Ruby script to a dll but only to an exe.

@ Regi: Thanks :)
What do the Smallest_Float and cores variables stand for ?
Nice script. I'll probably use it. Thanks
Really good.

A lot of people wanted a way to stop bmg when out of the maker window.
Now there will be no problem with musics sincronized with cutscenes. :D
This is actually pretty cool. It's too bad it couldn't have been a .dll file programmed in C++ or C#, but we have to take what we get.
@ berka: They're the rest of older versions :)
Smallest_Float just to be used as the smallest possible sleep time and the cores to set the sleep time depending on the numer of available CPU cores as the aot.exe runs as a seconds Thread.

@ Atoa & Glitchfinder: Thanks :)
This is *awesome*. When I finish Act III and start revising the game, I'm definitely going to use this. I'm one of those people who have music synched to cutscenes, so this is immensely useful.
:( you have no idea how useful this script would have been three years ago :x

Brilliant script for a common problem. My use would have been in an online game (so that player movements are always seen), I'm not working on that game any more though. :toot:
This really does look purposeful... I wonder about the small skip that seems to be there though when you're switching to another window... is that really a skip, or does it just look like it in the video?
The problem is, with being able to design non-skip assuming scripts, if there's a little lag, that could easily spawn little desynchs... It's not on your bugs list, so I figure it's either known and unavoidable (therefore not a bug), or you thought "Hey, it's just a split second!", in which case como's imagines online game wouldn't have worked right after all ;)

Keep up the good work!

As a side note... 'handle' is quite differently spelled than 'Handel' XD
@Sailerius: Thanks :)
@como: Yes, I heard about Vengeance Online :)
Anyway, even I you don't need it, thanks.
@BlueScope: The small stop (I doesn't skip but stop ;) can occur depending on the used sleep time. With 0.001 I don't have that problem now.
Anyway, it's not on the bug list for both reasons you mentioned. I think I cannot fix it, it is just a thing of the OS and the APIs with some sleep time values, but also as long as the player doesn't WANT the game to desynch the delay will be only a second or something.
I always forget whether it is handel or handle ^^"
Nice script. I like the idea though I may or may not use it.

Offtopic: I'm curious how you compiled aot.exe, I've been wanting to compile a coupile of my ruby apps.
Yay thanks for this *.*

Now a Dance dance revolution-esque minigame is possible now ^^
@ gameguy27: http://rubyonwindows.blogspot.com/2009/ ... ilder.html
@ Mimi Chan: I don't like music games :P
But anyway, thanks :)
Thanks neo. I'll look into it ^_^

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71126