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
Fundamentals
Setting Up Development Environment
Usage Examples
UnigineScript
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.

Library's Namespace

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(UNIGINE_VERSION,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 .cpp file add the following:

Source code (UnigineScript)
// my_world.cpp

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: 2018-04-26
Build: ()