4

I understand with Luabind that I can expose classes and then instances of those classes can be created in lua.

module[L_state]
   [
      class_<Player>("Player")
      .def(constructor<>())
      .def("Update",&Player::Update)
   ];

test.lua
player = Player()
player:Update()

But what if I wanted to create that player instance in C++ because I want to call it's members in C++, but I also want to expose that same instance of player to Lua so it can still call it's functions like:

player:Update()
user441521
  • 6,942
  • 23
  • 88
  • 160
  • You can call the lua functions from C++ to create and access the instance if you want to keep things done this way. – Etan Reisner Apr 15 '15 at 21:05

1 Answers1

2

You can push the value onto the Lua stack via luabind:

Player p;
luabind::globals(L)["player"] = p;

a runnable example: travis-ci, source.

P.S. beware of object lifetime and ownership issues. The LuaBridge manual can be of help to plan a strategy of shared object lifetime management. + an updated LuaBind manual for the LuaBind lifetime policies.

Dmitry Ledentsov
  • 3,620
  • 18
  • 28
  • Exactly what I was looking for thanks! For my process the object will live for the entire application so not an issue. – user441521 Apr 16 '15 at 15:12