简体   繁体   中英

Ruby vs Lua as scripting language for C++

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.

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.

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.

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.

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.

One thing Lua has going for it is its ability to shuttle data between C++ (or C) and itself very easily. Essentially you're just pushing/popping data onto a stack in order to communicate between the two. Having multiple Lua environments up and running at the same time is quite simple as well (should you need that functionality). Although Lua is a garbage collected language, it's easy to prevent it from doing so on data that needs to stick around in your C++ code. Creating an extensible plugin system should be easy with Lua once you lay the groundwork. Swapping plugins (in this case, scripts) in and out at runtime is also pretty trivial (although this may be true for Ruby as well, I'm not familiar enough with it to know).

One thing to think about is how much object-oriented stuff you want your scripts to be able to handle. Lua uses functions, tables, metatables, and prototypes to implement OO-like programming. Some people like it, some don't; personally I found it interesting to use, if a bit clunky at times. Not having used Ruby, I can't speak for it, but you may want to weigh your need for object/class support.

I think in your situation you should also consider how fast you want to get your project up and running. As you and others have noted, Ruby is hard to embed in C++, whereas Lua is not. Time is always precious and if you want to get something working ASAP, Lua is probably your best bet.

I would go with whatever was easiest to learn/has the most gamers using it. You want it to be as accessible to your customers as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM