简体   繁体   English

Ruby vs Lua作为C ++的脚本语言

[英]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. 我不确定我是否应该使用Ruby或Lua。 Lua is easier to embed, but Ruby has a larger library, and better syntax (in my opinion). Lua更容易嵌入,但Ruby有一个更大的库,更好的语法(在我看来)。 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. 问题是,我没有找到使用Ruby作为C ++脚本语言的简单方法,而使用Lua则非常容易。

Toughs about this? 对此有些不满? Suggestions for using Ruby as scripting language (I tried SWIG, but it isn't nearly as neat as using Lua)? 使用Ruby作为脚本语言的建议(我试过SWIG,但它不像使用Lua那样整洁)?

Thanks. 谢谢。

I've looked at embedding Ruby into C/C++ before, and it seemed extremely difficult. 我之前看过将Ruby嵌入到C / C ++中,看起来非常困难。 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) 从C / C ++调用Ruby需要编写2层函数(一层要调用,一层用于捕获异常)
  • Calling back into C/C++ from Ruby requires the normal SWIG-type work 从Ruby调用C / C ++需要正常的SWIG类型工作
  • Moving data back and forth requires keeping careful track of allocations, since Ruby will want to garbage collect anything it can 来回移动数据需要仔细跟踪分配,因为Ruby会想要垃圾收集任何东西

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. 我确信这可以做到,但对我来说这似乎非常困难,只有你能在最低限度的入口点跳进Ruby才有用。

I've used Lua extensively in the past. 我过去曾经广泛使用过Lua。

Luabind is really easy to use, there is no need for an external generator like SWIG, the doc is great. Luabind非常容易使用,不需要像SWIG这样的外部生成器,doc很棒。 Compile times remain decent. 编译时间仍然不错。

Biggest problem I've seen : lua is mostly ... write-only. 我见过的最大问题:lua主要是......只写。 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. 你没有真正的类,但只有带有一点语法糖的关联数组(object ['key']可以写成object.key),所以你很容易在一个模糊的函数中添加一个'成员',完全忘记了关于它,并在以后有副作用。

For this reason, and this reason only, I'd prefer Python. 出于这个原因,仅此原因,我更喜欢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). Boost :: Python是Luabind的基础,因此两者都有类似的API(Luabind以前稍微容易构建但不再容易)。 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. N Python线程:GIL(全局解释器锁)即将推出。 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. 每次在线程中使用变量时,它都会被锁定,因此除了长时间的I / O操作和对C函数的调用之外,它有点破坏了这一点。
  • lua has coroutines, but they aren't parallelisable. lua有协同程序,但它们不可并行。
  • Ruby threads aren't really threads, but similar to Lua's coroutines Ruby线程不是真正的线程,但与Lua的协同程序类似

Note that you can still create one environement for each thread, but they won't be able to communicate (except with a C++ machinery). 请注意,您仍然可以为每个线程创建一个环境,但它们将无法通信(除了使用C ++机制)。 This is especially easy in Lua. 这在Lua中尤其容易。

You may be interested in learning about Squirrel . 您可能有兴趣了解Squirrel I believe it was the scripting language used by Left 4 Dead 2 . 我相信它是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. 它比lua(使用对象和类)更高级,并且可以轻松嵌入到C ++应用程序中,这听起来就像您正在寻找的那样。

Go for lua, though i'd recommend luajit, not only for speed, but for the new ffi library, boosting intercommunication to the max :). 转到lua,虽然我推荐luajit,不仅仅是为了速度,而是为了新的ffi库,增强了对最大的互通:)。 Lua also has tones of modules, and new ones are very easy to create, this makes up for the lack in its stdlib. Lua也有模块的音调,新的模块非常容易创建,这弥补了stdlib的不足。

One thing Lua has going for it is its ability to shuttle data between C++ (or C) and itself very easily. Lua要做的一件事就是它能够非常容易地在C ++(或C)和它自己之间传送数据。 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). 同时启动和运行多个Lua环境也非常简单(如果您需要该功能)。 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. 尽管Lua是一种垃圾收集语言,但很容易阻止它在需要在C ++代码中保留的数据上这样做。 Creating an extensible plugin system should be easy with Lua once you lay the groundwork. 一旦你奠定了基础,Lua就可以轻松创建一个可扩展的插件系统。 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). 在运行时交换插件(在本例中为脚本)也非常简单(虽然对于Ruby来说也是如此,我对它的了解还不够熟悉)。

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. Lua使用函数,表,元表和原型来实现类似OO的编程。 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. 没有使用Ruby,我不能说它,但你可能想要权衡你对对象/类支持的需求。

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. 正如你和其他人所指出的那样,Ruby很难嵌入到C ++中,而Lua则不然。 Time is always precious and if you want to get something working ASAP, Lua is probably your best bet. 时间永远是宝贵的,如果你想尽快得到一些工作,Lua可能是你最好的选择。

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. 您希望它尽可能地为您的客户所用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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