Well, ever since I started making lists and prioritizing my tasks, I’ve mysteriously been getting things accomplished left and right. (That and a bit of a temporary lay-off on the job side. You have more time when you don’t have to go to work…oh well.) In my last project (the “Custom Display Menu”) I figured out how to load non-Blender programs via Python. I had been wondering how to do that for a while, and after I learned I thought I’d tackle an idea I had a while back.
Here it is: “Arcade Loader” - complete with 4 of my favorite freeware games.
They are Windows games, but the idea is cross-platform. I used the os module instead of an operating system specific one, which I believe is referred to as making my script more “Pythonic”. Here’s the main driving script:
import os
cont = GameLogic.getCurrentController()
own = cont.getOwner()
mouseOver = cont.getSensor("Over")
leftClick = cont.getSensor("LClick")
endGame = cont.getActuator("EndGame")
if mouseOver.isPositive() and leftClick.isPositive():
os.execl(own.path)
GameLogic.addActiveActuator(endGame, 1)
As you can see, there are two mouse sensors hooked to the Python controller to detect a mouse click (you need to use “Mouse over” and “Left Click” together to be sure that it’s your specific object being clicked). On the other end is a Game actuator set to “Quit this game”. It ends the program once the new game is loaded.
The line “os.execl(own.path)” really drives the script, though. I tried a few commands other than os.execl (I think “os.popen()” to name one) but they opened the new file as part of the same command line sequence, so when I ended the Blender Game, the new program shut down as well. You’ll notice that I stored the exact file to be executed as a property of the object calling the script (own.path). I chose to do this since I had four buttons and wanted to reuse the same script.
Actually, my final script turned out to be a bit more complicated, since the games are in subdirectories. I had to use os.getcwd() to get the current working directory, then I had to use os.chdir() to change the directory before I could use os.execl(). Trust me, me and my Python documentation had a nice night together… Ah well, I learned a lot.
Maybe someday I’ll integrate my Custom Display Menu, since all of these games (except maybe Area 2048) offer custom display/game settings either through the command line or through .ini files. I also wanted to add some cool background effects to the main menu, but I ran out of ambition…
Just as a side note, I had to replace one of the games that I was intending to include, because the .exe refused to run correctly when called from Python. (Well, Alex the Allegator had that problem, too, but I fixed it by calling a .bat that calls the .exe… didn’t work for the other game… crazy…) The game that would have made it in is called Invalid Tangram (trust me, the game is nowhere near as boring as the website), and I replaced it with Area 2048.
Well, before I forget, I should post the files:
These files do not contain .blends. They contain Windows executable files.
A zip file (16.9MB)
-or-
A rar file (15.8MB)