dynamic_cast of IGUIElement seg faults

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
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

dynamic_cast of IGUIElement seg faults

Post by fargoth »

It seems irrlicht is configured to compile with no RTTI - Is there a reason why I shouldn't enable it?

Maybe I should modify how I go about things?
I'm casting to get the gui values after a button was clicked, e.g:

Code: Select all

unsigned int CMazeGameMenu::getChosenNumOfAIPlayers()
    {
        float numOfAI = dynamic_cast<irr::gui::IGUISpinBox *>(
            _guiEnv->getRootGUIElement()->getElementFromId(MenuElement::MazeNumOfAI))->getValue();
        return static_cast<unsigned int>(round(numOfAI));
    }
Is there a better way, or should I just recompile the library with RTTI on?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: dynamic_cast of IGUIElement seg faults

Post by hendu »

RTTI brings 10% more binary size, and some runtime overhead as well.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: dynamic_cast of IGUIElement seg faults

Post by CuteAlien »

I never tried it with rtti. For reasons hendu mentioned it's usually not used in game industry (at least all engines I worked with so far had disabled it). You have to use comparisons with getType() instead. In this case for EGUIET_SPIN_BOX.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

Re: dynamic_cast of IGUIElement seg faults

Post by fargoth »

But to access the function of the spinbox from the base guielement pointer I still have to dynamic_cast, don't I?
My game crushed on me before I turned RTTI on, and the only thing I did was dynamic_cast, I didn't even use typeid...
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: dynamic_cast of IGUIElement seg faults

Post by CuteAlien »

No, you can use static_cast when you know the type will be fine.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply