Ruby vs Lua as scripting language for C++

11,777

Solution 1

I've used Lua extensively in the past.

Luabind is really easy to use, there is no need for an external generator like SWIG, the doc is great. Compile times remain decent.

Biggest problem I've seen : lua is mostly ... write-only. You don't really have classes, but only associative arrays with a bit of syntaxic sugar ( object['key'] can be written object.key ), so you easily end up adding a 'member' in an obscure function, completely forget about it, and have side effects later.

For this reason, and this reason only, I'd prefer Python. Boost::Python is the basis for Luabind so both have a similar API (Luabind used to be slightly easier to build but not anymore). In terms of functionality, they are quite equivalent.

Not directly related : None of these can be reliably used in a multithreaded environment (so this depends on the complexity of your server).

  • N Python threads : the GIL ( Global Interpreter Lock ) is on your way. Each and every time you use a variable in a thread, it's locked, so it kinda ruins the point, except for long I/O operations and calls to C functions.
  • lua has coroutines, but they aren't parallelisable.
  • Ruby threads aren't really threads, but similar to Lua's coroutines

Note that you can still create one environement for each thread, but they won't be able to communicate (except with a C++ machinery). This is especially easy in Lua.

Solution 2

I've looked at embedding Ruby into C/C++ before, and it seemed extremely difficult. There are a lot of challenges you'll face:

  • Calling into Ruby from C/C++ requires 2 layers of functions to be written (one layer to call, and one to catch exceptions)
  • Calling back into C/C++ from Ruby requires the normal SWIG-type work
  • Moving data back and forth requires keeping careful track of allocations, since Ruby will want to garbage collect anything it can

I'm sure that this can be done, but it seemed extremely difficult to me, only doable if you can jump into Ruby in a minimum of entry points.

Solution 3

You may be interested in learning about Squirrel. I believe it was the scripting language used by Left 4 Dead 2. It is more advanced than lua (uses objects and classes) and is meant to easily be embedded in a C++ app, which sounds like exactly what you are looking for.

Solution 4

Go for lua, though i'd recommend luajit, not only for speed, but for the new ffi library, boosting intercommunication to the max :). Lua also has tones of modules, and new ones are very easy to create, this makes up for the lack in its stdlib.

Share:
11,777

Related videos on Youtube

bl00dshooter
Author by

bl00dshooter

Updated on March 17, 2020

Comments

  • bl00dshooter
    bl00dshooter about 4 years

    I am currently building a game server (not an engine), and I want it to be extendable, like a plugin system.
    The solution I found is to use a scripting language. So far, so good.

    I'm not sure if I should use Ruby or Lua. Lua is easier to embed, but Ruby has a larger library, and better syntax (in my opinion). The problem is, there is no easy way I found to use Ruby as scripting language with C++, whereas it's very easy with Lua.

    Toughs about this? Suggestions for using Ruby as scripting language (I tried SWIG, but it isn't nearly as neat as using Lua)?

    Thanks.

    • ICTMitchell
      ICTMitchell about 13 years
      If you think Lua is too simplistic, and Ruby too hard to embed, maybe you should consider Python?
    • DaEagle
      DaEagle about 13 years
      If you are exposing a code base (especially a server) do you really want a large library? You may spend more effort taking away dangerous library features that users can exploit when it comes to sandboxing.
  • bl00dshooter
    bl00dshooter about 13 years
    Thanks. Are there any books about embedding the Squirrel language in C++?
  • James
    James about 13 years
    None so far as I know - it's a fairly new scripting language. Don't let the '3.0' fool you, it's been around since 2004, but has not been used as much as some other scripting languages. The documentation (linked on main page) seems good.
  • The Communist Duck
    The Communist Duck about 13 years
    Why should the number of gamers matter?
  • jpjacobs
    jpjacobs about 13 years
    With Lua, the multi threading thing can be solved using LuaLanes.
  • dunedain289
    dunedain289 about 13 years
    @Calvin1602 The newest versions of Ruby (1.9+) use real system level threads. It does still have a GIL though, which puts it on equal footing with Python.
  • Said algharibi
    Said algharibi about 13 years
    @dunedain289 : Nice to know, thanks. @jpjacobs : I'm aware of LuaLanes, but I never tried it. Feels a bit "unnatural", but I should give it a go.
  • kikito
    kikito about 13 years
    Regarding Lua and classes, may I suggest to give a look at this a look? github.com/kikito/middleclass (disclaimer: I'm its creator)
  • Said algharibi
    Said algharibi about 13 years
    @egarcia : Nice ! But you can still to object.CompletelyNewMember = WeirdStuff. Have you considered adding a __ set __ metammethod to forbid this ?
  • kikito
    kikito about 13 years
    @Calvin1602: the way middleclass is built, being able to do object.foo = bar is a necessity. However, I have set up getters and setters (with default ugly syntax, not metamethods) here: github.com/kikito/middleclass-extras/blob/master/…