r/Clojure • u/trstns • Oct 27 '21
Godot Engine clojure/jvm bindings
I've recently been writing experimental bindings for Clojure/JVM against the Godot engine GDNative API, reaching a point that demonstrates both:
- Clojure/JVM/NREPL working
- And GraalVM native-image builds
This appears to work both for games and inside of the editor, so writing plugins should be possible.
I've implemented the tutorial game "dodge the creeps" and put it here:
https://github.com/tristanstraub/thecreeps-godotclj
The GDNative bindings are implemented in: https://github.com/tristanstraub/godotclj
It is currently building against fedora 32 (64-bit), but I suppose other platforms should be possible. It is still in primordial soup stage, but is almost fun to work with.
Hope someone finds this interesting!
P.S. special mention to the dtype-next library, which helps greatly with FFI
7
u/ws-ilazki Oct 27 '21
Nice work. Out of curiosity, though, why did you go that route instead of the Arcadia Godot port? Was there something missing or wrong with that, or did you just want to see if you could get JVM Clojure working specifically?
9
u/trstns Oct 27 '21
Really good question. Some of the reasons:
Cider/Emacs isn't really compatible with ClojureCLR (having tried previously). inferior lisp mode in emacs just doesn't seem quite as powerful as Cider.
I wanted native builds via GraalVM, which is JVM only
The first point is the big one. I work heavily from inside emacs, with a REPL open, so I like that experience to be smooth, and familiar.
3
u/kwrooijen Oct 27 '21
Really cool! I've been working with ArcadiaGodot, but the experience hasn't been great. So I was hoping a JVM solution would pop up. I am wondering though why you decided to not wait for Godot 4? Since this solution won't be compatible with the next version https://godotengine.org/article/introducing-gd-extensions
3
u/trstns Oct 27 '21
At first, I didn't know Godot 4.0 was a thing, but then saw that they blogged about the newer native extensions after I had started this piece of work.
I did decide to try and generate as much C code as I could, instead of hand coding, under the assumption I might have to tackle this again, and so perhaps reuse the C generator.
The generator code is janky but tests the idea, at least.
Also, I could get this working while their newer native extensions were still effectively in alpha.
3
u/trstns Oct 27 '21
It would be interesting to know what things you found not so great with arcadia, to see if it can be improved upon.
2
u/kwrooijen Oct 30 '21
To me it just wasn't user friendly enough. Simple things like having to compile by hand, or having to connect functions using a text input. I've suggested some changes but it wasn't aligned with the author's goals of the project. Of course my ideas are my own and that doesn't mean they are the correct way of solving things. In the end I just forked the project and added most of what I was missing because it just wasn't flexible enough for me to add as an extension.
If you're interested in detailed examples of my changes / suggestions then there are a few issues / PRs on arcadiagodot which you can view (closed and open).
2
u/trstns Oct 31 '21
I am definately interested in changes that reflect what is better in your opinion. I am starting to lean towards a small kernel for FFI interop with the C world, but flexible enough to build multiple different api's on top, since I am currently generating a lot of it anyway. The kernel should really be there just to make it work, and ensure that native builds with graalvm keep working, which was one of my goals, so I didn't have to distribute or integrate with a JVM outside of my own machine.
Having a small interop kernel means that better API's just become libraries built on top.
The compiling by hand thing is present in my current implementation, but I'm not sure exactly how far I can go getting rid of that. I would have to bundle native builds per godot version within a jar, but godot still needs a native shared library to be found outside of a jar.
However, as it stands now, after building the wrapper the first time, it isn't necessary to build it again and you can simply restart the game as often as you like while working on the clojure portion. The cycle time between game restarts could do with some work. AOT of the godotclj wrapper library helps somewhat.
2
2
u/russmatney Oct 27 '21
Amazing! I'm excited to take this for a spin!
Literally last night I finished porting a tiny gd-script-based game to clojure and ArcadiaGodot: https://github.com/russmatney/lil-shooter
I'd been pleasantly surprised that emacs and cider connected and more or less worked out of the box. There are definitely gaps:
no auto-completion
no jump to def
no package/library management
have to start the game to start the repl (sometimes you just want to run some code, you know?)
imperfect stack traces and error handling
I spent alot of time digging around the arcadiaGodot clojure code to figure out how to work with Godot's 'GodotSharp' api.
Overall tho, it was still quite fun (and productive) to connect to the running game to fire functions on nodes, or figure out an implementation in a comment block. It also comes with live-reload (whenever you reload a namespace). The dev-loop itself was pretty good, and felt better than restarting the game after editing a function in a gd-script.
Now though, I feel I'll have to port it again to godotclj :D
I'm realizing how much goodness you retain by sticking with jvm clojure - cider would be fully-featured, and you'd have the usual classpath/deps.edn for managing deps/libraries out of the box.
3
u/trstns Oct 27 '21
As a warning, godotclj is definately in pre-alpha stage and the integration needs a lot of work, so you'll definately find some jankiness, and an incomplete feel to the API's, so I hope that won't be too off putting.
I'll be building some small experimental games against it to figure out its weak spots, and improve the actual experience of development.
Also, there is currently no documentation except the game implementation.
3
u/trstns Oct 28 '21
Right, so regarding auto-completion, I'm just working on generating clojure wrappers for the GDScript objects, and auto completion looks to be working. I think I can get multi arity wrappers working with defaults based on the api json generated by godot... Maybe cider-doc integration is also possible.
Jump to def works, since Cider works.
And, mentioning stack traces, as long as it isn't an FFI coding error, and memory isn't overrun somehow, the stack traces and even step debugging is working from emacs/cider. Standard java/clojure exceptions don't bring the whole thing down, but the game just keeps running till you fix it, all while the game is still running.
This last point really makes it a reasonable development experience.
What is then also nice is that the godot editor seems to be able to synchronise assets across to the running game, so as long as nothing crashes, it is somewhat possible to do some of the development live in the game.
2
u/russmatney Oct 28 '21
Aye, thanks for the warning! And the game-just-keeps-running sounds great. Godot does a great job of reloading assets, it's surprising to me that gd script doesn't get the same treatment.
I tooled around with thecreeps demo a bit - it works well! I can confirm the autocomplete, jump to def, and documentation popups work as you'd expect in any clojure + cider project.
It's cool to see the live-reload working. (i.e. adding printlns to the player-process func or adjusting the player's speed - both immediately impact the game, which is the critical feedback loop).
One issue i hit might just be my own emacs config - attempting to eval the expression here https://github.com/tristanstraub/thecreeps-godotclj/blob/a843faff9e6335d62cdbaa015b0f4f5e8193362f/src/clojure/thecreeps/main.clj#L15 locked up my emacs - most likely just due to the size of the blob itself (vs my own emacs version + config trying to handle it - this happens whenever i try to cider-inspect something too large). I appreciate the comment and callout though, as this specific thing was one of the trickier parts of working with ArcadiaGodot - there's an introspection namespace in there, but I had some trouble getting it to evaluate without errors (and haven't gotten back around to debugging it). Discovery of those apis in the repl is a real boon!
I'm excited to see the apis/libraries come together - it may be a bit raw right now, but having jump-to-def already working is huge, as it already made it trivial to bop around and start to grok what the register functions are doing. Mostly i feel like I need to take time to understand how godot native is intended to work.
One other thought - it seems great to have an nrepl available while running the editor (i've got tool scripts on the mind). The editor already spawns an nrepl (when running
./godotclj/scripts/godot.sh --editor) tho starting a game from that editor had issues (another new nrepl is spawned, and the start button disappears upon being clicked - I don't expect this is intended to be supported yet.) One of ArcadiaGodot's current pain points was having to reconnect to the repl after restarting the game - i'm curious if it's reasonable to have an nrepl persist, and have the other clients (emacs, godot as an editor, godot as a game) re-connect if the repl is already running. Much longer term of course! I'm dreaming aloud now.Anyway, thanks for sharing your work! This is honestly a huge step towards the ideal game dev hacking environment :)
4
u/trstns Oct 28 '21
Great feedback! Thanks for giving it a try. It's very encouraging to know it works on someone else's machine. I guess you are the first!
The cider lock up is almost certainly due to the large object being inspected. You can usually fix this by putting "(set! *print-length* 13)" in the repl. The output should then get truncated. It means that "pr-str" can't be used for serialization without it breaking the edn output. You can unset it with "(set! *print-length* nil)" and everything will go back to how it was.
Regarding the two nrepl sessions, I can see the problem and have also been thinking about this. I know figwheel does a great job of reconnecting to browser sessions, so I suppose it must be possible. I suppose "piggieback" makes this possible in clojurescript, but I know too little about that right now.
Starting the game from the editor should be working. The start button in the game (if this is what you mean) does disappear deliberately when clicked, in the same way that the "dodge the creeps" game tutorial describes. The button should disappear, the player should be movable with cursor keys, and mobs should start spawning and moving around the screen. I might consider adding a short screencast to demonstrate a working setup.
Thanks again for giving it a try!
4
u/frompdx Oct 27 '21
Wow, this is awesome! Great work wedding two of my favorites. I'll definitely be keeping an eye on this project.
12
u/chrisnuernberger Oct 27 '21
This is great!! Exactly the type of use case dtype-next was designed for.
For a small bit of back story, a while ago Tristan had Clojure working from Blender using a python module called javabridge. I took note of this, reviewed his pathway and based upon that work built the embedded pathway for libpython-clj - So it has come full circle in a sense :-).