matthew.clifton Posted November 6, 2018 Share Posted November 6, 2018 Hi all, I'm trying to access a public variable I have created within AppSystemLogic.h. I have various different AppWorldlogic classes. I've been following the processes for sharing Member Variables from https://developer.unigine.com/en/docs/2.7.2/code/usage/sharing_data/index but I'm struggling to get it to compile. I think the problem seems to come from trying to create a pointer to AppSystemLogic within my new WorldLogic class as there is already a pointer in AppWorldLogic.h There are only a few lines of code within each header, but it would be quite a block of text to paste them all here. I've zipped them up if someone would have a moment to scan over them? Source:https://we.tl/t-HSW9e3yMd7 I'm trying to access the 'test_var' in PlanettestWorldLogic::init() I can't thank you enough in advance, Matt. Link to comment
fox Posted November 7, 2018 Share Posted November 7, 2018 Hi Matt, Maybe it's easier to use a global variable in this case instead of an AppSystemLogic class member. This should solve your problem. Thank you! Link to comment
matthew.clifton Posted November 7, 2018 Author Share Posted November 7, 2018 Thank you for your reply Fox That is a shame I have to go down the route of a global variable class. There isn't a way of having the shared variables within the AppSystemLogic? I guess ultimately the final goal is the same, it just seems cleaner to have them within the persisting AppSystemLogic. Kind Regards, Matt. Link to comment
matthew.clifton Posted November 7, 2018 Author Share Posted November 7, 2018 Hi again @fox... just thinking a bit more about this. Surely i'm going to need access to the AppSystemLogic object to handle the loading of Worlds? Apologies if this is derailing the thread a bit, but it all stems from my inability to access AppSystemLogic from derived WorldLogic classes. Any thoughts on this? Best Regards, Matt. Link to comment
fox Posted November 8, 2018 Share Posted November 8, 2018 Hi Matt, Sorry for the delay. No problem, if you want to use a member variable of the AppSystemLogic class. You can put everything related to changing world logics to the AppWorldLogic. // 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; } // ... In this case we'll have just world loading operations in the AppSystemLogic. // 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; } // ... Then you can add your member variable: // AppSystemLogic.h // ... class AppSystemLogic : public Unigine::SystemLogic { public: // ... int member_var = 1;// <-- member variable }; Include the AppSystemLogic.h and add a pointer to the AppSystemLogic to each of your derived classes and use your member variable: // WorldLogic1.h // ... #include "AppSystemLogic.h" //<-- header file for the AppSystemLogic class class AppWorldLogic1 : public Unigine::WorldLogic { public: // ... AppSystemLogic *system_logic;// <-- pointer to the AppSystemLogic instance }; // --------------------------------------------- // WorldLogic1.cpp // ... int AppWorldLogic1::init() { Log::message("This is WORLD 1 named\n"); Log::message("The value of the member variable is %d\n", system_logic->member_var);// <-- print the value of the member variable system_logic->member_var = 111; // <-- modify the value of the member variable return 1; } And the last thing to do is to link instances of your derived classes with the AppSystemLogic instance in main(): // <your_project_name>.cpp // ... int main(int argc, char *argv[]) #endif { // init engine Unigine::EnginePtr engine(UNIGINE_VERSION, argc, argv); // UnigineLogic AppSystemLogic system_logic; AppWorldLogic world_logic; world_logic.wl1.system_logic = &system_logic; //<-- linking WL1 with the AppSystemLogic instance world_logic.wl2.system_logic = &system_logic; //<-- linking WL2 with the AppSystemLogic instance AppEditorLogic editor_logic; // enter main loop engine->main(&system_logic, &world_logic, &editor_logic); return 0; } // ... Hope this helps! Thank You! Link to comment
matthew.clifton Posted November 8, 2018 Author Share Posted November 8, 2018 Thank you so much.... this worked perfectly! Best regards, Matt. Link to comment
Recommended Posts