This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Basics
Rendering
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Version Control
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
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
Materials and Shaders
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
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
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

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.cs file 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.cs

Source code (C#)
// ...

namespace UnigineApp
{
    class WorldLogic1 : WorldLogic
    {
        public WorldLogic1()
		{
		}

		public override bool Init()
		{
            Log.Message("Initializing the FIRST world ({0})\n", World.Path);
			
			// insert your init logic for the first world
			
			return true;
		}
		
		public override bool Update()
		{
			// insert your update logic for the first world
			
			return true;
		}
		
		// ...
    }
}

WorldLogic2.cs

Source code (C#)
// ...

namespace UnigineApp
{
    class WorldLogic2: WorldLogic
    {
        public WorldLogic2()
		{
		}

		public override bool Init()
		{
            Log.Message("Initializing the SECOND world ({0})\n", World.Path;
			
			// insert your init logic for the second world
			return true;
		}
		
		public override bool Update()
		{
			// insert your update logic for the second world
			
			return true;
		}
		
		// ...
    }
}

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. 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 should change the current world logic. So, we modify the AppWorldLogic.cs file as follows:

AppWorldLogic.cs

Source code (C#)
// ...

using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{
        public WorldLogic1 wl1;						//<-- world logic for the first world
        public WorldLogic2 wl2;						//<-- world logic for the second world
        public WorldLogic current_world_logic;		//<-- pointer to the current world logic

		public AppWorldLogic()
		{
            // creating world logic instances and setting the first one as current
            wl1 = new WorldLogic1();
            wl2 = new WorldLogic2();
            current_world_logic = wl1;
		}

		public override bool Init()
		{
            // checking the name of the loaded world and changing current world logic if necessary
            if (string.Compare(World.Path, "world1") == 0)
                current_world_logic = wl1;
            else if (string.Compare(World.Path, "world2") == 0)
                current_world_logic = wl2;

            // calling the init() method of the current world logic
            current_world_logic.Init();

			return true;
		}

		// start of the main loop
		public override bool Update()
		{
			// Write here code to be called before updating each render frame: specify all graphics-related functions you want to be called every frame while your application executes.
            
			// calling the update() method of the current world logic
            current_world_logic.Update();
			
			return true;
		}

		// ...
	}
}

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.cs file as follows:

AppSystemLogic.cs

Source code (C#)
// ...

using Unigine;

namespace UnigineApp
{
    class AppSystemLogic : SystemLogic
    {
        // ...

        public override bool Update()
        {
            // checking key states and switching worlds
            if (App.ClearKeyState(App.KEY_PGUP) == 1)
            {   // loading the second world if it is not already loaded
                if (World.Path.EndsWith("world2.world"))
                    World.LoadWorld("world2");
            }
            else if (App.ClearKeyState(App.KEY_PGDOWN) == 1)
            {   // loading the first world if it is not already loaded
                if (World.Path.EndsWith("world1.world"))
                    World.LoadWorld("world1");
            }
            return true;
        }

        // ...
    }
}

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

Last update: 2023-02-28
Build: ()