Where can I find tutorials on embedding Lua scripting into applications (kbin.social)
from ZILtoid1991@kbin.social to programming@programming.dev on 12 Nov 2023 21:14
https://kbin.social/m/programming@programming.dev/t/621134

The official documentation isn't 100% clear on things (why am I getting LUA_TNIL for functions?), and the best I can find with some simple web search is kinda relevant stackoverflow (🤮) posts, except they're mostly about calling host functions from Lua side, the rest are things that seem I've nailed so far.

EDIT: Solution was that everyone was using luaL_dofile, while I was forward thinking and used lua_load instead, which isn't a macro, and as such doesn't do an initial lua_pcall. Now I do it manually, and now I get different, but less cryptic and actually documented errors. Now I just have to wrestle with D metaprogramming features (very strong and capable, but is a rabbit hole itself).

#programming

threaded - newest

Limeey@lemmy.world on 12 Nov 2023 21:23 next collapse

What do you mean “embedding lua into applications”?

I assume you mean you want an application extensible by user lua script?

You build an API that calls the lua interpreter and passes the script, and reads the output; same as you would for any other scripting language. You define what the inputs should be, create the interface for executing the user defined script through shell commands, and then retrieve the output.

For python you’re going to probably use this:

docs.python.org/3/library/subprocess.html#subproc…

For C# you’re going to use Process

stackoverflow.com/…/process-start-how-to-get-the-…

The complexities arise in your implementation and there’s no single guide.

samus7070@programming.dev on 13 Nov 2023 12:30 collapse

While lua ships a standalone interpreter, it is very much designed to be embedded directly into an application. This is done by invoking some C apis to load the interpreter into the application’s memory space. OP wants to do that rather than invoking another process and reading the output. When embedding into a host, the host can provide its own objects to be manipulated by the user script allowing for a much better extensibility experience.

ZILtoid1991@kbin.social on 13 Nov 2023 20:13 collapse

That solution is janky and slow as hell. I'd rather just embed it into my own software, which is mostly done, except it doesn't find functions as functions, but as nil value.

tdawg@lemmy.world on 12 Nov 2023 21:54 next collapse

Not really sure what you’re trying to achieve here. Do you have an application that lets you use Lua extensions? Or do you want to build an application that lets you use Lua extensions?

bizdelnick@lemmy.ml on 12 Nov 2023 22:04 next collapse

Well, reference manual was enough for me. There’ nothing difficult. You probably have troubles with your lua code, try executing it in the standalone interpreter.

DmMacniel@feddit.de on 12 Nov 2023 22:38 next collapse

Whats the host language you try to embed lua into?

ZILtoid1991@kbin.social on 13 Nov 2023 07:30 collapse

D.

It has C ABI compatibility, so it should work. But as others wrote, I might have messed up my Lua script.

aluminium@lemmy.world on 13 Nov 2023 07:57 collapse

Upvote for D, underrated lang.

Swim@lemmy.ca on 13 Nov 2023 00:34 next collapse

let me take a guess, gta online modder ?

ZILtoid1991@kbin.social on 13 Nov 2023 07:28 collapse

No, I'm developing my own engine.

adamnejm@programming.dev on 13 Nov 2023 09:08 collapse

One Lone Coder has made few videos about adding Lua scripting to a C++ program, maybe it has what you’re looking for: Embedding Lua in C++ #1