This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
Programming
Setting Up Development Environment
Usage Examples
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
CIGI Client Plugin
Rendering-Related Classes
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Where to Put Your Code: Update(), Flush() or Render()

The world script has a number of functions that are used to code logic of the application. Initialization function of the world script (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 script execution to avoid memory leaks.

But what about the frame-by-frame update? In the world script there are three blocks for that:

  • in the update() you can put any logic: control what to render on the screen and how to do it, render to textures, create nodes, call console commands, and a lot more
  • in the flush() you can 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.).
  • in the render() you can correct behavior according to the updated node states in the same frame

Code Update()#

In the world script update() you can put all functions to be called every frame while your application executes. 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:

Notice
Do not apply forces and torques to rigid bodies in the update(). Otherwise, you will get unstable result that varies with each rendering frame. All continuous operations must be within the flush().

World Callbacks#

Callbacks set in the update() (for example, by using WorldTrigger or widget callbacks) are not executed immediately. They will be run only when the next engine function is called: that is, before flush(), if any (in the current frame), or before the next update() (in the following frame) — whatever comes first.

Code Flush()#

The flush() function of the world script is used to control physics in your application. This function should be used for continuous operations such as pushing a car forward depending on current motor's RPM, simulating a wind blowing constantly, perform immediate collision response (as flush() 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. So, in the flush() you can:

Notice
There are certain limitations, for what can be done from within flush(). As the main loop accesses the list of nodes and their transformation data during the flush call, you can't do the following:
  • Make reposition and transform nodes if they are enabled.
  • Create new nodes.
  • Delete nodes.
These operations can violate the rendering process already in progress.

Physics Callbacks#

Just like in case with update(), if you set any physics-based callbacks in the flush() or use a PhysicalTrigger, they cannot be executed immediately, as rendering process is already in action and they can violate it. If there is one more physics iteration to go, they are executed before the next flush(); if not, before the next world script update().

If you want to reposition or transform, create or delete nodes that are returned by your physics callback, the workflow is as follows: store them in the array and then perform all necessary operations in the update().

Code Render()#

The render() function of the world script 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).

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 hold in the hand, because the animation is actually played right after the world script update() has been executed. This means, returned bone transformations in the update() will be for the previous frame. World script render() is executed after animation is played, which means from within this function you can get updated bone transformations for the current frame and set them to the sword.

Notice
Use render() for correction purposes only; otherwise, this will increase the main loop time. All other functions should be placed within the update() or flush() (physics).
Last update: 2018-12-27
Build: ()