[solved]using lua with c++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

[solved]using lua with c++

Post by cndr »

I am new to lua, and I would like to make lua change variables in c++, say I have something like the following code, the hardest thing I have learning lua, is integrating it into c++.

Code: Select all

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
 int s = 0;
int x;
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,"file.lua"); // lua would set x to 5
printf("x is %d .",x); //how would I get lua to set x?
lua_close(L);
return 0;
}
Last edited by cndr on Mon Apr 26, 2010 5:09 pm, edited 1 time in total.
Long Live The Queen!
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: using lua with c++

Post by mv2112 »

There are a few ways you could do this but here i think is the easiest.

Code: Select all

    extern "C"
    {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    }
    int main()
    {
    int s = 0;
    int x;
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luaL_dofile(L,"file.lua"); // lua would set x to 5
    x=lua_getglobal(L,"x"); //assuming the variable in the lua script is also named x 
    printf("x is %d .",x); //how would I get lua to set x?
    lua_close(L);
    return 0;
    }

You could also register a SetX function to lua but X would have to be global.
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

Re: using lua with c++

Post by cndr »

didn't work for me, maybe I should just stick with pure lua for a while, it seems getting the code to work with c++ is harder than I thought, when I ran the code I got the following error message:

Code: Select all

error: void value not ignored as it ought to be
the lua file just had

Code: Select all

x =5;
Long Live The Queen!
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: using lua with c++

Post by mv2112 »

Oops, my code was wrong, this works, i tested it:

Code: Select all

        extern "C"
        {
        #include "lua.h"
        #include "lualib.h"
        #include "lauxlib.h"
        }
        int main()
        {
        int s = 0;
        int x;
        lua_State *L = lua_open();
        luaL_openlibs(L);
        luaL_dofile(L,"file.lua"); // lua would set x to 5
        lua_getglobal(L,"x"); //assuming the variable in the lua script is also named x
	x=lua_tonumber(L,-1); //gets x from the lua stack
        printf("x is %d .",x); 
        lua_close(L);
        return 0;
        }
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

Re: using lua with c++

Post by cndr »

Thanks for the help I really appreciate it
Long Live The Queen!
Post Reply