How To Make Your Own Haxe Library
Hold up!If you like 90s platformers, check out my new game Kid Bubblegum!
When I made Magnet Man Adventures, I created a small pile of code that I kept adding to over the course of the next couple of games. Stuff like menus, controller input, sound effects etc. have to be in every game, so reusing code you’ve already written makes a lot of sense. However, constantly copy-pasting bits of code from one project to the next is a) bothersome and b) very hard to maintain. So I decided to create my own library with all the code that I had amassed over the years. And here’s a quick explanation on how to create your own Haxe library!
It’s quite simple, really. First, create a folder with your intended name for the library. Then, create a
haxelib.json file in the root of the folder. This file will contain some metadata on the library, which will be required if you ever decide to publish your library to haxelib. You can find documentation on the file here.
Then you can start populating your folder with the source files you want in there. There are two things to keep in mind here.
Put the source files in the folder you defined in the
classPathattribute in the json file above. So, if you specified
src/as in the example, create a
srcfolder in the root of your folder and put the source files in there. You can of course create subfolders, but
classPathneeds to point to the root folder of your source files!
Make sure to specify the package definitions at the top of the source files. These should reflect the folder structure you are creating. For example, if a file is in the
srcfolder, the package should be
src. If a file is in
src/foo/bar, the package should read
src.foo.bar. Use the folder structure and package definitions to bring a bit of order into your library!
Once all the files are in, your library is ready to be used. The final step is to tell Haxe about it! You do this via the
haxelib dev command. Its syntax is as follows:
haxelib dev [project-name] [directory]
So, for example, if you had the library
MyLib and you saved it under
C:\libs\mylib, you would enter:
haxelib dev MyLib "C:\libs\mylib"
Now that Haxe knows about your library, you can include it in your project like you would any other external library from Haxelib!
Also: If you ever decide to make your lib publicly available on Haxelib, follow the steps under Submission process here!
If you have any questions, comments or criticism, post them in the comments below or reach out to me on Twitter @ohsat_games!
Join my Discord Server!
Hang out, get news, be excellent!
Want To Buy Me a Coffee?
Coffee rules, and it keeps me going! I'll take beer too, though.
Check out the rest of this tutorial series!