Logic System
Unigine has three classes for implementing system, world and editor logic by using C++ and C# APIs.
The logic of these classes is the same as for runtime UnigineScript scripts:
- WorldLogic class has the same logic as world script.
- SystemLogic class has the same logic as system script.
- EditorLogic class has the same logic as editor script.
These classes' methods are called automatically by engine after equivalent scripts' methods.
World Logic
WorldLogic class allows user to write application logic in C++ / C# in the same manner as in UnigineScript.
When you create a new C++/ C# project and open an AppWorldLogic class (inherited from WorldLogic class), you will see the following methods (C++ example):
#include "AppWorldLogic.h"
AppWorldLogic::AppWorldLogic() {
}
AppWorldLogic::~AppWorldLogic() {
}
int AppWorldLogic::init() {
return 1;
}
int AppWorldLogic::shutdown() {
return 1;
}
int AppWorldLogic::destroy() {
return 1;
}
int AppWorldLogic::update() {
return 1;
}
int AppWorldLogic::render() {
return 1;
}
int AppWorldLogic::flush() {
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;
}
Let's clarify each of this function inside the AppWorldLogic class.
init() function
This function has the same logic as world script's init() function. Engine calls this function on world initialization. So inside you should put the code that initialize resources for your world scene during the world start.
shutdown() function
Shutdown 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 world shutdown. Inside this method you should delete all pointers to created objects.
destroy() function
This function is used to reinitialize the graphics context. Engine calls this function when the video mode is changed or application is restarted (i.e. video_restart is called).
update() function
In the update() function you can specify all graphics-related functions you want to be called every frame while your application executes. This function has the same logic as world script's update() function. Engine calls this function before updating each render frame.
render() function
The render() function of the world script 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
Using flush() function of the world script you can control physics in your application. Besides the physics control, you can put here some calculations that are not related to rendering. Engine calls this function before updating each physics frame.
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 restore function. Engine calls this function when the world is restoring its state (i.e. state_restore is called).
System Logic
SystemLogic class allows user to write application logic in C++ / C# in the same manner as in UnigineScript. As well as system script, it stays loaded during the whole Unigine engine runtime. SystemLogic methods are called immediately after corresponding system script functions.
When you create a new C++ / C# project and open an AppSystemLogic class (inherited from SystemLogic class), you will see the following methods (C++ example):
#include "AppSystemLogic.h"
AppSystemLogic::AppSystemLogic() {
}
AppSystemLogic::~AppSystemLogic() {
}
int AppSystemLogic::init() {
return 1;
}
int AppSystemLogic::shutdown() {
return 1;
}
int AppSystemLogic::destroy() {
return 1;
}
int AppSystemLogic::update() {
return 1;
}
int AppSystemLogic::render() {
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.
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).
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 engine job. For example, some of operations that are performed by 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.
Editor Logic
EditorLogic class allows user to write application logic in C++ / C# in the same manner as in UnigineScript. All the EditorLogic methods are called only if the editor is loaded. Otherwise, the engine ignore these methods (as well as editor script's methods).
When you create a new C++ / C# project and open an AppEditorLogic class (inherited from EditorLogic class), you will see the following methods (C++ example):
#include "AppEditorLogic.h"
AppEditorLogic::AppEditorLogic() {
}
AppEditorLogic::~AppEditorLogic() {
}
int AppEditorLogic::init() {
return 1;
}
int AppEditorLogic::shutdown() {
return 1;
}
int AppEditorLogic::destroy() {
return 1;
}
int AppEditorLogic::update() {
return 1;
}
int AppEditorLogic::render() {
return 1;
}
int AppEditorLogic::worldInit() {
return 1;
}
int AppEditorLogic::worldShutdown() {
return 1;
}
int AppEditorLogic::worldSave() {
return 1;
}
init() function
Engine calls this function on world initialization. It initialized all the belongings of UnigineEditor.
shutdown() function
Engine calls this function on editor shutdown. Engine applications can still work.
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)
update() function
If editor is loaded, 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 editor is loaded, engine calls this function before rendering each render frame.
worldInit() function
If editor is loaded, engine calls this function on world initialization. Use this function, if you want initialize something before the AppWorldLogic starts its init() function.
worldShutdown() function
If editor is loaded, engine calls this function on world shutdown.
worldSave() function
If editor is loaded, engine calls this function on world save.