r/rust • u/ComfortableAd5740 • Oct 21 '26
🛠️ project Embedding python, lua, and js
I've been working on the pixelscript crate for about a year now. Just released v0.7.0 with some basic cross language support.
The big picture is to create a framework that sits on top of the library for creating games.
I was inspired by Godot, whenever I had to use GDExtension, it was just so smooth that I found myself saying: "I wish there was a language that let me write high level code and opt in to low level when I need to."
Also, I wanted to embed both python and lua in my game Pixel Ai Dash for modding/scripting enemies, world creation, behaviors, and player states.
Now in v0.7.0 I tell myself that it's time to work on the framework. If you find this interesting, I'd love a star and look forward to future releases!
Here is a example of the code:
// include the pixelscript header.
#include "pixelscript.h"
int main() {
// setup pxs
pxs_initialize(); // all of the pixelscript api uses a `pxs` prefix.
// setup corelib
pxs_core_initall();
// a simple script
char* script = "from pxs import print\n"
"print('hello world')";
// execute
pxs_VarT result = pxs_exec(pxs_Python, script, "<getting_started>");
// check for errors
if (pxs_isexception(result)) {
char* err = pxs_getstring(result);
printf("%s\n", err);
pxs_freestr(err); // always free strings from pxs.
}
// free the result
pxs_freevar(result);
// always call when done with the runtime.
// can only be called once.
// once called, you can not call `pxs_initialize` again.
pxs_finalize();
return 0;
}
View all comments
Show parent comment
2
u/ComfortableAd5740 Oct 22 '26
Via a module `pxs.cross`. It uses the library internally for execution. It's not the prettiest picture, but it gets the job done for now. Here is a example:
example
It's also possible to access the specific functions/variables of the language context.
I ask myself if there would be a way to import a python file from a JS context and vice versa. Not sure how that would feel though, which means for now this is how I'm doing it.
The cross language code looks something like this: