|
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. Class Test { void main() { /* here goes initcode */ lua_getglobal(luaVM, "modT"); } - lua - function modT(Test t) local x = t:getX() end I know this wont work, but what should i do to make it work? |
X-G
Member #856
December 2000
|
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. -- |
Marcello
Member #1,860
January 2002
|
Or you could just use jsgen and let it do all of that for you, automatically. Marcello |
nonnus29
Member #2,606
August 2002
|
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 Lua 5 Let me know which Lua you have and I can help you more. Pete
|
Marcello
Member #1,860
January 2002
|
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
|
Yes, but it won't be Lua, will it? So it's ultimately useless to this guy. -- |
Marcello
Member #1,860
January 2002
|
He could port it to lua, it's not very complicated. Marcello |
Korval
Member #1,538
September 2001
|
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
|
Name this greater functionality of which you speak. Marcello |
spellcaster
Member #1,493
September 2001
|
Well, but since JavaScript is a "nicer" and more widespread lang than Lua, using JS might be not that bad -- |
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 |
|