How to use gRPC in LOVE?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
dahfuku
Prole
Posts: 3
Joined: Sun Dec 08, 2024 2:17 pm

How to use gRPC in LOVE?

Post by dahfuku »

It seems that you can execute C++ functions from lua.
Can we use gRPC using it?

If possible, I would like to achieve this without recompiling LOVE2D.

If you have any simple samples or links to helpful sites, please let me know.
I've looked up terms like LuaJIT FFI and Lua stack on websites, but I'm finding it difficult. :cry:

(I'm not familiar with English, so I'm using automatic translation.)
dahfuku
Prole
Posts: 3
Joined: Sun Dec 08, 2024 2:17 pm

Re: How to use gRPC in LOVE?

Post by dahfuku »

Why does the error "Error loading DLL" occur when I run the following program?
main.lua, sample.dll, and lua54.dll are in the same directory.
Maybe there's an error in the dll file? :cry:

----------
//sample.cpp
#include <iostream>
#include <lua.hpp>

void printTable(lua_State* L, int index) {
lua_pushnil(L);
while (lua_next(L, index) != 0) {
const char* key = lua_tostring(L, 1);
const char* value = lua_tostring(L, 2);
std::cout << "[cpp input] " << key << " : " << value << std::endl;
lua_pop(L, 1);
}
}

int cppFunction(lua_State* L) {

if (lua_gettop(L) != 1 || !lua_istable(L, 1)) {
return luaL_error(L, "Expected a table.");
}

printTable(L, 1);

lua_pushstring(L, "new key1");
lua_pushstring(L, "new value1");
lua_settable(L, 1);

lua_pushstring(L, "new key2");
lua_pushstring(L, "new value2");
lua_settable(L, 1);

printTable(L, 1);

lua_pushvalue(L, 1);
return 1;
}

extern "C" int luaopen_mystack(lua_State* L) {
lua_register(L, "cppFunction", cppFunction);
return 0;
}

----------
--main.lua
io.stdout:setvbuf("no")

function love.load()
sample = package.loadlib("sample.dll", "luaopen_mystack")
if sample then
sample() -- DLLの初期化関数を呼び出す
else
print("Error loading DLL")
end
end

function love.draw()
local myTable = {key1 = "value1", key2 = "value2"}

if cppFunction then
cppFunction(myTable)
for key, value in pairs(myTable) do
print(key, value)
end
else
print("cppFunction is not loaded")
end

love.graphics.print("Hello, world!", 400, 300)
end

----------
#CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyLuaProject)

# インクルードディレクトリを設定
include_directories(${CMAKE_SOURCE_DIR}/include)

# ソースファイルを指定
set(SOURCE_FILES src/main.cpp)
#set(SOURCE_FILES src/main.cpp src/sample.cpp)

# 共有ライブラリ(DLL)を作成
add_library(sample SHARED ${SOURCE_FILES})

# 静的ライブラリを指定
target_link_libraries(sample ${CMAKE_SOURCE_DIR}/lib/liblua54.a)

# ライブラリ名を設定
set_target_properties(sample PROPERTIES OUTPUT_NAME "sample")

# コマンドを実行する!
# cd ./build
# cmake -G "MinGW Makefiles" ..
# mingw32-make
----------
--console
Error loading DLL
cppFunction is not loaded
cppFunction is not loaded
cppFunction is not loaded
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to use gRPC in LOVE?

Post by RNavega »

Hi, welcome.
I think the entry function needs to return 1? At least that's what I'm seeing in other people's examples of C modules:
And also, on Windows (like with your MinGW) you need to have the entry function be marked visible in a special way:

Code: Select all

int __declspec(dllexport) luaopen_mystack( ...
See the header to the first example: https://github.com/Seng3694/CLuaModuleE ... h-our-goal

If those changes don't fix it, I suggest in any case building those example C files so you at least have something that you know to be working. Then you can modify the C file with your own code in steps, and see when the error appears.
For convention, I'd also suggest renaming the DLL to the same name used in the luaopen_* function, which would be "mystack.DLL" in this case. This is used when you load it with require("mystack"), which is not your case of course, but maybe someone else will try to use your DLL in that way.
If you find the error, do share what it was!

Edit: ah, nevermind, even for the luaopen_* entry function, it also returns the number of return values added to the stack. So that "1" was from the table that those example C modules created. In your case you're putting a global function, no return values needed. But then 'sample' will be nil, since you're not adding any return values to the stack.
dahfuku
Prole
Posts: 3
Joined: Sun Dec 08, 2024 2:17 pm

Re: How to use gRPC in LOVE?

Post by dahfuku »

Thank you for your advice!
I'll try and figure out what to do with your advice.
I'm not sure if I can give you a good answer, but I'll try my best.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to use gRPC in LOVE?

Post by RNavega »

Of course, good luck.

To add to that, the second example shows the use of luaL_register():
https://github.com/mostvotedplaya/Lua-C ... .c#L49-L56

From the docs, that's the ideal function to use to help set up your C module as a library to be used back in Lua, it saves you time by doing a lot of stuff:
https://www.lua.org/manual/5.1/manual.h ... L_register
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to use gRPC in LOVE?

Post by zorg »

Also, you mentioned lua54.dll which sounds out of place, considering löve uses luaJIT by default, which iirc has a different name for its library file.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests