Hi again,
Thought I’d write a short post on how to get up and started with Godotcaml. Normally this would go in a README, but I suspect it will get out of date quite quickly, so I thought I’d put it in a blog post instead. It’s also extremely rough at the moment; like I said before, I wanted to get stuff in the hands of people interested as soon as Godot and OCaml could call each other back and forth.
First, clone the repo.
$ git clone https://github.com/Fizzixnerd/godotcaml.git
You will notice a couple of things: firstly, this is already setup as a sample Godot project. You can open it with Godot 4.2 and play around with it. Visit to the file ./ocaml/godotcaml/apis/uses_ppx_godot.ml
for a file to play around in and experiment. When you’ve made changes to this file, cd ./ocaml/godotcaml/apis
and dune build
(the cd might be necessary as I’m not sure bin
still builds… will fix later!) I’m pretty sure I got the dependencies right in opam, but if not you can find them in the dune
file and install them yourself.
Second, there is a file hello.gdextension
. In this file, there will be three paths to the _build
directory of godotcaml proper. Modify these paths so they point to the correct location for you, for example, if you’re on Windows, you’ll have to enter the (relative) path to the .dll
file that gets built. If you’re on linux, it’s already setup (hopefully).
Launch a version of Godot 4.2, then import the project, and launch the “game” by pressing F5
, or the equivalent on your operating system. There is a little play button in the UI if you prefer that. Explore the GDScript script on the main scene by clicking the little script icon next to it, and n
The uses_ppx_godot.ml
file defines three Godot methods. Currently _process
doesn’t work properly I don’t think, since I was hoping it would be called on every frame without having to explicitly call it in its parent’s _process
method. Basically, the syntax is:
let%<gfunc or gfunc_void depending on if you return a value or not> <method name> =
[<ClassMethodFlags (found in godotcaml.ml); if in doubt, just use `default`>]
(module <BuiltinClass0.Name or Class.Name (no zero then!) for the first argument>)
(module <BuiltinClass0.Name or Class.Name (no zero then!) for the second argument>)
...
(module <Class.ClassYouAreInheritingFromName for the self parameter, example Class.Node by default>)
(module <ApiTypes.Void if returning unit, or same as an argument for returning anything else>)
(fun x0 x1 ... self -> <body>)
You will get funky unhelpful type errors if you mess anything up. You can see examples in the file when you clone it. After defining these methods, you may call them from GDScript.
Everything is WIP, so expect things to break. One thing I noticed most recently is a GC error if you run _process
too long. I Will take a look at that soon.
I will post an introduction to the code next week hopefully, for those intrepid people looking to explore. Hope you have fun!
Best
Matt
About Matt Walker
Matt Walker is a software engineer with a love for all things Functional, DevOps, and Typed, currently residing in Toronto, Canada.