trouble accessing global from C

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble accessing global from C

Max Ischenko
I've been writing C function that needs access to Lua global variable.
I use 
 lua_getglobal(L, "session");
but it returns `nil'.

But I *have* global `session' var. I even done setglobal("session", session)!

Execution goes as this

main.cpp:
register(c-func)
dofile(main.lua)

main.lua:
session={...}
call c-func

-- 
Trap full -- please empty.

Reply | Threaded
Open this post in threaded view
|

Re: trouble accessing global from C

Luiz Henrique de Figueiredo
It's hard to tell what is wrong, assuming that in main.cpp you have lua_open.
Could you please post or send me the smallest program that shows this problem?
Thanks.
--lhf

>I've been writing C function that needs access to Lua global variable.
>I use 
> lua_getglobal(L, "session");
>but it returns `nil'.
>
>But I *have* global `session' var. I even done setglobal("session", session)!
>
>Execution goes as this
>
>main.cpp:
>register(c-func)
>dofile(main.lua)
>
>main.lua:
>session={...}
>call c-func

Reply | Threaded
Open this post in threaded view
|

Re: trouble accessing global from C

Max Ischenko
	Hi there, Luiz Henrique de Figueiredo!
	On Monday, 08 of January you wrote:

> It's hard to tell what is wrong, assuming that in main.cpp you have lua_open.
> Could you please post or send me the smallest program that shows this problem?
> Thanks.

I forgot to say that 'session' is a table. May this change sthg?

Here excrept from my code:

main.lua:
svd_dofile("session");
session.user = users.guest -- default user profile is `guest'
session.load()
ITM=session.itm.value -- set to 180

lib.cpp:
lua_getglobal(L, "ITM");
cerr << lua_tostring(L, -1) << endl; // prints 180
lua_getglobal(L, "session");
cerr << lua_tostring(L, -1) << endl; // prints (null)




> >I've been writing C function that needs access to Lua global variable.
> >I use 
> > lua_getglobal(L, "session");
> >but it returns `nil'.
> >
> >But I *have* global `session' var. I even done setglobal("session", session)!
> >
> >Execution goes as this
> >
> >main.cpp:
> >register(c-func)
> >dofile(main.lua)
> >
> >main.lua:
> >session={...}
> >call c-func

-- 
Best regards, Maxim F. Ischenko.

Reply | Threaded
Open this post in threaded view
|

Re: trouble accessing global from C

Max Ischenko
	On Tuesday, 09 of January I wrote:

> 
> 	Hi there, Luiz Henrique de Figueiredo!
> 	On Monday, 08 of January you wrote:
> 
> > It's hard to tell what is wrong, assuming that in main.cpp you have lua_open.
> > Could you please post or send me the smallest program that shows this problem?
> > Thanks.
> 
> I forgot to say that 'session' is a table. May this change sthg?
> 
> Here excrept from my code:
> 
> main.lua:
> svd_dofile("session");
> session.user = users.guest -- default user profile is `guest'
> session.load()
> ITM=session.itm.value -- set to 180
> 
> lib.cpp:
> lua_getglobal(L, "ITM");
> cerr << lua_tostring(L, -1) << endl; // prints 180
> lua_getglobal(L, "session");
> cerr << lua_tostring(L, -1) << endl; // prints (null)
>

I bring my apologizes. As usual, I overlooked my code.

I need to get value of variable `session.itm.value. I use

	lua_getglobal(L, "session");
	lua_pushstring(L, "itm");
	lua_pushstring(L, "value");
	lua_gettable(L, -3);
	lua_gettable(L, -2);
	cerr << lua_tostring(L, -1) << endl;

But this returs error: Cannot index string value.

-- 
Best regards, Maxim F. Ischenko.

Reply | Threaded
Open this post in threaded view
|

Re: trouble accessing global from C

Max Ischenko
Apologizes again. Problem's been fixed.

-- 
Best regards, Maxim F. Ischenko.