Sign in to follow this  
SonicD007

Creating a lua lib to use for C++?

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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. :(

Share this post


Link to post
Share on other sites
@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! :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sorry I do not fully understand what you are trying to do in saving the file to disk. If all you want to do is change the value of a number and it is a global then you would push the value to the Lua stack and call lua_setglobal, or call a function in the script with the new value etc.


extern lua_State* l;
...
lua_pushnumber(l,1);
lua_setglobal(l,"globalVariableName");





Did you have a look at the reference manual? You need to understand about the stack and C API

Note that the reference manual for 5.1 states that lua_setglobal
Quote:
is defined as a macro:

#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, s)

Do not use LUA_GLOBALSINDEX if you want to use a newer version of Lua, as it was removed from 5.2

Share this post


Link to post
Share on other sites
Well the reason for saving the file is because the scripting system is being used just for triggers. So when the game is saved, we want to save the script related to the level so that any triggers that have been set already won't go off again when the game is loaded from that save. So basicly the variable would be the on or off switch for the trigger.

I need to read more about the stack because I was reading the documentation but I didn't understand it too well.

Share this post


Link to post
Share on other sites
Ok I see so you want to save the state. I would probably use a table for this into which the trigger and on/off values are stored, when a save is called traverse the table saving the state to disk.

For a simple and not full proof example:

function save_table( t ,file_name)
f = io.open(file_name,"w")
f:write("someTableName = {")
for key,value in pairs(t)do
f:write( tostring(key) ... "=" .. tostring(value) .. ",\n")
end
f:write("}")
end




Edit: missed a comma from the write operation, also note that it is fine in Lua for the last entry into a table to have a suffix comma.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this