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
Objects-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.

Application Logic System

In UNIGINE, you can implement application logic in the following ways:

  • in pure UnigineScript
  • in C++ or C#
  • combining UnigineScript with C++ or C# — in this case, the execution sequence for each implemented method, corresponding to a certain stage of the application's life cycle (i.e., init(), update(), render(), etc.), is the following:
    1. UnigineScript function
    2. C++/C# method

The application logic has three basic components that have different lifetimes:

  • World logic. It takes effect only when the world is loaded. For projects that use UnigineScript only, the logic is written to the world script. This script is created as a *.usc file named after your project at the project creation. The file is stored in the data/ folder.
    For applications that use C++ AppWorldLogic.cpp is created, and for C# applications — AppWorldLogic.cs. This file is stored in the data/source/ folder and stays loaded during the whole engine runtime. It has implemented methods to put your logic code inside.
  • System logic. It exists during the application life cycle. The logic is implemented in the default system script named unigine.usc and stored in the data/ folder of your project as the core.ung archive. You can put your logic in this file if you use UnigineScript only. You can also inherit SystemLogic class and implement your logic in C++ or C#.
  • Editor logic. It takes effect only when UnigineEditor is loaded. Create a UnigineScript file and add it using the -editor_script path_to_file.usc command-line argument. You can also inherit EditorLogic class and implement your logic in C++ or C#.
    Notice
    When you create a new C++ / C# project, it has already inherited world logic class (AppWorldLogic), system logic class (AppSystemLogic), and editor logic class (AppEditorLogic) with implemented methods to put your logic code inside.

If a project uses UnigineScript only, its execution sequence is strictly defined in scripts and cannot be modified.
If a project is created using C++ or C#, a programmer can control the execution sequence by manually calling Engine's init(), update(), render(), swap(), and shutdown() methods when required and add logic anywhere between these blocks, as shown in the C++ sample <UnigineSDK>/source/samples/Api/Render/Screenshot/Screenshot.cpp.

World Logic#

If the world logic is implemented in UnigineScript, it is loaded or unloaded at the same time as the world itself is loaded or unloaded. The world logic implemented in C++/C# stays loaded during the whole Unigine engine runtime. The world logic may use the following methods, which are available for both UnigineScript and C++/C#:

Source code (C++)
#include "AppWorldLogic.h"

AppWorldLogic::AppWorldLogic() {

}

AppWorldLogic::~AppWorldLogic() {

}

int AppWorldLogic::init() {

	return 1;
}

int AppWorldLogic::update() {

	return 1;
}

int AppWorldLogic::render() {

	return 1;
}

int AppWorldLogic::flush() {

	return 1;
}

int AppWorldLogic::shutdown() {

	return 1;
}

int AppWorldLogic::destroy() {

	return 1;
}

int AppWorldLogic::save(const Unigine::StreamPtr &stream) {
	UNIGINE_UNUSED(stream);
	return 1;
}

int AppWorldLogic::restore(const Unigine::StreamPtr &stream) {
	UNIGINE_UNUSED(stream);
	return 1;
}

init() function#

Engine calls this function on the world initialization. The code inside should initialize resources for your world scene during the world start.

update() function#

In the update() function, you can specify all logic-related functions you want to be called every frame while your application is executed. Engine calls this function before updating each render frame.

render() function#

This is an additional function used to correct behavior after the state of the node has been updated. Engine calls this function before rendering each render frame.

flush() function#

This function is used to control physics in your application. Engine calls this function before updating each physics frame.

shutdown() function#

The function is called when the world is unloaded, and is used to delete resources that were created during world script execution to avoid memory leaks. Engine calls this function on the world shutdown. Inside this method, you should delete all pointers to created objects.

destroy() function#

This function is used to destroy all created resources (such as dynamic textures) to avoid crashes due to invalid pointers. Engine calls this function when the video mode is changed or application is restarted (i.e. video_restart is called).

save() and restore() functions#

The save() function saves custom user data to a file. Engine calls this function when the world is saving its state (i.e. state_save is called). To restore data from a file, you need to use the restore() function. Engine calls this function when the world is restoring its state (state_restore is called).

Notice
The basic recommended workflow is to have a single AppWorldLogic class to process all worlds in your project. However, you can split world-specific code between separate classes inherited from WorldLogic.

System Logic#

The system logic stays loaded during the whole Unigine engine runtime. It may use the following methods, which are available for both UnigineScript and C++/C#:

Source code (C++)
#include "AppSystemLogic.h"

AppSystemLogic::AppSystemLogic() {

}

AppSystemLogic::~AppSystemLogic() {

}

int AppSystemLogic::init() {
	return 1;
}

int AppSystemLogic::update() {
	return 1;
}

int AppSystemLogic::render() {
	return 1;
}

int AppSystemLogic::shutdown() {
	return 1;
}

int AppSystemLogic::destroy() {
	return 1;
}

init() function#

Engine calls this function on initialization. Here you should put the code that you want to be executed during the system script initialization.

update() function#

Engine calls this function before updating each render frame. Here you should put the code that you want to be updated each frame. The logic should be related to the engine job. These are the examples of operations that are performed by a default system script:

  • The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time, or not handled by the system.
  • Main menu logic is updated.
  • Other system-related user input is handled.

render() function#

Engine calls this function before rendering each render frame. It can access the updated data on node states and correct the behavior accordingly in the same frame.

shutdown() function#

Engine calls this function on application shutdown. You should release all the resources that you created for system script.

destroy() function#

Engine calls this function when the video mode is changed or application is restarted (i.e. video_restart is called). It is used to reinitialize the graphics context if application directly uses any OpenGL or DirectX commands).

Editor Logic#

The editor logic methods are called only if the editor is loaded. Otherwise, the engine ignores these methods.

The editor logic may use the following methods, which are available for both UnigineScript and C++/C#:

Source code (C++)
#include "AppEditorLogic.h"

AppEditorLogic::AppEditorLogic() {
	
}

AppEditorLogic::~AppEditorLogic() {
	
}

int AppEditorLogic::init() {
	return 1;
}

int AppEditorLogic::update() {
	return 1;
}

int AppEditorLogic::render() {
	return 1;
}

int AppEditorLogic::shutdown() {
	return 1;
}

int AppEditorLogic::destroy() {
	return 1;
}

int AppEditorLogic::worldInit() {
	return 1;
}

int AppEditorLogic::worldShutdown() {
	return 1;
}

int AppEditorLogic::worldSave() {
	return 1;
}

init() function#

The engine calls this function on the world initialization. It initializes all belongings of UnigineEditor.

update() function#

If the editor is loaded, the engine calls this function before updating each render frame. Here you can put some editor-related logic that should be updated each frame.

render() function#

If the editor is loaded, the engine calls this function before rendering each render frame.

shutdown() function#

The engine calls this function on editor shutdown. Engine applications may continue running.

destroy() function#

The engine calls this function when the video mode is changed or the application is restarted (i.e. video_restart is called). The function is used to reinitialize the graphics context (if the application directly uses any OpenGL or DirectX commands)

worldInit() function#

If the editor is loaded, the engine calls this function on the world initialization. Use this function, if you want to initialize something before the world logic starts its init() function.

worldShutdown() function#

If the editor is loaded, the engine calls this function on the world shutdown.

worldSave() function#

If the editor is loaded, the engine calls this function on the world save.

Last update: 2019-08-16
Build: ()