放置代码的位置
The world logic has a number of functions that are used to code logic of the application. Initialization function of the world logic (init()) is used to create objects and initialize all other necessary resources on the world load. Shutdown function (shutdown()) is called when the world is unloaded and is used to delete resources that were created during the logic execution to avoid memory leaks. 世界逻辑具有许多用于对应用程序的逻辑进行编码的功能。世界逻辑的初始化函数(init())用于创建对象并初始化世界负载上的所有其他必要资源。 关闭函数(shutdown())在卸载世界时调用,该函数用于删除逻辑执行过程中创建的资源以避免内存泄漏。
But what about the frame-by-frame update? In the world logic, this update is performed within the main loop that has the following blocks for that:但是逐帧更新又如何呢?在世界逻辑中,此更新是在主循环中执行的,该循环具有以下块:
- updateAsyncThread() can contain logic functions to be called every frame independently of the rendering thread: pathfinding, generation of procedural textures, custom physics, etc. updateAsyncThread()可以包含独立于渲染线程而在每一帧被调用的逻辑功能:寻路,过程纹理的生成,自定义物理等。
- updateSyncThread() can contain any parallel logic functions to be executed before update(). updateSyncThread()可以包含要在update()之前执行的任何并行逻辑功能。
- update() can contain any logic: control what to render on the screen and how to do it, render to textures, create nodes, call console commands, etc. update()可以包含任何逻辑:控制在屏幕上呈现的内容以及如何执行,呈现为纹理,创建节点,调用控制台命令等。
- updatePhysics() can be used to simulate physics: perform continuous operations (pushing a car forward depending on current motor's RPM, simulating a wind blowing constantly, perform immediate collision response, etc.). updatePhysics()可用于模拟物理:执行连续操作(根据当前电动机的RPM向前推动汽车,不断模拟风吹,执行即时碰撞响应等)。
- postUpdate() can be used to correct behavior according to the updated node states in the same frame. postUpdate()可用于根据同一帧中更新的节点状态来纠正行为。
- swap() can be used to process the results of the updateAsyncThread() function. swap()可用于处理updateAsyncThread()函数的结果。
Code UpdateAsyncThread()Code UpdateAsyncThread()#
In the world logic updateAsyncThread(), you can specify all logic functions you want to be called every frame independently of the rendering thread. This function can be used to perform some heavy resource-consuming calculations performed during the whole frame, such as pathfinding, generation of procedural textures, custom physics, and so on. This function does not block the main thread.在世界逻辑updateAsyncThread()中,可以独立于渲染线程指定要在每一帧调用的所有逻辑函数。此功能可用于执行在整个框架中执行的大量耗资源的计算,例如寻路,过程纹理的生成,自定义物理等。此函数不会阻塞主线程。
Code UpdateSyncThread()Code UpdateSyncThread()#
In the world logic updateSyncThread(), you can specify any parallel logic functions you want to be executed before update(). This function blocks the Main Thread until all calls are completed.在世界逻辑updateSyncThread()中,可以指定要在update()之前执行的任何并行逻辑函数。此功能阻塞主线程,直到完成所有调用。
This function can be used to perform some heavy resource-consuming calculations such as pathfinding, generation of procedural textures, manipulations with big amounts of data, the result of which is required before the frame starts to be rendered. This function can also be used for simpler operations, that can be implemented in the update() function, to distribute between threads and improve performance.此功能可用于执行一些消耗大量资源的计算,例如寻路,过程纹理的生成,对大量数据的操作,在开始渲染帧之前需要这些结果。此函数还可以用于更简单的操作(可以在update()函数中实现),以在线程之间分配并提高性能。
Code Update()Code Update()#
In the world logic update(), you can put all functions to be called every frame, while your application is executed. It serves for implementing any logic. In brief, you can control a lot of things (whether graphics-related or not) from within the update(). Here you can:在世界逻辑update()中,可以在执行应用程序时在每一帧调用所有函数。它用于实现任何逻辑。简而言之,您可以在update()中控制很多事情(无论是否与图形相关)。在这里您可以:
- Create and delete nodes.创建和删除节点。
- Move nodes around the scene and rotate them. 移动场景中的节点并旋转。
- Change any parameters of nodes.更改节点的任何参数。
- Simulate particle systems. 模擬 粒子系統.
- Control skinned animation.控制带有外观的动画。
- Set global rendering settings.设置全局渲染设置。
- Create and manipulate your GUI.创建和操作您的 GUI 。
- Render to textures. 渲染到紋理.
- Execute console commands. 執行控制台命令.
- Even do some physics — perform some momentary actions: add impulses, simulate a hit or a push, set linear or angular velocity, add or remove shapes and joints, and change their parameters.甚至做一些物理学工作-执行一些瞬时动作:添加脉冲,模拟撞击或推动,设置 linear 或角速度,添加或删除形状和关节,并更改其参数。
World Callbacks世界回调#
Callbacks set in update() (for example, by using WorldTrigger or widget callbacks) are not executed immediately. They are run when the next engine function is called: that is, before updatePhysics(), if any (in the current frame), or before the next update() (in the following frame) — whatever comes first.在update()中设置的回调(例如,通过使用WorldTrigger或 widget回调)不会立即执行。它们将在调用下一个引擎函数时运行:即在updatePhysics()之前(如果有)(在当前帧中)或在下一个update()(在下一帧)之前-以先到为准。
Code UpdatePhysics()Code UpdatePhysics()#
The updatePhysics() function of the world logic is mainly used to control physics in your application. As a rule this function is used for continuous operations such as pushing a car forward depending on current motor's RPM, simulating a wind blowing constantly, performing immediate collision response (as updatePhysics() can be executed several times during a single rendering frame, you can process multiple bounces of objects, when they collide with each other and monitor them, while the update() will only show us the final result), etc. The updatePhysics() is executed in the Main thread, so you can perform any actions here just like in the update():世界逻辑的updatePhysics()函数用于控制应用程序中的物理。此功能应用于连续操作,例如根据当前电动机的RPM推动汽车前进,不断模拟风向,执行即时碰撞响应(由于updatePhysics()可以在单个渲染帧中执行多次,因此您可以处理多次反弹对象,当它们相互碰撞并监视它们时,而update()只会向我们显示最终结果),等等。因此,在updatePhysics()中,您可以:
- Operate on all physical bodies and shapes.在所有物理实体和形状上运行。
- Apply forces, impulses, and torques to rigid bodies.将力,脉冲和扭矩施加到刚体。
- Create, attach, or break joints, as well as modify their parameters.创建,附加或断开关节,以及修改其参数。
- Create and manipulate shapes and modify their parameters.创建和操作形状并修改其参数。
- Create and adjust physicals.创建和调整物理。
- Reposition and transform nodes if they are enabled.如果节点已启用,则重新定位和变换。
- Create new nodes.创建新节点。
- Delete nodes. 刪除節點。
Physics Callbacks物理回调#
Just like in case with update(), if you set any physics-based callbacks in updatePhysics() or use a PhysicalTrigger, they cannot be executed immediately, as the rendering process is already in action and they can violate it. If there is one more physics iteration to go, these callbacks or a PhysicalTrigger are executed before the next updatePhysics(); if not, before the next world logic update().就像在使用update()的情况下一样,如果在updatePhysics()中设置任何基于物理的回调或使用PhysicalTrigger,它们将无法立即执行,因为渲染过程已经在进行中,并且可能会违反它。如果还有一个物理迭代要进行,则这些回调或PhysicalTrigger将在下一个updatePhysics()之前执行;如果不是,则在下一个世界逻辑update()之前。
You can safely reposition or transform, create or delete nodes in your physics callback functions, as they are executed in the Main thread.如果要重新定位或变换,创建或删除物理回调返回的节点,则工作流程如下:将它们存储在数组中,然后在update()中执行所有必要的操作。
Code PostUpdate()Code PostUpdate()#
The postUpdate() function of the world logic is an additional function used to correct behavior after the state of the node has been updated (for example, skinned animation has been played in the current frame or particle system has spawn its particles).世界逻辑的postUpdate()函数是一个附加函数,用于在更新节点状态后纠正行为(例如,在当前帧中播放了蒙皮动画或粒子系统生成了其粒子)。
Imagine a situation when we need to attach an object (let's say, a sword) to the hand of a skinned mesh character. If we get transformation of the character hand bone and set it to the sword in the update() function, the attachment will be loose rather than precise. The sword will not be tightly held in the hand, because the animation is actually played right after the world logic update() has been executed. This means, returned bone transformations in the update() will be for the previous frame. The world logic postUpdate() is executed after animation is played, which means you can get updated bone transformations for the current frame from within this function and set them to the sword.想象一下一种情况,当我们需要将一个对象(例如,一把剑)连接到蒙皮的网格角色的手上时。如果我们得到角色手骨的变形并将其设置为update()函数中的剑,则附件将变得松散而不是精确。剑不会紧紧握在手中,因为动画实际上是在执行世界逻辑update()之后立即播放的。这意味着,update()中返回的骨骼变换将用于前一帧。世界逻辑postUpdate()是在播放动画之后执行的,这意味着您可以从此函数中获取当前帧的更新骨骼转换并将其设置为剑。
Code Swap()Code Swap()#
Engine calls this function after the following processes are completed: rendering (CPU portion), physics calculations and pathfinding, GUI rendering, and all Async threads. The function is designed to process the results of the updateAsyncThread() method — all other methods (threads) have already been performed and are idle. After this function, only two actions occur:引擎在完成以下过程之后调用此函数:渲染(CPU部分),物理计算和寻路,GUI渲染以及所有异步线程。该函数旨在处理updateAsyncThread()方法的结果-所有其他方法(线程)已经执行并且处于空闲状态。此功能之后,仅发生两个动作:
- All objects that are queued for deletion are deleted.所有排队等待删除的对象都将被删除。
- Profiler is updated. 探查器已更新。