This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Landscape Tool
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Rebuilding the Engine 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
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
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.

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 <UnigineApp.h>
#include <UnigineWorld.h>

// ...
int AppSystemLogic::update()
{
	// checking key states and loading worlds
	if (App::clearKeyState(App::KEY_PGUP))
	{	// loading the second world if it is not already loaded
		if (strstr(World::getPath(), "world1.world"))
			World::loadWorld("world2");
	}
	else if (App::clearKeyState(App::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: 2021-12-13
Build: ()