开发者

Get Lua state from inside Lua so it can be passed back to C

I am able to load a DLL created from C source from within Lua开发者_如何学编程. So what I want to be able to do is pass the current Lua state FROM Lua to the loaded DLL.

Basically I'm using a game engine that uses Lua. The scene editor of said game engine creates the Lua state and calls Lua scripts and I know for a fact that it uses 1 lua state for all the scripts that it calls. So, I would imagine that state is known from within these lua scripts themselves. From within these Lua scripts I want to load my own DLL and pass in this state to that DLL, so my C++ code can use that lua state to call lua functions from the same lua scripts and be in the same state. Does that make sense?


I'm missing something obvious, I guess (which wouldn't surprise me - I'm far from a Lua expert).

But if you call package.loadlib, the function handle you get back IS going to be called with the state by Lua itself, isn't it? See the CFunction prototype


Write your DLL as a normal Lua module implemented in C. PiL has a description, but it adds up to naming a single exported function after the DLL's name so that the normal require function can load it. To get a library loaded by require "mylib", you create mylib.dll with the exported function luaopen_mylib() that creates a table containing all of the methods you want to be able to use and returns it. That function, as well as all the other methods it creates, is passed the current Lua state on each call.

If your engine doesn't allow require in scripts, then it isn't likely to have allowed package.loadlib either, and you are probably not going to be able to load your DLL at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜