matthew.clifton Posted October 30, 2018 Share Posted October 30, 2018 Hi all, Created a new World and now want to create a new WorldLogic class. I can see the UnigineScript class being created with the new World, but how do I create the equivalent c++ link between world and a c++ WorldLogic? Each world seems to be calling the template AppWorldLogic. I can create a new class derived from Unigine::WorldLogic, but I don't know where to instantiate it etc. I've been hunting the demos and samples but they all seem to be just a single world. I hope the above made some sense. Best regards in advance, Matt. Link to comment
fox Posted October 31, 2018 Share Posted October 31, 2018 Hi Matt, Suppose in your project you have a default world named "world1" and another one named "world2" and you want to have two separate world logic classes for each of these worlds. Suppose you want to switch between these worlds by pressing PGUP and PGDOWN keys. The basic recommended workflow is to have a single AppWorldLogic class to process all worlds in your project. You can split world-specific code between separate classes inherited from Unigine::WorldLogic (in this example that would be WorldLogic1 and WorldLogic2) and use the AppWorldLogic class as a manager calling corresponding methods (init, update, render, etc.) for the current world logic. 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() methods like this: // WorldLogic1.cpp file int WorldLogic1::init() { // reporting that we're processing the first world and displaying its name Log::message("This is WORLD 1 named %s\n", World::get()->getName()); return 1; } // WorldLogic2.cpp file int WorldLogic2::init() { // reporting that we're processing the second world and displaying its name Log::message("This is WORLD 2 named %s\n", World::get()->getName()); return 1; } Then we put everything related to changing world logics to theAppWorldLogic. // AppWorldLogic.h #include <UnigineLogic.h> #include <UnigineStreams.h> #include "AppWorldLogic1.h" //<-- include world logic class declaration for the first world #include "AppWorldLogic2.h" //<-- include world logic class declaration for the first world class AppWorldLogic : public Unigine::WorldLogic { public: //... Unigine::WorldLogic *current_world_logic = &wl1; //<-- pointer to the current world logic AppWorldLogic1 wl1; //<-- world logic for the first world AppWorldLogic2 wl2; //<-- world logic for the second world }; #endif // __APP_WORLD_LOGIC_H__ Inside each method of the AppWorldLogic class we simply call the corresponding method of the current world logic class. 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 #include <UnigineWorld.h> // ... 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 init method of the current world current_world_logic->init(); return 1; } // ... Switching between the worlds is to be performed in the AppSystemLogic class, so we should modify implementation of its update() method in the AppSystemLogic.cpp file like this: // AppSystemLogic.cpp #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; } // ... Hope this helps! Thank you! Link to comment
matthew.clifton Posted October 31, 2018 Author Share Posted October 31, 2018 Hi fox, Thank you so much for the detailed reply!! I'll give it a shot just as soon as I get home this evening. I'm only a week into this engine (only dabbled with worlds and assets up to now), and i'm loving it! Best Regards, Matt. Link to comment
Valuna Posted November 8, 2018 Share Posted November 8, 2018 (edited) On 10/31/2018 at 8:19 AM, fox said: current_world_logic = &wl2; World::get()->loadWorld("world2"); Wouldn't this mean that you call wl2::shutdown() on loadworld(wl2) instead of wl1::shutdown()? Edited November 8, 2018 by Valuna spelling error Link to comment
fox Posted November 8, 2018 Share Posted November 8, 2018 Sorry, my bad, the code is fixed! Thank you for reporting! 1 Link to comment
Recommended Posts