Hi everyone
I will begin by thanking @aytos for his awesome writeup on how to setup and use Fennel in Defold. I will extend with my own adventures with implementing the ahead of time workflow with Fennel in Defold.
A look at Fennel (again?)
As mentioned in the previous post, Fennel is programming language with Lisp-like syntax running on top of Lua. Since it keeps Lua semantics, code transcompiled from Fennel to Lua is very clean.
How do you use it
Fennel can be embedded in applications in two ways -
- You use the fennel library, which can compile a string at runtime and run it. This approach was explained in beautiful detail in the original post.
- You can compile Fennel to Lua ahead of time and use the Lua code it generates as is. I explored how to implement this approach in Defold.
Why AOT ? (and why not ?)
As explained in the previous post, using runtime compilation poses some issues, like having go.property in the original script, which is isn’t that helpful when you have a lot of properties that you’re working with. Also, Hot reloading is not supported with custom resources currently.
AOT aims to solves this problem by generating the lua code ahead of time, and using it like any normal lua code. This solves the first issue by providing a way to call go.property directly in script. The second one is solved too as scripts and modules are hot reloadable.
But this approach also has its caveats, with the most major being that it adds a layer of friction over your codebase. You must remember to compile the code to see the changes.
The approach
As Fennel’s compiler writes the compiled code to stdout, I started by modifying the Fennel source ( ) to allow a way to export the compiled scripts as lua files with the specified extension. It wasn’t the best way I know, and could be surely done in other files that sit around the compiler, but it was surely the easiest way to start. I then wrote a layer of Makefile (Yep those makefiles
) around the generated executable to make it possible to batch compile a directory of files at with the specified extension ( to account for Defold’s scripts ). It was then a breeze to compile all the required scripts into the required format and use them as is without any issues.
The Road Ahead
I’m aware that Makefiles and modifying sources is not the best way to go about, so I’m currently looking in editor scripts to find a way to use the official Fennel binaries to produce the same result. Stay Tuned.
Show us something
To test the capabilities of Fennel, here is a port of Sidescroller tutorial written in Fennel (I’m going around posting this everywhere )
Answering the big questions
With everything detailed, it is perhaps time to answer the big questions
Can you use Fennel in Defold?
Absolutely. Fennel is a great language, and both of the methods detailed here make it dead simple to do so. (well not both )
Should you use Fennel in Defold?
Before you consider using Fennel for your next project, I will like to address several issues that Fennel + Defold had in general
- Fennel has a habit of forcing returns, which the standard
on_input
method does not like. As long as there is a bit of code inon_input
that returns something other than true or false, you’re bound to get an error. Even though it hamper the code execution in any way, it is still painful to see errors - Forced returns really become a blocker when you start coding gui scripts. Gui Scripts
init
function throws an error when areturn
statement is encountered in its init method. As a follow up, the guiscript is not initialized and hence won’t work at all.
While the first one is ignorable, the second error makes it very difficult to code gui scripts in Fennel.
You still Here?
Thanks for reading through the post. It was a great project to explore the feasibility of a new language in Defold, and was definitely worth it. Feel free to shoot any questions that you might have and I’ll do my best to answer it.