(Non-issue) How do you achieve lua in minecraft?
PublicVoidUpdate opened this issue · 2 comments
i am working on some games of my own, and i want to have a similar terminal setup with custom programs and mapped output. im working in c in dev and want to use lua in interface. How did you map print to the screen in game, and have game components?
This project makes use of the Lua library*, the API allows C to create globals, tables, (C) functions, etc. (and of course load and run lua).
There are plenty of resources online about this, the terms you're looking for are 'embed lua in c'.
As for answering the question, they pretty much just override the print
function to something that puts text on the screen**.
You'll also find it useful to know that some functions (specifically computer.pullSignal
) actually yield to give control back to Java, and resume the Lua thread when an event (or timeout) is ready. This uses Lua coroutines.
All the lua code this project uses is somewhere in src/main/resources/assets/opencomputers/
.
If you're going to run untrusted lua code in your game, Lua by default can access files, so you might want to consider sandboxing. Limiting CPU time and memory is possible, see machine.lua on this repo (debug hooks are used for this).
Lua docs: https://www.lua.org/manual/5.4/ - includes C API docs
(*) Actually they use two other libraries, eris with luaj as fallback. Eris is used because it can save and restore entire Lua states, LuaJ is used as it is written in Java.
(**) print
specifically is more complicated, as its actually an abstraction provided by OpenOS of the gpu
and screen
components