如何在我的游戏中实施脚本?

发布于 2024-08-14 14:48:25 字数 663 浏览 8 评论 0原文

我正在尝试编写一个游戏并实现脚本,以便在以后的开发中,当我想要更改数字时,我不必重新编译所有内容。

我的问题是我不知道脚本应该如何与游戏交互。我使用的脚本语言是 angelscript

现在,我有一个状态:介绍状态,我将其用作游戏“引擎”中大多数模块的测试(它更像是松散的类集合)。它将加载并绘制图片并绘制文本,并使用脚本来更新自身,然后可能切换到虚拟状态以测试状态管理器。

在编写它时,我意识到使用脚本来完成大部分更新需要我 用脚本注册我的大部分游戏引擎模块,并将大部分代码移至脚本语言。就我个人而言,我宁愿让 C++ 部分完成大部分工作,并让脚本语言提供在公式/绘图/其他中使用的数字。

然而,如果我是对的,那么这样做将需要许多不同的更新模块来满足游戏中需要更新的大部分内容,并要求将它们全部加载进去,并且 C++ 代码将具有单独运行每个更新函数。

或者,有一种我忽略的实现脚本和程序互操作性的方法。无论哪种方式,有人可以帮助我找出将脚本实现到我的游戏中的最佳方法是什么吗?

I'm trying to write a game and implement scripting so that later on in development I won't have to recompile everything when I want to change numbers.

My problem is that I don't know how scripts should interface with the game. The scripting language I'm using is angelscript.

Right now, I have a state: the intro state, which I'm using as a test for most of the modules in my game "engine" (it's more like a loose collection of classes). It will load and draw a picture and draw text, and use scripting to update itself, and maybe switch to a dummy state afterwards to test the state manager.

While writing it, I realized that using the script to do most of the updating would require that I register most of my game engine's modules with the script, and pretty much move the bulk of the code to the scripting language. Personally, I'd rather have the C++ portion doing the majority of the work, and have the scripting language come up with the numbers to use in the formulas/drawing/whatever.

However, if I'm right, doing it that way would entail lots of different update modules for the majority of the things in the game that need to be updated, and requiring that they all be loaded in, and that the C++ code would have to run each update function individually.

Or, there's a way to achieve script and program interoperability that I'm overlooking. Either way, could someone help me figure out what the best way to get scripting implemented into my game is?

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

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

发布评论

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

评论(4

胡大本事 2024-08-21 14:48:25

这么大的问题确实没有正确答案。这样做的方式与在 C++ 中进行引擎/游戏逻辑分离的方式相同。定义一个脚本可以调用的 API,允许它执行您想要执行的任何操作。使用脚本注册该 API 中的函数,并在angelscript 中使用该 API。该 API 应该是什么完全取决于您的需求以及您想要给予脚本编写者什么样的权力。

There's no correct answer to such a large question really. You do it the same way you would do engine/game logic separation in C++. Define an API that the script can call that allows it whatever it is you want it to do. Register functions in that API with the script, and use the API in angelscript. What that API should be depends entirely on your needs and what kind of power you want to give the scripter.

难以启齿的温柔 2024-08-21 14:48:25

如果您希望 AngelCode(或您选择的任何其他脚本方法)只是“得出一些数字”,嘿,就这样使用它 - 例如,在 AngelCode 中,通过向脚本公开您的单个 C++ 函数来编译脚本,说“void ProvideNumberFor(string Reason, number value)”,脚本将负责根据需要多次调用该函数以“提供数字”,仅此而已。

If you want AngelCode (or any other scripting approach of your choice) to just "come up with some numbers", hey, use it that way -- e.g., in AngelCode, compile the scripts by exposing to them a single C++ function of yours, say "void ProvideNumberFor(string reason, number value)", and the scripts will be responsible for calling that function as many times as needed to "provide the numbers", and nothing more.

半世蒼涼 2024-08-21 14:48:25

如果您查看 Garry 的 mod 等真实示例或使用 UnrealScript 编写的游戏,您会发现现代游戏中的相当多逻辑都是在脚本中实现的。 C/C++ 代码最适合引擎的“静态”和容易出现瓶颈的部分,例如渲染器、物理引擎、低级网络等。脚本最适合内容(即游戏逻辑)。

旁白:恕我直言,最好的游戏脚本语言是 Lua。它提供了与 C/C++ 的轻松集成,文档非常齐全,并且 Javascript 用户会很熟悉。

我正是为了这个目的而使用了 Lua,而且它做得很好。看看所有使用 Lua 编程的游戏。而且,它的速度非常快。

编辑:我没有完全阅读问题......抱歉。这是我真正的答案;)

If you look at real examples like Garry's mod or games written with UnrealScript you'll find that quite a bit of logic in modern games is implemented in the scripts. C/C++ code is best for "static" and bottleneck-prone parts of the engine, like the renderer, physics engine, low-level networking, etc. Scripts are best for content (i.e., game logic).

Aside: The best game-scripting language IMHO is Lua. It provides easy intregration with C/C++, is very well-documented, and will be familiar to users of Javascript.

I've used Lua for exactly this purpose, and it does a great job. Look at all the games programmed using Lua. Also, it's blazingly fast.

EDIT: I didn't read the question fully...sorry. This is my real answer ;)

荒人说梦 2024-08-21 14:48:25

如果您熟悉 Qt4/Javscript,则可以随时使用 QtScript http://qt。 nokia.com/doc/4.5/qtscript.html

If you're familiar with Qt4/Javscript you can always use QtScript http://qt.nokia.com/doc/4.5/qtscript.html.

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