Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » passing an objectpointer to lua

This thread is locked; no one can reply to it. rss feed Print
passing an objectpointer to lua
Matthew Hopkins
Member #2,980
December 2002

Have searched the archives for an anwser but can seem to find any.

Anyway, what i want to do is to pass a pointer/reference to an object to lua and in lua be able to change the objects members and call its methods.

Ex.
- cpp code -

Class Test {
public:
int x, y;
void setX(int ix) {x=ix;}
int getX() {return x;}
};

void main() {
Test t;

/* here goes initcode */

lua_getglobal(luaVM, "modT");
lua_pushpointer(luaVM, &t); // ???
lua_call(luaVM, 1, 0);

}

- lua -

function modT(Test t)

local x = t:getX()
t:setX(x+5)
t:y = 5

end

I know this wont work, but what should i do to make it work?

X-G
Member #856
December 2000
avatar

Not much you can do short of writing API functions for manipulating all these values, but that's a huge kludge. I recommend rethinking your design strategy so that you don't have to do things like this instead.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Marcello
Member #1,860
January 2002
avatar

Or you could just use jsgen and let it do all of that for you, automatically.

Marcello

nonnus29
Member #2,606
August 2002
avatar

You could look at it the other way and use function calls from lua to create objects on the c++ side, stick them in a vector then return the vector index to lua and use that in lua in place of a pointer...

Marcello; are you saying you can create objects in spidermonkey that are mirrored on the c++ side? And jsgen wraps this functionality? Can this be done? How does it work? Can you explain that because I really don't understand....

Peter Hull
Member #1,136
March 2001

Marcello probably meant Luabind?

To do it by hand, it depends if you're using Lua 4 or Lua 5

Lua 4
-----
Look at tag methods - you need to provide settable and gettable methods. The table has one 'real' member, a userdata pointing to your C++ object.

Lua 5
-----
Similar but using metatables and a 'full userdata' pointing to the C++ object.

Let me know which Lua you have and I can help you more.

Pete

Marcello
Member #1,860
January 2002
avatar

jsgen will take his class up there and do all the work of making a JavaScript object that behaves and acts just like the C++ object. But also, you can pass the C++ object around as a pointer to other C++ and JavaScript objects and it will let you work with it normally.

For example:

var t = new Test();
t.setX(3);

function foo(test) {
  t.setX(4);
}
foo(t);
var o = new SomeObject();
o.moveTo(t);

There's a better example with jsgen itself... as for LuaBind, jsgen is easier to use and work with, so it's really up to you.

Marcello

X-G
Member #856
December 2000
avatar

Yes, but it won't be Lua, will it? So it's ultimately useless to this guy. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Marcello
Member #1,860
January 2002
avatar

He could port it to lua, it's not very complicated.

Marcello

Korval
Member #1,538
September 2001
avatar

Since you are already using C++, you should definately use LuaBind. It makes this kind of thing trivial, at best.

And ignore Marcello's raving about "jsgen"; LuaBind offers far greater functionality, let alone ease-of-use, than his "jsgen" tool could possibly offer. If for no other reason than LuaBind is a library and thus works through C++ (using template metaprogramming, so the syntax looks a little funky), while "jsgen" is a pre-processing tool that you have to add to your project/makefile and run it on appropriate files.

Oh, and if you're serious about using Lua, definately pick up the book "Programming in Lua". It's a very good read.

Marcello
Member #1,860
January 2002
avatar

Name this greater functionality of which you speak.

Marcello

spellcaster
Member #1,493
September 2001
avatar

Well, but since JavaScript is a "nicer" and more widespread lang than Lua, using JS might be not that bad ;)

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Matthew Hopkins
Member #2,980
December 2002

Im using lua 5.

Will take a look at this LuaBind and see if i can make that work.

JS isnt realy an option, im working on learning myself lua ;)

Go to: