在c ++应用程序中实现脚本 [英] Implementing scripts in c++ app

查看:169
本文介绍了在c ++应用程序中实现脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的应用程序的各个部分转换成简单的脚本,允许没有强大的c ++知识的人能够编辑和实现各种功能。

I want to move various parts of my app into simple scripts, to allow people that do not have a strong knowledge of c++ to be able to edit and implement various features.

因为它是一个实时应用程序,我需要有一些这些脚本的多任务。理想情况下,我想让它使得c ++应用程序调用一个脚本函数,然后继续运行(在c ++线程下),直到暂停点(等待(x)),或返回。在等待的情况下,状态需要保存准备好脚本重新启动,下次应用程序在持续时间过期后循环。

Because it's a real time app, I need to have some kind of multitasking for these scripts. Ideally I want it so that the c++ app calls a script function which then continues running (under the c++ thread) until either a pause point (Wait(x)), or it returns. In the case of it waiting the state needs to be saved ready for the script to be restarted the next time the app loops after the duration has expired.

脚本也需要能够调用c ++类方法,理想情况下使用c ++类而不是c ++类周围的纯包装函数。

The scripts also need to be able to call c++ class methods, ideally using the c++ classes rather than plain wrapper functions around c++ classes.

我不想花大量的时间实现这一点,所以使用现有的脚本语言比写我自己更喜欢。我听说Python和Lua可以集成到一个c ++应用程序,但我不知道如何做到这一点,以实现我的目标。

I don't want to spend a massive amount of time implementing this, so using an existing scripting language is preferred to writing my own. I heard that Python and Lua can be integrated into a c++ app, but I do not know how to do this to achieve my goals.


  • 脚本必须能够调用c ++函数

  • 脚本必须能够在调用某些函数时暂停(例如等待),并且可以通过c ++线程再次重新启动

  • 需要快速 - 这是一个实时应用程序,可能会有很多脚本运行。

我可以很容易地滚动多任务代码,只要脚本可以保存和重新启动(可能通过不同的线程到原始)。

I can probably roll the multitasking code fairly easily, provided the scripts can be saved and restarted (possibly by a different thread to the original).

推荐答案

您可以使用Lua或Python。 Lua比python更轻量级。它的内存占用比python小,在我们的经验是更容易集成(人们的里程在这一点可能不同)。它可以支持一堆同时运行的脚本。 Lua至少支持以你想要的方式停止/启动线程。

You can use either Lua or Python. Lua is more "lightweight" than python. It's got a smaller memory footprint than python does and in our experience was easier to integrate (people's mileage on this point might vary). It can support a bunch of scripts running simultaneously. Lua, at least, supports stopping/starting threads in the manner you desire.

Boost.python很好,但在我的(有限的)体验中,我们很难为我们的不同环境编译,是相当重量级。它(在我看来)需要Boost的缺点。对于一些,这可能不是一个问题,但如果你不需要Boost(或不使用它),你正在引入大量的代码,使Boost.python工作。 YMMV。

Boost.python is nice, but in my (limited) experience, it was difficult for us to get compiling for our different environments and was pretty heavyweight. It has (in my opinion) the disadvantage of requiring Boost. For some, that might not be a problem, but if you don't need Boost (or are not using it), you are introducing a ton of code to get Boost.python working. YMMV.

我们已经在多个平台(win32,Xbox360和PS3)上的应用程序中构建了Lua。我相信它将工作在x64。使用Luabind的建议是好的。我们绕过在两者之间编写我们自己的接口,虽然不太复杂,有了这个胶水代码会节省你很多时间,或许加重。

We have built Lua into apps on multiple platforms (win32, Xbox360 and PS3). I believe that it will work on x64. The suggestion to use Luabind is good. We wound up writing our own interface between the two and while not too complicated, having that glue code will save you a lot of time and perhaps aggravation.

,调试可能会很痛苦。我们目前没有很好的解决方案来调试嵌入到我们的应用程序中的Lua脚本。因为我们没有在我们的应用程序中使用python,我不能说什么工具可能在那里,但几年前的景观是大致相同 - 调试差。具有脚本扩展功能是很好的,但是脚本中的错误可能会导致问题,可能很难找到。

With either solution though, debugging can be a pain. We currently have no good solution for debugging Lua scripts that are embedded into our app. Since we haven't used python in our apps I can't speak to what tools might be available there, but a couple of years ago the landscape was roughly the same -- poor debugging. Having scripting to extend functionality is nice, but bugs in the scripts can cause problems and might be difficult to locate.

Lua代码本身是一个杂乱的工作if你需要在那里做出改变。我们已经在Lua代码库本身看到了很难跟踪的错误。我怀疑Boost :: Python可能有类似的问题。

The Lua code itself is kind of messy to work with if you need to make changes there. We have seen bugs in the Lua codebase itself that were hard to track down. I suspect that Boost::Python might have similar problems.

对于任何脚本语言,它不一定是非程序员扩展功能的解决方案。它可能看起来像,但你可能会花费大量的时间调试脚本,甚至可能是Lua。

And with any scripting language, it's not necessarily a solution for "non-programmers" to extend functionality. It might seem like it, but you will likely wind up spending a fair amount of time either debugging scripts or even perhaps Lua.

这一切说,我们一直非常快乐与Lua和已经运到了两个游戏。我们目前没有计划离开语言。总而言之,我们发现它比其他几年前可用的替代品更好。 Python(和IronPython)是其他选择,但基于经验,他们似乎比Lua更加沉重。我很想听听那里的其他经验。

That all said, we've been very happy with Lua and have shipped it in two games. We currently have no plans to move away from the language. All in all, we've found it better than other alternatives that were available a couple of years ago. Python (and IronPython) are other choices, but based on experience, they seem more heavy handed than Lua. I'd love to hear about other experiences there though.

这篇关于在c ++应用程序中实现脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆