getChild doesn't work with id zero

Issue #1148 resolved
Former user created an issue

Operating System: Win7 (64-bit) OS version/build: SP1

Reproducibility: always

Reproduction steps:

So I've got a UI class that wraps all my CEGUI stuff for me, and it has a member that holds a pointer to my GuiContext like this:

CEGUI::GUIContext* context;

which gets assigned in my initialization code like this:

this->context = &CEGUI::System::getSingleton().createGUIContext(UserInterface::renderer->getDefaultRenderTarget());
this->context->setDefaultFont("DejaVuSans-10");

CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::Window* root = wmgr.createWindow("DefaultWindow");
this->context->setRootWindow(root);

later on I'm creating some StaticText controls and then updating their text, however if I assign an id of zero then getChild doesn't seem to find my StaticText control (it's not null... but everything inside of it appears to be default values rather than the values for my control from what I can tell... I'm still new to CEGUI so I'm not 100% sure of what I'm seeing). Anywho, if I use ID zero then getChild fails to return my control, but if I use any other number then it works just fine and the text updates as expected.

const int id = 0;   //any other value besides zero works fine!

CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::DefaultWindow* control = static_cast<CEGUI::DefaultWindow*>(wmgr.createWindow("TaharezLook/StaticText"));
this->context->getRootWindow()->addChild(control);

control->setPosition(CEGUI::UVector2(CEGUI::UDim(0.5f, 0.0f), CEGUI::UDim(0.5f, 0.0f)));
control->setSize(CEGUI::USize(CEGUI::UDim(0.1f, 0.0f), CEGUI::UDim(0.1f, 0.0f)));

control->setText("test");
control->setID(id);

control = nullptr;  //just to be sure we're not using original pointer

CEGUI::Window* controlFromGetChild = this->context->getRootWindow()->getChild(id);
controlFromGetChild->setText("changed text");

I can work around this by just not using zero as an id, but thought I'd share incase anyone else stumbles on this. :)

Hope this helps, please let me know if you need any more info.

using CEGUI 0.8.7 with Visual Studio 2015 Update 3 (version 14.0.25431.01) and CEGUI's OpenGL renderer module.

Comments (6)

  1. Manh Nguyen Tien

    Failed to reproduce on Linux 64-bit with HelloWorld sample. CEGUI 0.8.7 & GCC 5.4.0

    wnd->setID(0);
    CEGUI::Window* w = d_root->getChild(0);
    w->setText("Hello World!");
    

    The window has the text "Hello World!" displayed.

  2. Christopher Cooper

    Ok, so I dug into it more and even made a stand-alone test app to try and reproduce the issue and I wasn't able to.

    What was actually happening is that I had created another child (a ListBox) of the root window without explicitly setting its ID prior to the area where I was creating StaticTexts and manually setting IDs. This first ListBox child was created without specifying a name or id which consumed ID zero when I added it, then I added the 2nd child (StaticText) and set its ID to zero. I stepped through the addChild code and saw that

    void Element::addChild_impl(Element* element)
    

    pushes back the element into the d_children set, so my static text was the 2nd ID zero I had in the set. When I went to call GetChild with ID zero, it gave me the first ID zero it found which was my first control (the ListBox), not my static text.

    I'll look into updating addChild_impl for my purposes to enforce unique IDs when adding (I know at a cost of performance... so maybe in DEBUG builds only???) but this might help me avoid this in the future. This is also what I get for using IDs instead of names :P

    Sorry for the false alarm here, but this infact looks like user-error on my part.

  3. Log in to comment