Jump to content
  • Advertisement
Sign in to follow this  

C++ objects in Lua both ways - the solution

This topic is 4650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Some time ago, say two weeks, I started developing with lua. I've found it a nice language, however, C++ objects are not easy supported. Then I've found a toLua library which I like very much. But after a glance a problem appeared. toLua supports only full userdata and not light userdata. And that means that it can only operate on objects created in lua script. What I wanted was the system that would let operate on both light and full userdata (and that means c++ objects created in lua and in c++) in the same way. And that would be object:method(arg1, arg2). That is, in a lua standard way to call "methods". While toLua make it possible for full userdata, the light userdata cannot be accesed in this way. What I've done is a piece of code that creates a binder table that let call methods on light userdata. Here is the code:
function binder_index(table, key)
	f = table[1][key]
	if (f == nil) then
		error("Method/field "..key.." not defined")
	if type(f) == "function" then
	-- method wrapper
		local method = f
		return	function (b, ...)
					return method(b[0], unpack(arg))
	-- variable
		return f

function bind(class_name, light_userdata)
	local binder = {}
	binder[0] = light_userdata
	binder[1] = _G[class_name]
	if (binder[1] == nil) then
		error("Class "..class_name.." not defined")
	m = {}
	m["__index"] = binder_index
	setmetatable(binder, m)
	return binder
The code is quite simple, yet very useful. There is a one problem with using it. For some strange reason toLua (or maybe lua) returns full userdata in a different way than light userdata. For this code to work you need to pass a ligth UD as a pointer to pointer or modify toLua functions to return light userdata in a correct way. Also you have to compile your code with TOLUA_RELEASE defined to avoid toLua type checking. Here is an example how to use this code:
function sample_update(self, time)
	self = bind("CSampleProcess", self)
	dr = time * 30;
	self:setRotation(self:getRotation() + dr)
Here CSampleProcess is a class binded to lua with toLua library. As you can see all you have to do is create a binder with bind function, giving the name of the class a light userdata is. It's the first version of this code and it only supports methods (class variables and operators are not working). In the future I'm trying to add variables and operators and maybe defining new variables on lua side. I'll appreciate any comments on this code. Thanks [Edited by - cBull on October 24, 2005 6:34:30 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!