removing ALL gui elements

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

removing ALL gui elements

Post by Halan »

hey how can i remove all gui elements but not the gui enviroment? :P

thanks,
Halan
rdm
Posts: 10
Joined: Mon Jun 19, 2006 2:53 pm

Post by rdm »

What about a list of existing gui elements :P
You may keep a list of gui elements and remove them according to that list ...
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

get the root element with IGUIEnvironment::getRootGUIElement
use getChildren to get a list of all IGUIElements, then remove() to get rid of them. I think children should be removed automatically, but if not you may have to recursively get all children into a list, then remove them in reverse order. read the source to CGUIElement::remove() for clarification.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

yeah i thougth so too or do drop(); for everyone but i just wanted to know if theres a better way
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

you shouldn't ever use drop on an object that you didn't grab or create, it will destroy the object and the GUI manager will be left with a broken pointer, causing a crash when it tries to access it.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

so i did it like that

Code: Select all

bool deleteGUI()
{
    list<IGUIElement*> childlist = env->getRootGUIElement()->getChildren();
    list<IGUIElement*>::Iterator it = childlist.begin();
    IGUIElement* dummyelement;
    for(int i = 0; i < childlist.getSize(); i++)
    {
       dummyelement = *it;
       dummyelement->remove();
       it++;
    }
};
but i get a segementation fault :(

any ideas?

greets,
Halan
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

Maybe deleting an object invalidates the iterator?
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

yeah i think youre right but again im not so into lists and dont really know how to do it. i tried this but its in some way the same ^^

Code: Select all

bool deleteGUI()
{
    list<IGUIElement*> childlist = env->getRootGUIElement()->getChildren();
    IGUIElement* dummyelement;
    for(int i = 0; i < childlist.getSize(); i++)
    {
       dummyelement = *childlist.begin()+i;
       if(dummyelement)
           dummyelement->remove();
    }
};
greets,
halan
Dibalo
Posts: 30
Joined: Sat Aug 12, 2006 2:31 pm
Location: Finland
Contact:

Post by Dibalo »

You are deleting the GUI in function. I don´t see eny parameters importing IGUIEnvironment-pointer to function. Is the env-pointer global and you have 100% sure that it isn´t NULL-pointer?? I´d do this function with one parameter:

Code: Select all

bool deleteGUI(IGUIEnvironment* env)
{
	if(env)			// lets check that pointer is not NULL
	{
		list<IGUIElement*> childlist = env->getRootGUIElement()->getChildren();
		list<IGUIElement*>::Iterator it = childlist.begin(); 
		IGUIElement* dummyelement;
		for(int i = 0; i < childlist.getSize(); i++)
		{
			dummyelement = *it;				// you can do also a check that iterator isn´t pointing to NULL
			dummyelement->remove();
			it++;
		} 
	}
};
You can do the same check for iterator than I did for Environment. Or the easiest way is using debugger. :wink:
Dattebayo!!
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

no it isnt pointing to null cause it works like that (of course only one element is removed).

Code: Select all

bool deleteGUI()
{
    list<IGUIElement*> childlist = env->getRootGUIElement()->getChildren();
    IGUIElement* dummyelement;
    list<IGUIElement*>::Iterator it = childlist.begin();

       dummyelement = *childlist.begin();
       if(dummyelement)
           dummyelement->remove(); 

};
but i think stodge is right, when i delete the element the iterator becomes invalid. so ive no idea how to do it instead...
imflammable
Posts: 2
Joined: Sun Aug 13, 2006 6:11 pm

Post by imflammable »

I use this, and it seems to work...

Code: Select all

bool deleteGUI()
{
    core::list< gui::IGUIElement * > childlist = env->getRootGUIElement()->getChildren();
    while( !childlist.empty() )
        (*(childlist.getLast()))->remove();
  
    return true;
}
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

damit it doesnt work well if i do it like that 4 (and thats really confusing me) GUI elements will be removed

Code: Select all

bool deleteGUI()
{
    list<IGUIElement * > childlist = env->getRootGUIElement()->getChildren();

    return true;
}       
something is really going wrong here...

greetings,
halan

edit: atm ive a workaround with an array wich contains the pointers...
but i dont like it :cry: ;)

edit2: maybe this i related to my guielements not having a background e.g. the listbox
Post Reply