This page has been translated automatically.
Видеоуроки
Интерфейс
Основы
Продвинутый уровень
Подсказки и советы
Основы
Программирование на C#
Рендеринг
Профессиональный уровень (SIM)
Принципы работы
Свойства (properties)
Компонентная Система
Рендер
Режимы вывода изображения
Физика
Браузер SDK 2
Лицензирование и типы лицензий
Дополнения (Add-Ons)
Демонстрационные проекты
API Samples
Редактор UnigineEditor
Обзор интерфейса
Работа с ассетами
Контроль версий
Настройки и предпочтения
Работа с проектами
Настройка параметров ноды
Setting Up Materials
Настройка свойств
Освещение
Sandworm
Использование инструментов редактора для конкретных задач
Расширение функционала редактора
Встроенные объекты
Ноды (Nodes)
Объекты (Objects)
Эффекты
Декали
Источники света
Geodetics
World-ноды
Звуковые объекты
Объекты поиска пути
Player-ноды
Программирование
Основы
Настройка среды разработки
C++
C#
UnigineScript
Унифицированный язык шейдеров UUSL (Unified UNIGINE Shader Language)
Плагины
Форматы файлов
Материалы и шейдеры
Rebuilding the Engine Tools
Интерфейс пользователя (GUI)
Двойная точность координат
API
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
Учебные материалы

Splitting Logic Between Several WorldLogic Classes

Each world script is associated with a certain world. Although the basic recommended workflow when writing application logic in C++ / C# is to have a single AppWorldLogic class to process all worlds in your project, sometimes it might be necessary to split world logic between several separate classes. In such a case the basic AppWorldLogic class is used as a manager calling corresponding methods (init(), update(), postUpdate(), etc.) of a particular class implementing logic of the current world.

This example illustrates how to split world-specific code between separate classes. Suppose in our project we have a default world named "world1" and another one named "world2" and we want to have two separate world logic classes (WorldLogic1 and WorldLogic2) for each of these worlds. Suppose we also want to switch between these worlds by pressing PGUP and PGDOWN keys.

1. Creating WorldLogic Classes#

First, we just inherit two new classes (WorldLogic1 and WorldLogic2) from Unigine::WorldLogic. You can simply copy the contents of AppWorldLogic.h and AppWorldLogic.cpp files and modify their init() and update() methods like this:

Notice
You can implement your logic for other methods of the execution sequence if necessary.

WorldLogic1.cpp

Source code (C++)
// ...

int WorldLogic1::init()
{
	// reporting that we're processing the first world and displaying a path to the .world file
	Log::message("Initializing the FIRST world (%s)\n", World::getPath());

	// insert your init logic for the first world

	return 1;
}

int WorldLogic1::update()
{
	// insert your update logic for the first world

	return 1;
}

// ...

WorldLogic2.cpp

Source code (C++)
// ...

int WorldLogic2::init()
{
	// reporting that we're processing the first world and displaying a path to the .world file
	Log::message("Initializing the SECOND world (%s)\n", World::getPath());

	// insert your init logic for the second world

	return 1;
}

int WorldLogic2::update()
{
	// insert your update logic for the second world

	return 1;
}

// ...

2. Managing World Logics via AppWorldLogic#

Now, we implement world logic management in the AppWorldLogic, so we should add all our world logics, and a pointer to the one currently used. In the init() method, as it is called each time a new world is loaded, we should change the current world logic. So we modify the AppWorldLogic.h file as follows:

AppWorldLogic.h

Source code (C++)
// ...

#include "WorldLogic1.h"		//<-- include world logic class declaration for the first world
#include "WorldLogic2.h"		//<-- include world logic class declaration for the second world

class AppWorldLogic : public Unigine::WorldLogic
{

public:

// ...

	
	Unigine::WorldLogic* current_world_logic = &wl1;	//<-- pointer to the current world logic, we set the first one by default

private:
	WorldLogic1 wl1;									//<-- world logic for the first world
	WorldLogic2 wl2;									//<-- world logic for the second world
};

//...

Inside each method of the AppWorldLogic class, we simply call the corresponding method of the current world logic class, like for the init() and update() methods below. In the init() method, as it is called each time a new world is loaded, we also should change the current world logic, if necessary:

AppWorldLogic.cpp

Source code (C++)
#include <UnigineWorld.h>	//<-- include the UnigineWorld.h for the World class

using namespace Unigine;
// ...

int AppWorldLogic::init()
{
	// checking the name of the loaded world and updating current world logic
	if (strstr(World::getPath(), "world1.world"))
		current_world_logic = &wl1;
	else if (strstr(World::getPath(), "world2.world"))
		current_world_logic = &wl2;

	// calling the init() method of the current world logic
	current_world_logic->init();

	return 1;
}

int AppWorldLogic::update()
{
	// calling the update() method of the current world logic
	current_world_logic->update();

	return 1;
}

3. Keyboard World Loading via AppSystemLogic#

Switching between the worlds is to be performed in the AppSystemLogic class. In the update() method we should check keyboard input and load the corresponding world, if necessary. So, we modify the AppSystemLogic.cpp file as follows:

AppSystemLogic.cpp

Source code (C++)
#include <UnigineWorld.h>

// ...
int AppSystemLogic::update()
{
	// checking key states and loading worlds
	if (Input::isKeyPressed(Input::KEY_PGUP))
	{	// loading the second world if it is not already loaded
		if (strstr(World::getPath(), "world1.world"))
			World::loadWorld("world2");
	}
	else if (Input::isKeyPressed(Input::KEY_PGDOWN))
	{	// loading the first world if it is not already loaded
		if (strstr(World::getPath(), "world2.world"))
			World::loadWorld("world1");
	}

	return 1;
}
Last update: 16.08.2024
Build: ()