Creating a lua lib to use for C++?

Started by
11 comments, last by dmail 13 years, 6 months ago
I want to follow this article:
http://www.gamedev.net/reference/articles/article1932.asp
but I want to use the newest version of lua. In the article he compiles lua 4.0.1 into a library file that he can include into his projects. It doesn't say how he did it though. I was wondering if I should even bother trying to do the same thing with lua 5.1.4 so I can follow the article with the newer version. When I tried to use lua 5.1.4 with the examples given, the code had no syntax errors but it gave a linker error which I'm assuming is because there were no .lib files. I had included the include directory that came with lua into the VC directories settings but it had no lib folder so is it necessary I create this lib file like the author in the article did? If so, how do I do that?


Hopefully you understood that. Any help would be appreciated.

Language: C++
IDE: VS 2010
Lua version: 5.1.4

EDIT: After looking around the article is too outdated to use with the current lua release. If anyone could just point me to a getting started type of tutorial with the new version I would appreciate it. I'm just trying to get it set up to work (like with any api where u would add the includes and libs) Thanks.

[Edited by - SonicD007 on September 29, 2010 4:38:43 PM]
Advertisement
Quote:In the article he compiles lua 4.0.1 into a library file that he can include into his projects. It doesn't say how he did it though.
He followed the installation directions. Where are these directions?

Did you download the Lua sourcecode? Did you look inside the folder? Did you see files called "README" and "INSTALL"? Did you open up those files, and uh, read them?

Quote:is it necessary I create this lib file like the author in the article did?
Of course. How can you use Lua without making use of the code?
I was reading the documentation and I think I found what I was looking for. Gonna try to see if the tutorial will work now. Thanks.
If you try and follow that article you are going to hit many problems as that was written for a version which was released eight years ago, since then there have been a number of breaking changes to the API.
For changes from 4.0 to 5.0 see changes (scroll down a little)
For changes from 5.0 to 5.1 see changes
And for changes from the current 5.1 to 5.2 work 4 see changes.

If you want to learn about Lua you can read the first edition which is relevant to 5.0 online for free, the current reference manual, the Lua user wiki and/or buy the second edition to programming in Lua which is valid for 5.1.

Download and install LuaForWindows_v5.1.4-40 (there might be new release, so downloadn the new one). Install it into any directory you want (I choose the default C:/program files/Lua).
On your visual studio, and new include path via:
Quote:
Project -> MyProjectName properties -> Configuration properties -> C/C++ -> Additional include directories

It should have an entry to Lua/include/ folder
Add the path to Lua/lib/ folder via
Quote:
Project -> MyProjectName properties -> Configuration properties -> Linker -> Additional library directories

Then:
Quote:
Project -> MyProjectName properties -> Configuration properties -> Linker -> input -> Additional dependencies

Add lua5.1.lib
And compile your project!

I would love to change the world, but they won’t give me the source code.

I've also posted Lua and LuaBind binaries built in VS10.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
I've also posted Lua and LuaBind binaries built in VS10.


I did mean to comment on your blog post but I might as well do it here.
You are using Lua 5.1 really? Do you not mean 5.1.4?
Is LuaBind build from the latest source not the download version, otherwise it will need a patch and rebuilding for VS2010 (NULL pointer problem), plus the trunk has more bug fixes applied.
No headers, why would you do this? How would a user know which set of headers to grab for the compiled libs?

edit:
After downloading them I also see you are not respecting the MIT licence. :(
@dmail: Thank you for posting the changes. After looking through the gamedev tutorial and another one that I had found, I noticed the openiolib openbaselib ect. wasn't there. Instead luaL_openlibs() was used so I figured it was a change from 4.0.1 to 5.1.4 After reading a bit of the changes that were made in your links, I also found that lua_open() was replaced with luaL_newstate() good to know :)

@skwee: Thanks for the step by step guide to linking lua. It was exactly what I was looking for at the time of the post (I ended up finding the lib and include folder after a while and i forgot to add the 5.1.lib to the project >.< Took me 4 hours....heh...)

@Promit: I'm not exactly sure what those binaries do (then again I'm not very experienced with lua and I'm a somewhat beginner/intermediate programmer) so I will do a little research on them and maybe I'll find an answer.

Thank you for the help everyone. My original problem is done :)

Now that I got lua set up and running, is there a function that will let me change a lua script variable from 1 to 0 and save the new script? I've been trying to find something that will do this but so far the only thing I've found is that tables can be manipulated some how. Not sure how yet because I'm still messing around with things and looking online. I'm trying to create a way that my game will continue off from save file that will essentially know what scripts have been set off already so that it doesn't run them again. My friend and I have decided that saving a temporary script file would be best for this case so that when the "checkpoint" is loaded, the temporary script file will load and any triggers that have been set won't go off again. This is mainly why I'm trying to change the one variable in the lua script from 1 to 0 (trigger on trigger off) This is the approach we've decided on and I think it should work fine, I just need to figure out A) how to change the data in the lua script file from the C++ program B) how to save this file that is essentially the same as the original script file except the trigger variable has changed from 1 to 0.

I appreciate all the time and help everyone has put into their posts. Back to looking for an answer to this! :)
Quote:Original post by dmail
edit:
After downloading them I also see you are not respecting the MIT licence. :(
Yes, I probably should've remembered to include the license text. Maybe I'll amend the archives.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Well I still can't find any functions in the lua library that would allow me to change the value of a variable in a lua script through my C++ program so I wondering if using fstream would be a good way to replace the value of the variable i want to change in the lua script. I don't think this method would be the best way simply because it would have to look through every line of the script (and if the script is long that means it would take longer to check every line). Since CPU's are so fast though I'm not sure if the file search would be noticed during runtime.

Is this a plausible work around for what I'm trying to do? Using this method I could change the variable value then save a temporary copy of the file using fstream. Any input would be appreciated greatly.

This topic is closed to new replies.

Advertisement