高效调用Python函数

发布于 2024-11-26 11:17:07 字数 538 浏览 0 评论 0原文

我正在将 Python 嵌入到我的游戏中。这些脚本将用于定义角色 AI、实体如何对游戏事件做出反应等——这意味着游戏中的每种类型的实体都会有一个脚本。

每个脚本都会有一个类似于 createEntity() 的函数或返回构造实体的函数。调用这些函数的一种有效的方式是什么(请记住,每个实体的脚本中都有一个)。

我最初的想法是做如下所示的事情,但是,我不确定这有多有效。例如,运行该字符串后导入的英雄模块会发生什么?它是否仍然加载在主模块中?如果是这种情况,那就有问题了,因为我将为我可能需要添加到游戏世界的所有实体导入大量脚本。

boost::python::handle<> result(
    PyRun_String("import hero\n"    "createEntity()\n",
        Py_file_input, main_namespace.ptr(), main_namespace.ptr())
);

// Then extract the entity from `result`...

您有什么建议?

I'm embedding Python into my game. The scripts will be used to define the character AI, how entities react to game events, etc — this means there's going to be a script for every type of entity in the game.

Each script will have a function like createEntity() or something which will return the constructed entity. What would be an efficient(ish) way of calling these functions (remember, there's one in every entity's script).

My initial thought was to do something like what you see below, however, I'm unsure as to how efficient this is. For example, what happens with the imported hero module after I run that string? Does it remain loaded in the main module? If that's the case, that's problematic since I'm going to be importing lots of scripts for all the entities I might need to add to the game world.

boost::python::handle<> result(
    PyRun_String("import hero\n"    "createEntity()\n",
        Py_file_input, main_namespace.ptr(), main_namespace.ptr())
);

// Then extract the entity from `result`...

What suggestions do you have?

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

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

发布评论

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

评论(2

浪荡不羁 2024-12-03 11:17:07

您的问题没有具体说明空间效率(即内存)、时间效率还是劳动效率对您来说最重要。仅仅因为您正在考虑混合 C++/Python 应用程序,我就认为劳动效率是一个重要因素。因为您正在开发一款游戏,所以我假设其中的某些部分需要极其严格的执行速度。

也许这种方法会取得平衡。使所有用户交互(输入和输出,包括任何网络)基于 C++,以实现最小延迟。您可以将其放置在其自己的线程或进程中。给定模型中的高级事件(例如角色移动),此代码会非常快速地更新屏幕和网络。给定一个用户事件或来自网络的事件,它会向模型发送一条消息。

为了方便起见,可以与视图/控件异步的游戏模型将采用 Python 语言,因此您可以利用函数式编程等。您可以在两者之间使用共享内存或类似的 IPC 机制并分别启动它们如果实际上嵌入解释器很不方便。

虽然某些 AI 应用程序是 CPU 密集型的,因此可能很容易返回到 C 或 C++,但我建议将其作为最后一步,以响应解释代码中的明确响应性问题(如果您在全部。您甚至可能也想在图形方面遵循这一思路,因为现在大多数图形处理都委托给视频硬件,如果您有办法从 Python 代码进行所需的库调用。

虽然我不是游戏开发人员,但我见过一些情况,其中单个等效 C 和 Python 操作之间(现在)微秒的差异是用户可以察觉的。可察觉的性能问题几乎总是由其他因素造成,例如磁盘 I/O、网络延迟或低效的算法实现。

Your question does not specify whether space efficiency (i.e. memory), time efficiency, or labor efficiency is most important to you. Merely because you are considering a hybrid C++ / Python application, I assume that labor efficiency is a significant factor. Because you are developing a game, I assume that there will be some part of it that has a need for extremely tight execution speed.

Perhaps this approach will strike a balance. Make all the user interaction (input and output, including any networking) C++ based for minimum latency. You might place this in its own thread or process. Given a high level event from the model, like a character moving, this code very quickly updates the screen and the network. Given a user event, or an event from the network, it sends a message to the model.

The game model, which can be asynchronous to the view/control, would then be in Python for your convenience and so you can take advantage of functional programming etc. You could use shared memory or a similar IPC mechanism between the two and start them separately if actually embedding an interpreter is inconvenient.

While certain AI applications are CPU-intensive, and therefore it may be tempting to go back to C or C++ for them, I would advise doing that as a final step, in response to clear responsiveness issues in interpreted code, if you do it at all. You may even want to follow this line of thought with the graphics also, since nowadays most graphics processing is delegated to the video hardware, if you have a way to make the library calls you need from Python code.

Though I am not a game developer, I have been around and I have seen few situations in which the (nowadays) microseconds difference between single equivalent C and Python operations is perceptible to users. Perceptible performance problems are nearly always due to other factors, such as disk I/O, network latency or inefficient algorithm implementations.

故人的歌 2024-12-03 11:17:07

关于脚本语言的作用的 Ousterhout 是一个老东西,但很不错。对于您所说的游戏玩法要编写脚本的情况,模型(Python)已经按照您所描述的方式组织流程。如果 PyGame 或类似的库无法胜任呈现视图的任务,请查找或构建一个可以胜任的 Python 模块。

换句话说,当我建议您向后进行集成时,听起来您是在要求视图重复调用模型以更新自身。我想不出有什么办法可以本末倒置来提高开发效率或简化开发过程。

Ousterhout on the role of scripting languages is an oldie but goodie. In the case of a game where you have said that the gameplay is to be scripted, the model (Python) is already organizing the flow as you have described it. If PyGame or similar library isn't up to the task of presenting the view, find or build a Python module that can.

Put another way, when I suggested you've got the integration backwards, it sounds like you are asking the view to call the model repeatedly to update itself. I cannot think of a way that putting the metaphoric cart before the horse will yield either efficiency or ease of development.

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