🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lua : Push void* onto the stack

Started by
4 comments, last by Ironica 20 years, 11 months ago
Hey. Was wondering, is it possible to push a void* onto the stack? What I wanna do is, create a function to push a pointer to a C++ class (casted as void*, maybe) onto the stack. I wanna be able to create Lua variables of type myclass. So I could write something like:

myvar = newmyclass()
 
in Lua. newmyclass() would be a function written in C++, which creates a variable of type myclass, and then returns a pointer to it to Lua. It possible? I''m sorry if it doesn''t make sense, I don''t understand Lua that well yet.
Advertisement
Yes, you could push that onto LUas stack as a light user data, but keep in mind that LUA can''t do anything with it unless it has a metatable.

If you want LUA to access functions of the class, check out Luna.
Thanks for the reply. But how do you actually do that? >.<

I recently discovered that Lua tables are awesome! They do exactly what I want, I''ve even given up using C++ classes in my project. But the thing is, I still want to access a SOCKET through Lua. I am new to Lua, so I would appreciate some help >.< hehe.
Ahh, I''m on the right track now, thanks. But I still haven''t got it to work >.< Check out some of my functions.
#define LUA_CONNECT_FUNC "connection"#define LUA_NOCONNECT_FUNC LUA_CONNECT_FUNC "() was not defined!"void newConnection(SOCKET sock){	lua_State *L = lua_newthread(l);	lua_getglobal(L, LUA_CONNECT_FUNC);	if(!lua_isfunction(L, -1)){		output(LUA_NOCONNECT_FUNC);	}else{		lua_pushlightuserdata(L, &sock);		lua_call(L, 1, 0);	}	shutdown(sock, 0);	closesocket(sock);}

Everytime my game receives a new connection, it creates a new thread, and passes the SOCKET to the above function. The output function outputs the message to my gameserver console. The Lua function it calls (connection), is here:
function connection(sock)	output("Got a connection! " .. tostring(sock))	send(sock, "Hello!")end 

That''s just a test function so far really. It gets called ok, and outputs:
Got a connection! userdata: 00DAFFBC
Fine. That send function isn''t working properly. I don''t know what''s wrong with it, but it doesn''t send the message to the client. Here it is:
static int lgame_socksend(lua_State *L){	if(lua_islightuserdata(L, -1) && lua_isstring(L, -2)){		SOCKET *sock = (SOCKET*) lua_touserdata(L, -1);		const char *c = lua_tostring(L, -2);		send(*sock, c, sizeof(c), 0);	}	lua_pop(L, 2);	return 0;}

Anyone see why it doesn''t work? There is no such function as lua_tolightuserdata, so I used lua_touserdata, that ok? I also tryed lua_topointer, but the same thing happenned. This is my first Lua project, so I''m still learning. It''s great, just a few problems understanding, hehe. Would be greatful if anyone could help >.<
Aren''t the arguments in direct order on stack, with -1 being the last element (top of stack) and -2 being the one before that.

If you remove the - signs from your send function stack indices it should work (or swap the indices).
Thank you very much!

Can''t believe it was such a dumb mistake! Hehe =/

This topic is closed to new replies.

Advertisement