使用 C++并一起编写脚本

发布于 2024-09-29 02:54:08 字数 277 浏览 6 评论 0原文

我正在开发简单的游戏作为一种爱好。对于我的新项目,我希望某些部分可以编写脚本。我熟悉Python,但不介意学习新语言。问题是:

我计划用 C++ 实现寻路、视野、碰撞检测等,但想使用 AI 状态机、脚本事件的脚本。这种工作使用什么类型的结构? 我想我可以让一个 C++ 程序运行一个 python 进程,而该进程又调用 C++ 方法,但这似乎效率很低。另一个想法是开发一个从 python 调用的库,这听起来也不是很有吸引力。这样做的常规方法是什么(除了编写我自己的语言和解析器?)我听说 lua 很流行嵌入到 C 程序中。

I am developing simple games as a hobby. For my new project, I want some parts to be scriptable. I am familiar with python but don't mind learning new languages. Here is the question:

I am planning to implement path-finding, field-of-vision, collision detection etc. in C++ but want to use scripts for AI state machines, scripted events. What type of structure is used for this kind of job?
I imagine I can make a C++ program run a python process which in turn calls C++ methods, but it seems inefficient. Another idea is to develop a library to be called from python, which doesn't sound very attractive either. What is the regular way of doing this(except for writing my own language and parser?) I heard lua is popular for embedding into C programs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眉黛浅 2024-10-06 02:54:08

从我个人的经验来看, luatcl 拥有出色的用于嵌入的 C API。两种语言都非常简单。如果您正在编写命令接口,我可能会建议使用 tcl,但如果您只是使用嵌入式解释器,我会推荐 lua。鉴于您使用的是 C++,您可能还想查看 luabind API,我听说过一些关于它的好消息。

对于 AI 脚本或其他状态机类型的东西,Zed Shaw 的这篇博文是一个很好的选择读。基于协程的人工智能代码对于复杂的脚本来说看起来更好,而不是管理大量的状态及其转换。

如果您使用 python,您可能最好扩展而不是嵌入

From my personal experience, both lua and tcl have fantastic C APIs for embedding. Both languages are very simple. If you're writing a command interface, I'd probably say go with tcl, but if you're just using an embedded interpreter, I'd recommend lua. Given that you're using C++, you might also want to look into the luabind API, I have heard good things about it.

For AI scripting, or other state machine-type things, this blog post by Zed Shaw is a good read. Coroutine-based AI code can look a lot nicer for complex scripts, rather than managing an enormous pile of states and their transitions.

If you're using python, you might be better off extending rather than embedding.

(り薆情海 2024-10-06 02:54:08

运行Python进程?不……

嵌入

Run a Python process? Nooo....

Embed.

情魔剑神 2024-10-06 02:54:08

根据个人经验,我强烈推荐 Google 的 Javascript 引擎 V8。它的性能非常好,用 C++ 编写,非常容易嵌入,没有其他依赖项,并且有一个非常漂亮的本机接口。

http://code.google.com/p/v8/

From personal experience I can highly recommend Google's Javascript engine V8. It's very performant, written in C++, is trivially easy to embed, has no other dependencies and a really beautiful native interface.

http://code.google.com/p/v8/

感受沵的脚步 2024-10-06 02:54:08

我同意 Tcl 和 Lua 是最容易嵌入到 ac/c++ 应用程序中的语言之一。主要是因为这从一开始就是一个设计决定。另一种专为嵌入而设计的语言是 Guile。

如果您有兴趣嵌入C++ 应用程序中的脚本语言请查看 Swig。 Swig 可以自动为多种脚本语言创建粘合代码,包括您已经了解的 python。主要优点是它可以处理许多不同的脚本语言。

I agree that Tcl and Lua are one of the easiest to embed in a c/c++ application. Mainly because that was a design decision from the very start. Another language that was designed for embedding is Guile.

If you are interested in embedding a scripting language in your c++ application take a look at Swig. Swig can automatically create the glue code for a number of scripting languages including python which you already know. The main advantage is that it handles many different scripting languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文