angus Posted August 15, 2017 Posted August 15, 2017 The below code adds a button to the screen when run in main()/winmain() GuiPtr gui = Unigine::Gui::get(); WidgetButtonPtr widget_button = WidgetButton::create(gui, "click"); widget_button->setCallback0(Gui::CLICKED, MakeCallback(Utils::testit)); gui->addChild(widget_button->getWidget(), Gui::ALIGN_TOP); When the same code is run from AppWorldLogic::init() the button does not appear, even though the routine is run. Any idea where I'm going wrong?
ded Posted August 16, 2017 Posted August 16, 2017 Hi Angus, As you may know Unigine::Ptr<> is somewhat similar to std::shared_ptr<> and maintains shared ownership of an object. The lifetime of the WidgetButtonPtr in your code ends when the AppWorldLogic::init() returns and button is destroyed almost immediately after creation. The right way to create a button with the lifetime of the scene is like this: class AppWorldLogic : public WorldLogic { public: ... virtual int init() { GuiPtr gui = Gui::get(); widget_button = WidgetButton::create(gui, "click"); widget_button->setCallback0(Gui::CLICKED, MakeCallback(Utils::testit)); gui->addChild(widget_button->getWidget(), Gui::ALIGN_TOP); return 1; } virtual int shutdown() { widget_button.clear(); return 1; } private: WidgetButtonPtr widget_button; } 1
Recommended Posts