Returning function from Lua

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Returning function from Lua

Steven Lecompte-2
First of all I use Luabind as an interface between Lua (5.1) and C++

I want to return a table with lua functions that I can call in C++. When
I read the docs I had the idea to use a luabind object te catch the
result and then to execute this object. Now in practice this doesn't
really work. Here is my test code:


Lua:
----

Commands = {}

Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end
Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end
Commands.Construct = function (FromProxy, TargetProxy) print
("Construct") end
Commands.Destroy =  function (FromProxy, TartgetProxy) print ("Destroy") end

function SetPropertyCommand (Type)

return Commands

end




C++: (Commands_ is the luabind::object)
----

Commandgroup::Commandgroup (std::string Type, LuaStateObject& Luahandling):
Luahandling_ (Luahandling)
{
  Commands_ = luabind::call_function <luabind::object>
(Luahandling_.Getstate(),
  "SetPropertyCommand", Type);
}

void Commandgroup::ExecCommand (std::string CommandName, ProxyObj&
Target, ProxyObj& Source)
{
  //TEST
  luabind::call_function <void> (Commands_[1], boost::ref(Target),
  boost::ref(Source)); //error
  //TEST
}



Error:
------

Assertion failed: crep && "you are trying to use an unregistered type",
file ../
../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808

This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application's support team for more information.

Press any key to continue.


Now I wonder if Lua returns this as a 'pass by value' or as a reference
I have also tried to pass a luabind object as a reference argument (with
boost::ref) but this didn't work either.
Sorry if this is a noob question but I'm totally new with Lua and Laubind.

Thanks for your help


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Returning function from Lua

Vladislav I. Sobkovskiy

Why you use tables? Luabind classes it is better think, take a look on my test code:

 

test3.lua

----------------------

class 'entity'

 

      function entity:__init(name)

            self.name = name

            print('entity init')

      end

     

      function entity:dump()

            print('\n --- entity dump ---\nname: ' .. self.name)

      end

     

      function entity:foo()

            print('entity foo')

      end

     

class 'test' (entity)

function test:__init(name) super(name)

      print('test init')

end

 

function test:foo()

      entity.foo(self)

     

      print("test foo")

     

end

 

function test:bar()

      print('test bar')

end

 

 

C++ code

------------------------------

object *o1;

object *o2;

 

// manual controlled 'live of objects'

void g()

{

      o1 = new object( (globals(interpreter->state())["test"])("Entity1") );

      o2 = new object( (globals(interpreter->state())["test"])("Entity2") );

      call_member<void> (*o1, "dump");

      call_member<void> (*o2, "dump");

      delete o1;

      delete o2;

}

 

// automatically controlled 'live of objects'

void w()

{

      object o = globals(interpreter->state())["test"];

      object e = object(o("Entity3"));

 

      call_member<void> (e, "dump");

}

 

Try this one.

 

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Steven Lecompte
Sent: Wednesday, August 30, 2006 1:42 PM
To: [hidden email]
Subject: [luabind] Returning function from Lua

 

First of all I use Luabind as an interface between Lua (5.1) and C++

 

I want to return a table with lua functions that I can call in C++. When

I read the docs I had the idea to use a luabind object te catch the

result and then to execute this object. Now in practice this doesn't

really work. Here is my test code:

 

 

Lua:

----

 

Commands = {}

 

Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end

Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end

Commands.Construct = function (FromProxy, TargetProxy) print

("Construct") end

Commands.Destroy =  function (FromProxy, TartgetProxy) print ("Destroy") end

 

function SetPropertyCommand (Type)

 

return Commands

 

end

 

 

 

 

C++: (Commands_ is the luabind::object)

----

 

Commandgroup::Commandgroup (std::string Type, LuaStateObject& Luahandling):

Luahandling_ (Luahandling)

{

  Commands_ = luabind::call_function <luabind::object>

(Luahandling_.Getstate(),

  "SetPropertyCommand", Type);

}

 

void Commandgroup::ExecCommand (std::string CommandName, ProxyObj&

Target, ProxyObj& Source)

{

  //TEST

  luabind::call_function <void> (Commands_[1], boost::ref(Target),

  boost::ref(Source)); //error

  //TEST

}

 

 

 

Error:

------

 

Assertion failed: crep && "you are trying to use an unregistered type",

file ../

../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808

 

This application has requested the Runtime to terminate it in an unusual

way.

Please contact the application's support team for more information.

 

Press any key to continue.

 

 

Now I wonder if Lua returns this as a 'pass by value' or as a reference

I have also tried to pass a luabind object as a reference argument (with

boost::ref) but this didn't work either.

Sorry if this is a noob question but I'm totally new with Lua and Laubind.

 

Thanks for your help

 

 

-------------------------------------------------------------------------

Using Tomcat but need to do more? Need to support web services, security?

Get stuff done quickly with pre-integrated technology to make your job easier

Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________

luabind-user mailing list

[hidden email]

https://lists.sourceforge.net/lists/listinfo/luabind-user

 

 

 

__________ NOD32 1.1668 (20060719) Information __________

 

This message was checked by NOD32 antivirus system.

http://www.eset.com

 

 


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Returning function from Lua

Arvid Norberg-2
In reply to this post by Steven Lecompte-2
On Wed, Aug 30, 2006 at 12:42:06PM +0200, Steven Lecompte wrote:

> First of all I use Luabind as an interface between Lua (5.1) and C++
>
> I want to return a table with lua functions that I can call in C++. When
> I read the docs I had the idea to use a luabind object te catch the
> result and then to execute this object. Now in practice this doesn't
> really work. Here is my test code:
>
>
> Lua:
> ----
>
> Commands = {}
>
> Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end
> Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end
> Commands.Construct = function (FromProxy, TargetProxy) print
> ("Construct") end
> Commands.Destroy =  function (FromProxy, TartgetProxy) print ("Destroy") end
>
> function SetPropertyCommand (Type)
>
> return Commands
>
> end
>
>
>
>
> C++: (Commands_ is the luabind::object)
> ----
>
> Commandgroup::Commandgroup (std::string Type, LuaStateObject& Luahandling):
> Luahandling_ (Luahandling)
> {
>   Commands_ = luabind::call_function <luabind::object>
> (Luahandling_.Getstate(),
>   "SetPropertyCommand", Type);
> }
>
> void Commandgroup::ExecCommand (std::string CommandName, ProxyObj&
> Target, ProxyObj& Source)
> {
>   //TEST
>   luabind::call_function <void> (Commands_[1], boost::ref(Target),
>   boost::ref(Source)); //error
>   //TEST
> }
>
>
>
> Error:
> ------
>
> Assertion failed: crep && "you are trying to use an unregistered type",
> file ../
> ../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808

It looks like you haven't registered ProxyObj with luabind. How does your
bindings look like? Or is it a mistake to try to convert ProxyObj to lua?

> Now I wonder if Lua returns this as a 'pass by value' or as a reference
> I have also tried to pass a luabind object as a reference argument (with
> boost::ref) but this didn't work either.
> Sorry if this is a noob question but I'm totally new with Lua and Laubind.
>
> Thanks for your help

--
Arvid Norberg

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Returning function from Lua

Steven Lecompte-2
In reply to this post by Vladislav I. Sobkovskiy
I already solved the problem (I forgot to declare the ProxyObj class to
use in LUA). But why is it better to use classes here?
It is the Lua function SetPropertyCommand that decides wich 'commandset'
I get based on the type. So I can't call directly to the lua functions
or make objects because I don't know if my type has acces to it.
So making objects (like in your void g() in C++) isn't possible because
I don't know if I may make these objects. However I could make these
objects in Lua and return that, but making tables would be easier then, no?

Thanks for your help.

Vladislav I. Sobkovskiy schreef:

>
> Why you use tables? Luabind classes it is better think, take a look on
> my test code:
>
>  
>
> test3.lua
>
> ----------------------
>
> class 'entity'
>
>  
>
>       function entity:__init(name)
>
>             self.name = name
>
>             print('entity init')
>
>       end
>
>      
>
>       function entity:dump()
>
>             print('\n --- entity dump ---\nname: ' .. self.name)
>
>       end
>
>      
>
>       function entity:foo()
>
>             print('entity foo')
>
>       end
>
>      
>
> class 'test' (entity)
>
> function test:__init(name) super(name)
>
>       print('test init')
>
> end
>
>  
>
> function test:foo()
>
>       entity.foo(self)
>
>      
>
>       print("test foo")
>
>      
>
> end
>
>  
>
> function test:bar()
>
>       print('test bar')
>
> end
>
>  
>
>  
>
> C++ code
>
> ------------------------------
>
> object *o1;
>
> object *o2;
>
>  
>
> // manual controlled 'live of objects'
>
> void g()
>
> {
>
>       o1 = new object(
> (globals(interpreter->state())["test"])("Entity1") );
>
>       o2 = new object(
> (globals(interpreter->state())["test"])("Entity2") );
>
>       call_member<void> (*o1, "dump");
>
>       call_member<void> (*o2, "dump");
>
>       delete o1;
>
>       delete o2;
>
> }
>
>  
>
> // automatically controlled 'live of objects'
>
> void w()
>
> {
>
>       object o = globals(interpreter->state())["test"];
>
>       object e = object(o("Entity3"));
>
>  
>
>       call_member<void> (e, "dump");
>
> }
>
>  
>
> Try this one.
>
>  
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Steven Lecompte
> Sent: Wednesday, August 30, 2006 1:42 PM
> To: [hidden email]
> Subject: [luabind] Returning function from Lua
>
>  
>
> First of all I use Luabind as an interface between Lua (5.1) and C++
>
>  
>
> I want to return a table with lua functions that I can call in C++. When
>
> I read the docs I had the idea to use a luabind object te catch the
>
> result and then to execute this object. Now in practice this doesn't
>
> really work. Here is my test code:
>
>  
>
>  
>
> Lua:
>
> ----
>
>  
>
> Commands = {}
>
>  
>
> Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end
>
> Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end
>
> Commands.Construct = function (FromProxy, TargetProxy) print
>
> ("Construct") end
>
> Commands.Destroy =  function (FromProxy, TartgetProxy) print
> ("Destroy") end
>
>  
>
> function SetPropertyCommand (Type)
>
>  
>
> return Commands
>
>  
>
> end
>
>  
>
>  
>
>  
>
>  
>
> C++: (Commands_ is the luabind::object)
>
> ----
>
>  
>
> Commandgroup::Commandgroup (std::string Type, LuaStateObject&
> Luahandling):
>
> Luahandling_ (Luahandling)
>
> {
>
>   Commands_ = luabind::call_function <luabind::object>
>
> (Luahandling_.Getstate(),
>
>   "SetPropertyCommand", Type);
>
> }
>
>  
>
> void Commandgroup::ExecCommand (std::string CommandName, ProxyObj&
>
> Target, ProxyObj& Source)
>
> {
>
>   //TEST
>
>   luabind::call_function <void> (Commands_[1], boost::ref(Target),
>
>   boost::ref(Source)); //error
>
>   //TEST
>
> }
>
>  
>
>  
>
>  
>
> Error:
>
> ------
>
>  
>
> Assertion failed: crep && "you are trying to use an unregistered type",
>
> file ../
>
> ../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808
>
>  
>
> This application has requested the Runtime to terminate it in an unusual
>
> way.
>
> Please contact the application's support team for more information.
>
>  
>
> Press any key to continue.
>
>  
>
>  
>
> Now I wonder if Lua returns this as a 'pass by value' or as a reference
>
> I have also tried to pass a luabind object as a reference argument (with
>
> boost::ref) but this didn't work either.
>
> Sorry if this is a noob question but I'm totally new with Lua and Laubind.
>
>  
>
> Thanks for your help
>
>  
>
>  
>
> -------------------------------------------------------------------------
>
> Using Tomcat but need to do more? Need to support web services, security?
>
> Get stuff done quickly with pre-integrated technology to make your job
> easier
>
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
>
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>
> _______________________________________________
>
> luabind-user mailing list
>
> [hidden email]
>
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>  
>
>  
>
>  
>
> __________ NOD32 1.1668 (20060719) Information __________
>
>  
>
> This message was checked by NOD32 antivirus system.
>
> http://www.eset.com
>
>  
>
>  
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> ------------------------------------------------------------------------
>
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>  


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Returning function from Lua

Vladislav I. Sobkovskiy
In reply to this post by Steven Lecompte-2

 

 


From: Vladislav I. Sobkovskiy
Sent: Wednesday, August 30, 2006 2:18 PM
To: '[hidden email]'
Subject: RE: [luabind] Returning function from Lua

 

Why you use tables? Luabind classes it is better think, take a look on my test code:

 

test3.lua

----------------------

class 'entity'

 

      function entity:__init(name)

            self.name = name

            print('entity init')

      end

     

      function entity:dump()

            print('\n --- entity dump ---\nname: ' .. self.name)

      end

     

      function entity:foo()

            print('entity foo')

      end

     

class 'test' (entity)

function test:__init(name) super(name)

      print('test init')

end

 

function test:foo()

      entity.foo(self)

     

      print("test foo")

     

end

 

function test:bar()

      print('test bar')

end

 

 

C++ code

------------------------------

object *o1;

object *o2;

 

// manual controlled 'live of objects'

void g()

{

      o1 = new object( (globals(interpreter->state())["test"])("Entity1") );

      o2 = new object( (globals(interpreter->state())["test"])("Entity2") );

      call_member<void> (*o1, "dump");

      call_member<void> (*o2, "dump");

      delete o1;

      delete o2;

}

 

// automatically controlled 'live of objects'

void w()

{

      object o = globals(interpreter->state())["test"];

      object e = object(o("Entity3"));

 

      call_member<void> (e, "dump");

}

 

Try this one.

 

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Steven Lecompte
Sent: Wednesday, August 30, 2006 1:42 PM
To: [hidden email]
Subject: [luabind] Returning function from Lua

 

First of all I use Luabind as an interface between Lua (5.1) and C++

 

I want to return a table with lua functions that I can call in C++. When

I read the docs I had the idea to use a luabind object te catch the

result and then to execute this object. Now in practice this doesn't

really work. Here is my test code:

 

 

Lua:

----

 

Commands = {}

 

Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end

Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end

Commands.Construct = function (FromProxy, TargetProxy) print

("Construct") end

Commands.Destroy =  function (FromProxy, TartgetProxy) print ("Destroy") end

 

function SetPropertyCommand (Type)

 

return Commands

 

end

 

 

 

 

C++: (Commands_ is the luabind::object)

----

 

Commandgroup::Commandgroup (std::string Type, LuaStateObject& Luahandling):

Luahandling_ (Luahandling)

{

  Commands_ = luabind::call_function <luabind::object>

(Luahandling_.Getstate(),

  "SetPropertyCommand", Type);

}

 

void Commandgroup::ExecCommand (std::string CommandName, ProxyObj&

Target, ProxyObj& Source)

{

  //TEST

  luabind::call_function <void> (Commands_[1], boost::ref(Target),

  boost::ref(Source)); //error

  //TEST

}

 

 

 

Error:

------

 

Assertion failed: crep && "you are trying to use an unregistered type",

file ../

../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808

 

This application has requested the Runtime to terminate it in an unusual

way.

Please contact the application's support team for more information.

 

Press any key to continue.

 

 

Now I wonder if Lua returns this as a 'pass by value' or as a reference

I have also tried to pass a luabind object as a reference argument (with

boost::ref) but this didn't work either.

Sorry if this is a noob question but I'm totally new with Lua and Laubind.

 

Thanks for your help

 

 

-------------------------------------------------------------------------

Using Tomcat but need to do more? Need to support web services, security?

Get stuff done quickly with pre-integrated technology to make your job easier

Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________

luabind-user mailing list

[hidden email]

https://lists.sourceforge.net/lists/listinfo/luabind-user

 

 

 

__________ NOD32 1.1668 (20060719) Information __________

 

This message was checked by NOD32 antivirus system.

http://www.eset.com

 

 


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user