This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
基础
专业(SIM)
UnigineEditor
界面概述
资源工作流程
Version Control
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C#
UnigineScript
统一的Unigine着色器语言 UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Animations-Related Classes
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
VR-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

Library's Namespace

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.

By default, all variables and functions are exported from C++ in the global namespace. Libraries provide a convenient way to organize exposed functionality by adding a library's namespace. This namespace is used instead of Foo::Bar syntax that is not allowed for export.

Namespace Export Example#

  1. In order to use a library namespace, a library should be registered first via Unigine::Interpreter::addExternLibrary().
  2. After that, you can use a library namespace to register your variables and functions.
Source code (C++)
#include <UnigineEngine.h>
#include <UnigineInterpreter.h>
#include <UnigineInterface.h>

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


using namespace Unigine;

// A variable within a namespace for export.
namespace Foo {
	int i = 25;
}

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

	// Register a library in order to use a library namespace.
	Interpreter::addExternLibrary("Foo");

	// Export a variable with a library prefix.
	Interpreter::addExternVariable("Foo.integer",MakeExternVariable(&Foo::i));

	AppSystemLogic system_logic;
	AppWorldLogic world_logic;
	AppEditorLogic editor_logic;
	
	Unigine::EnginePtr engine(argc,argv);

	// Enter main loop.
	engine->main(&system_logic,&world_logic,&editor_logic);

	return 0;
}

Access from Scripts#

You can simply call the registered variables, functions, classes from Unigine scripts using the registered name. (If Foo library is not registered, the first dot in an object or function name is treated as an operator of class member access, which is wrong in our case).

In the init() function of the world script .usc file add the following:

Source code (UnigineScript)
// my_world.usc

int init() {
	/* ... code ... */

	log.message("Foo.i is %d\n",Foo.integer);
	engine.console.setActivity(1);

	/* ... code ... */

}

Output#

The following console message will be printed:

Output
Foo.i is 25
Last update: 2024-08-16
Build: ()