Invoking C# code from within Lua? #2
-
Is it possible via Laylua to define a method in Lua or behavior such that, when called, invokes a C# method? As mentioned in my previous discussion, with the idea being an intermediate scripting language for a Discord bot, I would like the script to be able to, for example, reply or send a message in the context of a custom command. -- defined in custom ILuaLibrary
function send(msg)
-- this would...call the C# code below?
end
-- elsewhere, in a custom command...
a = 5
if (a ~= 42) then
send("Hello!")
end And what it would call in C#: // send(msg) should, as a simple example, send text to a user via this method
// TODO: Lua doesn't really do async/await, so may need to be a void with .GetAwaiter().GetResult() and hope for the best
public async Task SendAsync(string msg)
{
await discordClient.SendMessageAsync(/*something with msg*/);
} The goal here is to not have to re-invent the wheel by re-defining the entirety of Discord's REST API in Lua here. I could, if it came to it, utilize one of the various Discord Lua libraries, if compatible with 5.4.X, and import it here, but I'd be concerned about security issues with users trying to call functions they shouldn't, etc. An alternative I'm considering, if this isn't possible, is out of scope for the library, or would be too much work, is expecting users to return a "message" object of sorts, which I would then have to...interpret, somehow. Is this a better alternative, or also possible: Returning a LuaTable from |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, any delegate can be marshaled as a Lua function. Here's an example that sets a global function which, when called, invokes lua.SetGlobal("print", (Action<string>) Console.WriteLine);
lua.Execute("print('Hello, World!')");
I suggest creating wrapper functions that handle the necessary tasks. This approach introduces an intermediary layer between the .NET code and Lua, enabling you to implement features such as rate-limits for the calls and offloading the work to a dedicated worker thread.
Regarding the alternative approach, it's up to you. Returning a |
Beta Was this translation helpful? Give feedback.
Yes, any delegate can be marshaled as a Lua function.
Here's an example that sets a global function which, when called, invokes
Console.WriteLine(string)
:I suggest creating wrapper functions that handle the necessary tasks. This approach introduces an inte…