Jump to content

[SOLVED] GUI element not added in AppWorldLogic::init()


photo

Recommended Posts

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?

Link to comment

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;
}

 

  • Like 1
Link to comment
  • silent changed the title to [SOLVED] GUI element not added in AppWorldLogic::init()
×
×
  • Create New...