Building Stencyl Haxe files inside Sublime Text
Building Haxe files inside Sublime Text is pretty much a solved problem, thanks to the Haxe-Sublime-Bundle.
However, using Sublime to externally edit Stencyl Haxe files is a bit trickier, since Stencyl has a ton of dependencies.
Of course, this makes developing much easier and faster, but the build is a bit more complicated than the standard build bundle can handle.
The standard edit/build cycle is:
- In Stencyl, click to edit the file externally
- Edit in Sublime Text (the built in Stencyl editor is pretty crap)
- Save
- Swap back to Stencyl
- Save again (because Stencyl is a bit daft)
- Click the Check Syntax button (there’s no keyboard shortcut)
This takes maybe ten seconds, which includes quite a bit of messing around, plus using the mouse (always slower).
Here’s a much faster way (Mac only. Sorry if you’re on Windows, you’ll need to convert the bash script to DOS. If you’re on Linux, you may need to tweak the gamename= line):
Create a bash script for the actual build:
#!/bin/bash
# parameters – filename to build (including path)
gamespath=/c/g/dev/stencylworks/games-generated
if [[ $1 == */* ]]; then
sourcefile=$1
else
sourcefile=$(pwd)/$1
fi# find current game
cd $gamespath
gamename=$(find ./ -iname *.hx -type f | sed -e ‘s/ /\\ /g’ | xargs ls -tr | tail -1 | cut -d “/” -f3)cp $sourcefile “./$gamename/Source/scripts/”
cd $gamespath/$gamename
/Applications/Stencyl/plaf/haxe/haxe -cp Source/ -cp Export/flash/haxe/ Export/flash/haxe/debug.hxml
You’ll need to change the gamespath (at the top) to wherever you’ve decided to save your Stencyl games. Specifically, the games-generated directory.
The matching against $1 just means if you pass in a full path (as Sublime Text does) then it uses that. If you’re in a directory and just pass the filename (as you would if you ran the script from the commandline) it’ll auto-add the current directory.
The only vaguely tricky bit is the line that finds the game name for the most recently edited file (ie, the one you’re working on). You can pretty much ignore that line of gibberish, but hey, if you REALLY want to know…
First it finds all the .hx files in the games-generated subdirectories. Sed sorts out any spaces in the paths (so the remaining commands don’t puke), passes the whole lot through to ls to sort by time (so the newest one is last). Tail gets the last one. And cut gives us the root directory – ie, the game name.
Complicated, but it works.
The last line, starting “/Applications/Stencyl” is all one line (in case it line-wraps on your screen)
(don’t forget to chmod +x the file so it’ll execute)
So, that’s bash.
On the Sublime Text side, you need to do the following:
1. Tools | Build System | New Build System
2. Enter the following:
{
“cmd”:[“/usr/local/bin/build_stencyl_haxe”, “$file”],
“selector”:”source.hx”,
“env”:{“HAXEPATH”:”/Applications/Stencyl/plaf/haxe/”, “HAXE_STD_PATH”:”/Applications/Stencyl/plaf/haxe/std”}
}
3. Save the file as haxe.sublime-build in the directory it defaults to
Now, some important notes here.
- You’ll need to replace the /usr/local/bin/build_stencyl_haxe with wherever you’ve saved the bash script above
- The selector line just means that the auto build chooser will identify any *.hx files as haxe files and use this build command set. That’s the theory. However, there’s an hxml build built in, which doesn’t take into account the Stencyl specifics. So, auto-build won’t work anyway. This isn’t a big deal, you’ll just need to select Tools | Build System | Haxe to manually select the build type. Hardly the end of the world. If anyone knows how to stop the default hxml type from taking precedence, I’d be curious to know.
- The env line I’ve had to put in because I have a couple of different versions of Haxe on my system. This ensures that the build process runs the Stencyl version of the libraries, so nothing gets confused. You may not need that. If you delete that line, be sure to remember to delete the trailing comma from the end of the selector line, otherwise Sublime Text will complain
When you’re done, just save it in the current directory (it’ll be something like ~/Library/Application Support/Sublime Text 2/Packages/User) with the name Haxe.sublime-build. Whatever you put before the ‘.’ is whatever will appear in your Build Tools menu.
So, that looks like a lot, and it did take me a good solid day to nut it all out and get it working well. There’s really not much though – c&p the bash file somewhere, change the path, save & chmod it, then c&p the build file into a new build tool option, point it at your bash script, save it and you’re pretty much done.
Here’s the good news:
- You don’t even have to save the file in Sublime Text in order to run the build – it’ll auto save
- If you’ve got the Built type selected in Sublime Text, a simple Command-B (on Mac) will build it
- It typically takes me 0.3s to build the file (if it finds an error, a bit over a second for a full game build) – whereas all that fiddling around back and forth to Stencyl took around 10.
For something you’re likely be doing hundreds of times a day as part of your core dev cycle, that’s a huge gain.