store pointer as a global

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

store pointer as a global

Max Ischenko
I'm newbie to lua and I wonder how one can setglobal a C pointer
from C code.

There is a pushnumber() func. but it requires a double...
I suspect userdata may be used here but cannot figure out how.

-- 
One good reason why computers can do more work than people is that they
never have to stop and answer the phone.

Reply | Threaded
Open this post in threaded view
|

Re: store pointer as a global

Erik Hougaard
> I'm newbie to lua and I wonder how one can setglobal a C pointer
> from C code.

Lua has a special data type called 'userdata' userdata can hold a C pointer
(It was actually designed to do so).. Look in liolib.c for a example (FILE
*) stored in userdata..

/Erik


Reply | Threaded
Open this post in threaded view
|

Re: store pointer as a global

Markus Ewald
In reply to this post by Max Ischenko
Max Ischenko schrieb:

> I'm newbie to lua and I wonder how one can setglobal a C pointer
> from C code.

Try

   nTag = lua_newtag(pLuaState);
   lua_pushusertag(pLuaState, pMyPointer, nTag);
   lua_setglobal(pLuaState, "VariableName");

where nTag is an integer, pLuaState the state you created using lua_open().

VariableName is the name of the global and pMyPointer the pointer you want
to store as userdata in it.

-Markus-



Reply | Threaded
Open this post in threaded view
|

Re: store pointer as a global

Max Ischenko
	Hi there, Markus Ewald!
	On Tuesday, 12 of December you wrote:

> Max Ischenko schrieb:
> 
> > I'm newbie to lua and I wonder how one can setglobal a C pointer
> > from C code.
> 
> Try
> 
>    nTag = lua_newtag(pLuaState);
>    lua_pushusertag(pLuaState, pMyPointer, nTag);
>    lua_setglobal(pLuaState, "VariableName");
> 
> where nTag is an integer, pLuaState the state you created using lua_open().
> 
> VariableName is the name of the global and pMyPointer the pointer you want
> to store as userdata in it.

Thank you for your explanaition.

-- 
Best regards, Maxim F. Ischenko.