Re: Luabind and passing classes
Nathaniel Lewis <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <1279895462.23209.0.camel@XtremePC> |
It said exited on a run time exception. (luabind::error)
Nathaniel
On Fri, 2010-07-23 at 15:42 +1200, Nigel Atkinson wrote:
> Really? :-/
>
> You first line should work! What was the error you got?
>
> Nigel
>
> On Thu, 2010-07-22 at 07:03 -0700, Nathaniel Lewis wrote:
> > Oh, I see. The problem was in my Lua script (I just tried it). I tried
> > to call:
> >
> > print ("Lua: " .. obj:getSting())
> >
> > That caused it to error. However this worked.
> >
> > local str = obj:getString()
> > print ("Lua: " .. str)
> >
> > Nathaniel
> >
> > On Thu, 2010-07-22 at 18:19 +1200, Nigel Atkinson wrote:
> > > Heh, I should have tried you code before I replied....
> > >
> > > anyhow - using std:string will work, but I looks like const char *
> > > should work too.
> > >
> > > I tried this using gcc -
> > >
> > > #include <iostream>
> > >
> > > #include "lua.hpp"
> > > #include "luabind/luabind.hpp"
> > > #include "luabind/operator.hpp"
> > >
> > > class TestClass {
> > > std::string myString;
> > > public:
> > > TestClass() {
> > > myString = "Not Initalized";
> > > }
> > > TestClass( const char* aString ) {
> > > myString = std::string(aString);
> > > }
> > > void setString( const char* aString ) {
> > > myString = std::string(aString);
> > > }
> > > const char* getString( void ) {
> > > return myString.c_str();
> > > }
> > > void printString( void ) {
> > > std::cout << myString << std::endl;
> > > }
> > > static void configureLuaObject(lua_State* lVM) {
> > > luabind::module(lVM) [
> > > luabind::class_<TestClass>("TestClass")
> > > .def(luabind::constructor <> ())
> > > .def(luabind::constructor<const char*>())
> > > .def("setString", &TestClass::setString)
> > > .def("getString", &TestClass::getString)
> > > .def("printString", &TestClass::printString)
> > > ];
> > > }
> > > void callFunction(lua_State* lVM) {
> > > luabind::call_function<void>(lVM, "AFunction",
> > > boost::ref(this));
> > > }
> > > };
> > >
> > > int main()
> > > {
> > > lua_State* L = luaL_newstate();
> > > luaL_openlibs(L);
> > > using namespace luabind;
> > > open(L);
> > >
> > > TestClass::configureLuaObject(L);
> > >
> > > if (luaL_dostring(L, "t = TestClass('My Test')\n"
> > > "print(t:getString())") == 1)
> > > {
> > > std::cout
> > > << "Couldn't run Lua program: "
> > > << lua_tostring(L, -1)
> > > << std::endl;
> > > }
> > > }
> > >
> > > and it worked.
> > >
> > > Nigel
> > >
> > > On Wed, 2010-07-21 at 19:42 -0700, Nathaniel Lewis wrote:
> > > > Thanks I spent a bit and got what I wanted working sort of. How do you
> > > > you return strings from a exposed function? I got the exposing working
> > > > fine and I can pass a string as a parameter but how do I get it as a
> > > > result. the class (part of testing) is
> > > >
> > > > class TestClass {
> > > > std::string myString;
> > > > public:
> > > > TestClass() {
> > > > myString = "Not Initalized";
> > > > }
> > > > TestClass( const char* aString ) {
> > > > myString = std::string(aString);
> > > > }
> > > > void setString( const char* aString ) {
> > > > myString = std::string(aString);
> > > > }
> > > > const char* getString( void ) {
> > > > return myString.c_str();
> > > > }
> > > > void printString( void ) {
> > > > std::cout << myString << std::endl;
> > > > }
> > > > static void configureLuaObject(lua_State* lVM) {
> > > > luabind::module(lVM) [
> > > > luabind::class_<TestClass>("TestClass")
> > > > .def(luabind::constructor <> ())
> > > > .def(luabind::constructor<const char*>())
> > > > .def("setString", &TestClass::setString)
> > > > .def("getString", &TestClass::getString)
> > > > .def("printString", &TestClass::printString)
> > > > ];
> > > > }
> > > > void callFunction(lua_State* lVM) {
> > > > luabind::call_function<void>(lVM, "AFunction",
> > > > boost::ref(this));
> > > > }
> > > > };
> > > >
> > > > getString throws an error when run.
> > > > Nathaniel
> > > >
> > > > On Thu, 2010-07-22 at 13:40 +1200, Nigel Atkinson wrote:
> > > > > Hi Nathaniel,
> > > > >
> > > > > Short answer: Yes.
> > > > >
> > > > > If you bind a class using Luabind it can be passed to Lua functions as
> > > > > a parameter. When you bind a class you do not need to bind the
> > > > > class's constructor. Omitting the constructor prevents your Lua
> > > > > script from being able to instantiate that particular class, however
> > > > > Lua can otherwise use instances passed to it from C++.
> > > > >
> > > > > I do this quite often with singleton classes.
> > > > >
> > > > > Nigel Atkinson
> > > > >
> > > > > On Wed, 2010-07-21 at 16:53 -0700, Nathaniel Lewis wrote:
> > > > > > I was reading through some tutorials and I had a question unanswered.
> > > > > > How do I pass a specific class to Lua? I have a class that represents a
> > > > > > world in my game engine and I need some way to talk with it from Lua. I
> > > > > > think LuaBind is the answer but I don't know. I would never want to
> > > > > > create the class from Lua. Instead I would want to pass a reference to
> > > > > > the one created on the c++ side to Lua and then call functions inside of
> > > > > > it. Is this possible with LuaBind? Basically a simple script would
> > > > > > look like this
> > > > > >
> > > > > > -- called when map is loaded by engine --
> > > > > > function Init (worldInstance)
> > > > > > -- do stuff --
> > > > > > end
> > > > > >
> > > > > > -- called after every frame is rendered --
> > > > > > Update = coroutine.wrap( function (worldInstance, timeSinceLastFrame)
> > > > > > while true do
> > > > > > -- do stuff + call coroutine.yield() when waiting for things --
> > > > > > end
> > > > > > end)
> > > > > > Nathaniel
> > > > > >
> > > > > >
> > > > > > ------------------------------------------------------------------------------
> > > > > > This SF.net email is sponsored by Sprint
> > > > > > What will you do first with EVO, the first 4G phone?
> > > > > > Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> > > > > > _______________________________________________
> > > > > > luabind-user mailing list
> > > > > > [email protected]
> > > > > > https://lists.sourceforge.net/lists/listinfo/luabind-user
> > > > >
> > > > > ------------------------------------------------------------------------------
> > > > > This SF.net email is sponsored by Sprint
> > > > > What will you do first with EVO, the first 4G phone?
> > > > > Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> > > > > _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user
> > > >
> > > >
> > > >
> > > > ------------------------------------------------------------------------------
> > > > This SF.net email is sponsored by Sprint
> > > > What will you do first with EVO, the first 4G phone?
> > > > Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> > > > _______________________________________________
> > > > luabind-user mailing list
> > > > [email protected]
> > > > https://lists.sourceforge.net/lists/listinfo/luabind-user
> > >
> > > ------------------------------------------------------------------------------
> > > This SF.net email is sponsored by Sprint
> > > What will you do first with EVO, the first 4G phone?
> > > Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> > > _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
> >
> >
> > ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Sprint
> > What will you do first with EVO, the first 4G phone?
> > Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first