Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Lua and C++  (Read 9139 times)

0 Members and 1 Guest are viewing this topic.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Lua and C++
« on: January 29, 2011, 03:02:38 am »
Hello everyone, i should be a little off-topic with this, but any help would be apreciated!

I need to implement Lua scripting support, and i would like to know what is the best library which can help me expose C++ classes/functions easily.

Because i 've heard the regular Lua package is not directly suited for C++.

Thanks

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Lua and C++
« Reply #1 on: January 29, 2011, 06:18:32 am »
I haven't done this myself but it is something I've been looking into.

LuaBind seems to be the way to go.
Quote
...export entire C++ classes.

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Lua and C++
« Reply #2 on: January 29, 2011, 06:24:38 am »
Be aware that luabind is licensed under the MIT License:
Quote
The MIT License

Copyright (c) 2010 Rasterbar Software

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Red

  • Newbie
  • *
  • Posts: 9
    • View Profile
Lua and C++
« Reply #3 on: January 29, 2011, 11:40:29 am »
Hi!

Lua binding is something I just done recently in my code, I too searched for the best way to easily make a connection between Lua and C++.
I heard of LuaBind like Walker, but its syntax did not suit me well when using it. What I would recommend you is to use Lunar, which is an updated version of the Luna binding, for the last version of Lua, 5.1.
Lunar is really easy to use and it really lets you keep your Object oriented programming style when binding. One cool thing is that it also allow you to choose if a C++ class used in Lua code as an instance must be destructed automatically by the Lua GarbageCollector or if you (the C++ programmer), are already taking care of the destruction of this instance. This is a really useful feature. Lunar is just a template class in a unique .hpp file, so really easy to integrate too.

Here is the link with some samples :

http://lua-users.org/wiki/CppBindingWithLunar

To improve Lunar/Lua ease of use in my code I just created two more classes, one is "LuaValue" which is an object who can take any Lua value (boolean, number, string or table) in it to store a Lua parameter(s) or return value(s) really easily for the programmer to use them in the c++ code. And I did another class (named "Lua"), as a Singleton, which store the lua_State* and offer some functions too, for example :
- Loading Lua scripts
- Calling Lua functions with parameters (and return values) from C++
- Registering C++ Function to be called later in a Lua script
- Retrieving table elements stored in  a Lua table, from the C++ code (Lua Table can be considered as "objects", so its like retrieving a Lua object attribute from C++)
- Retrieving global variables
- etc...

If you want some advice or code or help with all that, I will be glad to do so :)!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Lua and C++
« Reply #4 on: January 29, 2011, 12:55:31 pm »
LuaBind is definitely the best -- if you don't mind the (possibly) huge compile times and the boost dependency. It's a clone of boost.python adapted to Lua, actually.

As far as I know, it's the only library that is able to directly translate native C++ properties/functions/objects to Lua, you don't have to manipulate the Lua C API at all. With Lunar and other wrappers, you always have to write an intermediate layer between your C++ API and the Lua binding, with ugly and error-prone code (extracting parameters, converting values, putting them on the Lua stack, etc.).

With LuaBind you just say "I want to bind this thing with that name" and that's it, it works.
Laurent Gomila - SFML developer

pierreyoda

  • Full Member
  • ***
  • Posts: 186
    • View Profile
    • http://pierreyoda.pagesperso-orange.fr/
Lua and C++
« Reply #5 on: January 29, 2011, 03:09:40 pm »
Yeah, luabind is definitively the best choice.
You can even inherit C++ classes in your Luabind code, it's wonderful.
Projects:
- Open Rodent's Revenge (rewriting in progress)
- Open Advanced War (paused)

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Lua and C++
« Reply #6 on: January 29, 2011, 03:21:12 pm »
I use luabind for my own project which relies on Lua a huge lot. The only thing I need to do though is to share a single huge LuaInterface C++ class, and make some calls to Lua functions.

luabind was a bit tricky to compile and the tutorials are not very helpful, but once you're done it's really easy. I just have a single file where I decide what I share so it's not that much in itself, just add a single line every time I add a method. Calls to lua functions were also very easy.

I was really especially surprised how easy it was to transform a Lua custom vector class (using some metatables to implement OOP in Lua) into a sf::Vector2f.

If you want to see how it's done, you can check my sources on my project wiki, pretty much everything happens in "L_state.cpp", so you can see if it suits your taste :)

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Lua and C++
« Reply #7 on: January 29, 2011, 03:46:54 pm »
There is a good alternative to Luabind called MLuabind. It uses Loki library instead of Boost. Though it is rather dated and currently not maintained it works fine. Using MLuabind compile time is shorter than with Luabind & Boost.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Lua and C++
« Reply #8 on: January 29, 2011, 03:49:08 pm »
Hey! thanks for the tips! LuaBind sounds really great, im only wondering if boost will have any negative impact at all in my engine and games, you know, because sometimes too much middleware starts being annoying
:)


Still, i think i will try to implement that : )

CJ_COIMBRA

  • Full Member
  • ***
  • Posts: 112
    • ICQ Messenger - 13077864
    • View Profile
    • http://www.cjcoimbra.com
    • Email
Lua and C++
« Reply #9 on: February 02, 2011, 02:20:13 am »
Also in Game Coding Complete 3rd edition book they show how to use C++ with Lua. By the way I think it is the best book of all.
CJ

 

anything