getElemenFromId problem

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
emre2345
Posts: 37
Joined: Mon Jul 09, 2007 7:02 pm

getElemenFromId problem

Post by emre2345 »

i want to get the button pointer from a xml based gui. The reason for this is wanting to configure the image of mouse_over image of button. But getElemenFromId returns an IGUIElement * type so i cant do what i want. i found the code below to do my will but it doesn't work.

IGUIButton *b = dynamic_cast<IGUIButton *>(env->getRootGUIElement()->getElementFromId(i));

is there another way to set the image?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I would have thought you could do it without a dynamic cast, just a normal cast:

IGUIButton *b = (IGUIButton *)(env->getRootGUIElement()->getElementFromId(i));

In what way does your dynamic cast not work? Crash? Not compile? No effect from the command?
Image Image Image
emre2345
Posts: 37
Joined: Mon Jul 09, 2007 7:02 pm

Post by emre2345 »

your suggestion does not work too, in my code there is no error while compiling, error comes in run time. IGUIButton b is empty
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Did you check that the pointer without a cast is != 0? Also, dynamic_cast will only work if RTTI is enabled, otherwise the pointer will be casted to 0.
emre2345
Posts: 37
Joined: Mon Jul 09, 2007 7:02 pm

Post by emre2345 »

yes i checked the pointer. nothing works!!! maybe i couldn't do, but for controlling xml base guis, there must be a way to get a pointer from specified element.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Can you be specific about what the "error" is, and what you mean by "IGUIButton b is empty".
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
emre2345
Posts: 37
Joined: Mon Jul 09, 2007 7:02 pm

Post by emre2345 »

actually there is no error, i was answerind so i used the word:).

The problem is that i must get a pointer to the element type i want, but the function return (iguielement *), and i cant convert it to anytype of elements.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

i cant convert it to anytype of elements
If an element with that id exists, and it has the correct type, then the simple C style cast should be fine.

I don't see why you can't write this code...

Code: Select all

gui::IGUIElement* element = env->getRootGUIElement()->getElementFromId(i);
if (element && element->getType() == gui::EGUIET_BUTTON)
{
    gui::IGUIButton* button = (gui::IGUIButton*)element;
    // use button here
}
Of course you shouldn't really need to do the null pointer or type check if you have carefully built your gui, but it is the safe way to make sure you don't have code with undefined behavior.

Travis
Post Reply