How to pass value from C++ to lua function by reference?

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

How to pass value from C++ to lua function by reference?

1 1

When programming on C++, you can do the following:

void byReference(int &y)
{
    y = 5;
}

int main()
{
   int x = 2;      // x = 2
   byReference(x); // x = 5
}

How to do the same using luabind?

Luabind docs says:

If you want to pass a parameter as a reference, you have to wrap it with the Boost.Ref.

Like this:

int ret = call_function(L, "fun", boost::ref(val));

But when I'm trying to do this:

#include <iostream>
#include <conio.h>

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

#include <luabind\luabind.hpp>

using namespace std;
using namespace luabind;

int main() 
{
  lua_State *myLuaState = luaL_newstate();
  open(myLuaState);

 int x = 2;

 
do { luaL_dofile(myLuaState, "script.lua");

 cout<<"x before = "<< x <<endl;

  try
{
call_function<void>(myLuaState, "test", boost::ref(x));
}
catch(const std::exception &TheError)
{
cerr << TheError.what() << endl;
}
cout<<"x after = "<< x <<endl;

  } while(_getch() != 27);

 lua_close(myLuaState); }

script.lua:

function test(x)
    x = 7
end

It gives me a "Trying to use unregistered class" error.

So, how to pass value from C++ to lua function by reference, so I can change it inside the script and it will be changed in c++ program too? I'm using boost 1.55.0, lua 5.1, luabind 0.9.1

Thanks.

P.S:
I posted the same question on StackOverflow, but seems like nobody knows what to do.

------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: How to pass value from C++ to lua function by reference?

Michael Steinberg
Hello,

It is impossible to pass object types that are modelled in lua with built-in types by reference. In Lua, there exists no attribute to types like reference or value type. A type is either a reference type (tables), or a value type (built-in types like number).
What you can do however is return the new value in the lua-side. A policy could be established, that transforms reference integral types to a list of parsed return types, so it is transparent on the c++ side. That would place hard constraints on the lua side though.

Kind regards,
Michael

Am 03.08.2014 21:09, schrieb 1 1:

When programming on C++, you can do the following:

void byReference(int &y)
{
    y = 5;
}

int main()
{
   int x = 2;      // x = 2
   byReference(x); // x = 5
}

How to do the same using luabind?

Luabind docs says:

If you want to pass a parameter as a reference, you have to wrap it with the Boost.Ref.

Like this:

int ret = call_function(L, "fun", boost::ref(val));

But when I'm trying to do this:

#include <iostream>
#include <conio.h>

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

#include <luabind\luabind.hpp>

using namespace std;
using namespace luabind;

int main() 
{
  lua_State *myLuaState = luaL_newstate();
  open(myLuaState);

  int x = 2;


  do
  {
    luaL_dofile(myLuaState, "script.lua");

    cout<<"x before = "<< x <<endl;


    try 
    {
        call_function<void>(myLuaState, "test", boost::ref(x));
    }
    catch(const std::exception &TheError) 
    {
        cerr << TheError.what() << endl;

    }

    cout<<"x after = "<< x <<endl;


   } while(_getch() != 27);


  lua_close(myLuaState);
}

script.lua:

function test(x)
    x = 7
end

It gives me a "Trying to use unregistered class" error.

So, how to pass value from C++ to lua function by reference, so I can change it inside the script and it will be changed in c++ program too? I'm using boost 1.55.0, lua 5.1, luabind 0.9.1

Thanks.

P.S:
I posted the same question on StackOverflow, but seems like nobody knows what to do.


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds


_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
1 1
Reply | Threaded
Open this post in threaded view
|

Re: How to pass value from C++ to lua function by reference?

1 1

Thank you for your answer, Michael!

On 7 Aug 2014 16:21, "Michael Steinberg" <[hidden email]> wrote:
Hello,

It is impossible to pass object types that are modelled in lua with built-in types by reference. In Lua, there exists no attribute to types like reference or value type. A type is either a reference type (tables), or a value type (built-in types like number).
What you can do however is return the new value in the lua-side. A policy could be established, that transforms reference integral types to a list of parsed return types, so it is transparent on the c++ side. That would place hard constraints on the lua side though.

Kind regards,
Michael

Am 03.08.2014 21:09, schrieb 1 1:

When programming on C++, you can do the following:

void byReference(int &y)
{
    y = 5;
}

int main()
{
   int x = 2;      // x = 2
   byReference(x); // x = 5
}

How to do the same using luabind?

Luabind docs says:

If you want to pass a parameter as a reference, you have to wrap it with the Boost.Ref.

Like this:

int ret = call_function(L, "fun", boost::ref(val));

But when I'm trying to do this:

#include <iostream>
#include <conio.h>

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

#include <luabind\luabind.hpp>

using namespace std;
using namespace luabind;

int main() 
{
  lua_State *myLuaState = luaL_newstate();
  open(myLuaState);

  int x = 2;


  do
  {
    luaL_dofile(myLuaState, "script.lua");

    cout<<"x before = "<< x <<endl;


    try 
    {
        call_function<void>(myLuaState, "test", boost::ref(x));
    }
    catch(const std::exception &TheError) 
    {
        cerr << TheError.what() << endl;

    }

    cout<<"x after = "<< x <<endl;


   } while(_getch() != 27);


  lua_close(myLuaState);
}

script.lua:

function test(x)
    x = 7
end

It gives me a "Trying to use unregistered class" error.

So, how to pass value from C++ to lua function by reference, so I can change it inside the script and it will be changed in c++ program too? I'm using boost 1.55.0, lua 5.1, luabind 0.9.1

Thanks.

P.S:
I posted the same question on StackOverflow, but seems like nobody knows what to do.


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds


_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user