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
Программирование
Fundamentals
Setting Up Development Environment
UnigineScript
C++
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
Внимание! Эта версия документация УСТАРЕЛА, поскольку относится к более ранней версии SDK! Пожалуйста, переключитесь на самую актуальную документацию для последней версии SDK.
Внимание! Эта версия документации описывает устаревшую версию SDK, которая больше не поддерживается! Пожалуйста, обновитесь до последней версии SDK.

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(), render(), 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 its name
	Log::message("Initializing the FIRST world named %s.world\n", World::get()->getName());
	
	// 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 its name
	Log::message("Initializing the SECOND world named %s.world\n", World::get()->getName());
	
	// 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 "AppWorldLogic1.h"		//<-- include world logic class declaration for the first world
#include "AppWorldLogic2.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:
	AppWorldLogic1 wl1;									//<-- world logic for the first world
	AppWorldLogic2 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 (strcmp(World::get()->getName(), "world1") == 0)
		current_world_logic = &wl1;
	else if (strcmp(World::get()->getName(), "world2") == 0)
		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 <UnigineApp.h>
#include <UnigineWorld.h>

// ...
int AppSystemLogic::update()
{
	// checking key states and loading worlds
	if (App::get()->clearKeyState(App::KEY_PGUP))
	{	// loading the second world if it is not already loaded
		if (strcmp(World::get()->getName(), "world2") != 0)
			World::get()->loadWorld("world2");
	}
	else if (App::get()->clearKeyState(App::KEY_PGDOWN))
	{	// loading the first world if it is not already loaded
		if (strcmp(World::get()->getName(), "world1") != 0)
			World::get()->loadWorld("world1");
	}
	
	return 1;
}

// ...

Now, the logic of each of our world files (world1 and world2) is controlled by the corresponding WorldLogic class.

Last update: 27.12.2018
Build: ()