LuaMachine icon indicating copy to clipboard operation
LuaMachine copied to clipboard

Any way of adding lua modules ?

Open FkinGuy opened this issue 1 year ago • 3 comments

I intend to use lua to make api calls, because it is much less cumbersome than blueprints when it comes to formating a string or extracting data from json. I tried to add a pure lua implementation of "requests" but it fails because it needs network support from the engine.

Is there any way to add not-pure-lua modules to the lua engine used here, like you would do with luarocks ?

Blueprints are very good for certain things, C++ for others, but I think there is still room for a traditional scripting engine too.

FkinGuy avatar Nov 14 '24 07:11 FkinGuy

Also, I noticed the discord link, in this repo and others, is expired :)

FkinGuy avatar Nov 14 '24 07:11 FkinGuy

You definitely want to embed c lua modules in the core itself (allowing the load of .so/.dll will basically break every security measure). This is probably one of the best articles on the topic: https://leiradel.github.io/2020/03/01/Embedding-Lua-Modules.html

In your case embedding luasockets should be enough.

Regarding the discord link it is still valid (there are people still joining even today), which error are you getting ?

rdeioris avatar Nov 14 '24 09:11 rdeioris

Did you get this working? I'm still in early stages but trying to grasp how the pipeline will be if I use lua for a lot of code (as it's a much more comfortable environment for me than C++ or blueprints).

Would we hack the additional embeds into LuaState.cpp? Looks like that's where the preload table is added.

This SO post talks about including socket+mime (required) https://stackoverflow.com/questions/2197144/how-could-i-embedded-socket-in-lua-internally-just-like-oslib-debuglib

I was hoping to get mysql database access (for the server) via lua as well, but it sounds more complicated - maybe the luasql library isn't made for full c++ importing & needs additional prototype classes? https://stackoverflow.com/questions/12236032/how-to-embed-luasql-sqlite3-in-a-statically-linked-c-program

inspire22 avatar Feb 18 '25 07:02 inspire22

I got back to this yesterday. I was very confused by Roberto's answer, as I didn't see where I actually added source code or a library from luasocket in the link provided.

Instead, I tried the following approach :

  • create a static lib project in Visual Studio
  • import the .h and .c files from lua source tarball
  • manage to compile it sucessfully
  • replaced the ThirdParty/x64/liblua53_win64.lib file by the .lib file created by my visual studio project

After that, I could use the lua-machine in an Unreal project => my .lib was ok.

Next thing, I tried to integrate the luasocket source into my visual studio static lib project. I managed to get it to compile after a few tries.

I had to add this to linit.c (in the luaL_Reg loadedlibs[] section) :

{LUA_SOCKETNAME, luaopen_socket_core},
{LUA_MIMENAME, luaopen_mime_core},

And then I added this in lualib.h (after the other similar declarations) :

#define LUA_SOCKETNAME	"socket.core"
LUAMOD_API int (luaopen_socket_core) (lua_State *L);

#define LUA_MIMENAME	"mime.core"
LUAMOD_API int (luaopen_mime_core) (lua_State *L);

Finally I added the following into LuaState.cpp from LuaMachine plugin :

extern "C" int luaopen_socket_core(lua_State *L);
extern "C" int luaopen_mime_core(lua_State *L);

lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");

lua_pushcfunction(L, luaopen_socket_core);
lua_setfield(L, -2, "socket.core");

lua_pushcfunction(L, luaopen_mime_core);
lua_setfield(L, -2, "mime.core");

lua_pop(L, 2);

luaL_dostring(L, "local socket = require('socket')");

Also, I had to tweak VS static lib project properties to make sure the .c files where not treated like .cpp files.

Right now, I am somewhat happy in that the last .lib I created does work like the original from LuaMachine plugin, but require('socket') or socket._VERSION still end up with a nil....

If anyone is interested, I can create a github repo for the vs static lib lua project, and a fork of LuaMachine to show the exact modifications.

FkinGuy avatar Jun 10 '25 00:06 FkinGuy

Oops, I didn't want to close the issue. I typed the wrong button.

FkinGuy avatar Jun 10 '25 00:06 FkinGuy