This page has been translated automatically.
Видеоуроки
Interface
Essentials
Advanced
Подсказки и советы
Основы
Программирование на C#
Рендеринг
Professional (SIM)
Принципы работы
Свойства (properties)
Компонентная Система
Рендер
Физика
Редактор UnigineEditor
Обзор интерфейса
Работа с ассетами
Настройки и предпочтения
Работа с проектами
Настройка параметров ноды
Setting Up Materials
Настройка свойств
Освещение
Sandworm
Использование инструментов редактора для конкретных задач
Расширение функционала редактора
Встроенные объекты
Ноды (Nodes)
Объекты (Objects)
Эффекты
Декали
Источники света
Geodetics
World-ноды
Звуковые объекты
Объекты поиска пути
Players
Программирование
Основы
Настройка среды разработки
Примеры использования
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Плагины
Форматы файлов
Materials and Shaders
Rebuilding the Engine Tools
Интерфейс пользователя (GUI)
Двойная точность координат
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
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Работа с контентом
Оптимизация контента
Материалы
Визуальный редактор материалов
Сэмплы материалов
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
Внимание! Эта версия документация УСТАРЕЛА, поскольку относится к более ранней версии SDK! Пожалуйста, переключитесь на самую актуальную документацию для последней версии SDK.
Внимание! Эта версия документации описывает устаревшую версию SDK, которая больше не поддерживается! Пожалуйста, обновитесь до последней версии SDK.

Callbacks

Warning
The scope of applications for UnigineScript is limited to implementing materials-related logic (material expressions, scriptable materials, brush materials). Do not use UnigineScript as a language for application logic, please consider C#/C++ instead, as these APIs are the preferred ones. Availability of new Engine features in UnigineScript (beyond its scope of applications) is not guaranteed, as the current level of support assumes only fixing critical issues.

Any function from system, world or editor scripts can be called in a C++ code. UnigineScript functions that are called from an external code are known as callbacks. Via callbacks scripts can communicate with each other, as well as with the external application.

  • Callbacks support up to 4 arguments and can return a value of an arbitrary type.

See also#

An example can be found in <UnigineSDK>/source/samples/Api/Scripts/Callbacks/ directory.

Callbacks Usage Example#

C++ Side#

To demonstrate how callbacks can be used, let's code the C++ part first. Here's code from your_project_name.cpp file:

Source code (C++)
#include <UnigineEngine.h>
#include <UnigineInterpreter.h>
#include <UnigineInterface.h>

#include "AppSystemLogic.h"
#include "AppWorldLogic.h"
#include "AppEditorLogic.h"


using namespace Unigine;

/*
	World function
*/
const Variable &runWorldFunction(const Variable &name,const Variable &v) {
	
	Log::warning("runWorldFunction(%s,%s) is called\n",name.getTypeName().get(),v.getTypeName().get());
	
	Engine *engine = Engine::get();
	return engine->runWorldFunction(name,v);
}

/*
*/
#ifdef _WIN32
	int wmain(int argc,wchar_t *argv[]) {
#else
	int main(int argc,char *argv[]) {
#endif

	// export the runWorldFunction() function defined above
	Unigine::Interpreter::addExternFunction("runWorldFunction", MakeExternFunction(&runWorldFunction));
	
	AppSystemLogic system_logic;
	AppWorldLogic world_logic;
	AppEditorLogic editor_logic;
	
	Unigine::EnginePtr engine(argc,argv);
	
	engine->main(&system_logic,&world_logic,&editor_logic);
	
	return 0;
}

The following code should be in AppWorldLogic.cpp

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

#include <UnigineInterpreter.h>


using namespace Unigine;
/*
 */
AppWorldLogic::AppWorldLogic() {
	
}

AppWorldLogic::~AppWorldLogic() {
	
}

/*
 */
int AppWorldLogic::init() {
	return 1;
}

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

/*
 */
int AppWorldLogic::update() {
	/*
		Callbacks usage example
	*/

	// call the counter() function of the script
	Variable ret = Engine::get()->runWorldFunction(Variable("counter"));
	// print a message depending on the value returned by the counter() script function:
	// print the current value of the counter
	if (ret.getInt() != -1) Log::message("counter is: %d\n", ret.getInt());
	// print the path to the world file
	if (ret.getInt() == 3) Log::message("\nworld-file path is: \"%s\"\n", Engine::get()->runWorldFunction(Variable("engine.world.getPath")).getString());

	return 1;
}

int AppWorldLogic::postUpdate() {
	return 1;
}

int AppWorldLogic::updatePhysics() {
	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;
}

Unigine Script Side#

Notice
The required world should be loaded and the script assigned to this *.world should not contain any compilation errors.

The world script file where callbacks are defined:

Source code (UnigineScript)
// unigine_project.usc

/*
*/
int callback(int value) {
	
	log.warning("callback(%s) is called\n",typeinfo(value));
	
	return value;
}

/*
*/
void counter() {
	
	for(int i = 0; i < 4; i++) {
		log.warning("counter(): called\n");
		yield i;
	}
	
	return -1;
}

/*
*/
int init() {
	
	log.message("\n");
	
	// run the callback() script function via the API runWorldFunction() function
	log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback",10)));
	log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback",vec3(1,2,3))));
	log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback","a string")));
	
	/////////////////////////////////
	
	// show a console
	engine.console.setActivity(1);
	
	return 1;
}

Calling Sequence#

The sequence of function call will be as follows:

  1. The interpreter exports the runWorldFunction() function to make it available from the script.
  2. The engine is initialized, and the init() function of the script is called. This function calls the exported runWorldFunction() function.
  3. The exported runWorldFunction() function calls the callback() function from the script.
  4. The engine enters the main loop, where it calls the counter() function from the script by using the Unigine::Engine::runWorldFunction() function.

Output#

The following result will be printed into the console:

Output
runWorldFunction(string,int): called
callback(int: 10): called
result is: int: 10

runWorldFunction(string,vec3): called
callback(vec3: 1 2 3): called
result is: vec3: 1 2 3

runWorldFunction(string,string): called
callback(string: "a string") is called
result is: string: "a string"

counter(): called
counter is: 0
counter(): called
counter is: 1
counter(): called
counter is: 2
counter(): called
counter is: 3

world world-file path is: "unigine_project/unigine_project"
Last update: 10.10.2022
Build: ()