lua-users home
lua-l archive

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


Hello,

I would like to enable communication between LUA and C++ classes. I don't 
want to use "tolua" or any other tool.

Considere the following code, do you think these is a good way (or almost 
quite right) or do you think I was lucky not to have test.exe crashing ?
Is there a way to simplify the whole operation ?

Thanks a lot !!

-------------------------- test.cpp

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern "C"
{
	#include "lua.h"
	#include "luadebug.h"
	#include "lualib.h"
}

class myObject
{
	double	aProperty;

public:

	void setProperty(double pParam);
	double getProperty();
};

double myObject::getProperty()
{
	return aProperty;
}

void myObject::setProperty(double pParam)
{
	aProperty=pParam;
	printf("Inside setProperty\n");
}

myObject *global_Object;

void createNewObject(void)
{
	myObject *temp;
	temp = new(myObject);
	lua_pushuserdata(temp);
	global_Object = temp; // Why using temp ? just to see
}

void setObjectProperty(void)
{
	lua_Object params;

	params=lua_getparam(1);

	((myObject*)(lua_getuserdata(params)))->setProperty(lua_getnumber(lua_g  
etparam(2)));
}

int main (int argc, char *argv[])
{
	lua_open();
	lua_register("createNewObject",&createNewObject);
	lua_register("setObjectProperty",&setObjectProperty);
	lua_dofile("test.lua");

	printf("%f\n",global_Object->getProperty());

	lua_close();

	delete global_Object;
	return 0;
}

-------------------------- test.lua

aPrettyCube = createNewObject();

setProperty(aPrettyCube,123);