lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am now starting to understand a bit more how Lua works, thanks to Rici
Lake who has been helping me out.

My next question is, i have this code:

static int MyTag;
static int MyVal = 3;

static int MyFunc(lua_State *L)
	{
	lua_settop(L,1);
	lua_pushusertag(L, &n, MyTag);
	return 1;
	}

static int GetMyVal(lua_State *L)
	{
	int* var=(int*) lua_touserdata(L,2);
	lua_pushnumber(L,*var);
	return 1;
	}

static int SetMyVal(lua_State *L)
	{
	int* var=(int*) lua_touserdata(L,2);
	int val=(int) lua_tonumber(L,3);

	*var=val;
	return 0;
	}

lua_register(L, "MyFunc", MyFunc);
MyTag = lua_newtag(L);
lua_pushcfunction(L, GetMyVal);
lua_settagmethod(L, MyTag , "getglobal");
lua_pushcfunction(L, SetMyVal);
lua_settagmethod(L, MyTag , "setglobal");

To make this code work I would write in the script:

MyFunc(x)

Which would effectively map x in the script to MyVal in C.  Now my question
is, how do I do the above, but all in C, add x to the variables in the
script, but add the functionality that happens when I call MyFunc, without
actually having to call MyFunc?

Chris Percival
Software Engineer
Interaxis Computing Ltd.
DDI:  +44 (0)1249 700072
http://www.interaxis.co.uk/