Jump to content
martyj

C++ libraries in LUA

Recommended Posts

I think it would be very beneficial for the LE community to be able to create and distribute C++ libraries that create additional LUA functionality.

 

For example, the LE exe could search through a /libs/ folder, load each .dll/.so dynamically calling a "leadwerks_init" function defined in the dynamic library.

 

The dynamic library could use to_lua++ to create additional functionality, such as playing MP3, OGG, audio file, networking, or anything else that people create.

 

To load a library in Windows:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx

To call a function in the library in windows use

https://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx

 

To load a library in Linux use

http://man7.org/linux/man-pages/man3/dlopen.3.html

dlopen

To call a function in the library in Linux use

dlsym

 

I currently use the C++ version of LE, and could distribute a game exe that would include this functionality with a library that I create, if I did. But it would be better for the LE community if the LUA version had this built in.

 

What if Josh never had to program networking into LE because someone in the community did it first?

What if Josh never had to program in a compressed audio format into LE because someone in the community did it first?

 

Imagine the possibilities.

Share this post


Link to post

This would be a dream come true! lol

 

It would also take a load off of Josh for smaller features like additional audio support and other cool but more specific technologies.

Share this post


Link to post

You can create your own C++ DLL's that expose functionality to Lua already. You load them in via Lua.

 

What if Josh never had to program networking into LE because someone in the community did it first?

 

He doesn't need to. There is already a Lua version of RakNet out there that you can load into Lua or C++.

 

The short answer is link to the Lua lib and bring in Lua headers that exist in your Leadwerks Steam installation folder into a C++ DLL project (for windows). Then expose whatever functionality you want. I had a post around here that did just that. Can't find it at the moment but all of this is very doable already via Lua or C++ library that you give to people.

Share this post


Link to post

You can't just included a LUA library into the LUA version of leadwerks. You need the C++ version. Unless I am mistaken to LUA's potential with LE.

 

What I'm talking about is having Josh provide a way to import dynamic libraries into the LUA version of the game.

Share this post


Link to post

Create an empty DLL project. If you have the C++ version of LE you can use it's lua .lib and header files that are in the steam install dir for LE but it's not required if you can make the .lib yourself from the Lua source (which anyone can or the C++ LE owners can just give the lua .lib and header files to anyone since they aren't LE specific in any way). Either way add the lua lib and header files to your project. Make your C++ code that exposes lua functionality like I did in the link below. Place the DLL in the base directory as the exe (you can give this DLL to any other Leadwerks user as well, even Lua only users), call package.loadlib() from Lua and you now have the DLL's functionality from within Lua.

 

http://www.leadwerks.com/werkspace/topic/11875-more-precise-timer/page__st__20#entry87654

 

 

What I'm talking about is having Josh provide a way to import dynamic libraries into the LUA version of the game.

 

I get that, but Josh doesn't need to provide this. Lua itself has this functionality built in.

Share this post


Link to post

You can't just included a LUA library into the LUA version of leadwerks. You need the C++ version. Unless I am mistaken to LUA's potential with LE.

 

What I'm talking about is having Josh provide a way to import dynamic libraries into the LUA version of the game.

 

Wrong, you can import an external dll. Just make sure the sand-boxing is off in LE options. I have made a DLL in C and use it in LUA just fine.

 

[Edit] I haven't linked against the LUA lib file that Rick is talking about. I just linked it to another dll file, I now have two LUA dlls in the root. Its messy, but it works for now. I will try to link it against the proper library once I have the time.

Share this post


Link to post

I downloaded the lua5.1. Windows sources (includes VS 2008 project) compiled the dll + lib. Included the lib in my C project. Wrote the code to interface with LUA and compiled it into a dll. Then put the lua5.1.dll and my own library dll in the same folder with the LE executable. Import the library as usual within App.lua. Its hacky but it works.

 

Now that you say there is a LUA lib that ships with LE (and I just checked its there) I can hopefully do away with having two LUA dlls in my game's root.

 

[Edit] Windows sources and compiled LUA bins can be found here: http://sourceforge.net/projects/luabinaries/files/

Share this post


Link to post

Ah so your dll name was lua5.1.dll vs LE's lua51.dll. I see. I was wondering how you had both in there. This raises the question that I guess I never noticed. If you link in a lib file does the dll it use need to be the same name as the lib? I assume that's how your DLL knows to use what lua DLL. Your lua lib file you used was lua5.1.lib as well?

 

So even if people don't have LE C++ they should be able to get those lua binaries and use those but might need to make sure the lib file they use has the same name as the LE lua dll (lua51.dll).

Share this post


Link to post

That's correct, same name for the lib and the dll. I think somewhere in the LUA sources you might find a constant but it always looks for the same name by default. However I was getting all sorts of crashes with other versions, so its important to have one that matches the one used in LE.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...