Page 1 of 1

[SOLVED] Selection box result

Posted: Sun Mar 01, 2015 5:57 pm
by Warl33k
Hello

I'm trying to make a basic RTS unit control but I'm stuck with the selection box. I followed the intermediate tutorial about it, and it work. Problem is the query's result is a list of movableObject, and I can't cast them to my character object.
Is it possible to get a list of character instead of movableobject ? Or how can I cast/convert them ?

ps : my character class inherit from ogre::entity

Thanks for your help

Re: Selection box result

Posted: Sun Mar 01, 2015 8:00 pm
by spacegaier
Since Ogre doesn't know your custom character class, it of course cannot handle instances of it directly. So all you can get is this list of MovableObjects. As a result you will have to check at runtime if the MovableObject is indeed one of your characters.

Note: The best practice in general is not to inherit from Ogre::Entity, but use composition = your character class should have a member pointer to an Ogre::Entity.

The most elegant way to solve your problem is to attach a pointer to your character class to the MovableObject. See this untested code snippet:

Code: Select all

Character* pC = new Character();
Ogre::Entity* pE = m_pSceneMgr->createEntity();
pE->getUserObjectBindings()->setUserAny("character", pC);

// to later retrieve the character pointer, when iterating over the MovableObject list

Ogre::Any a = pMovableObjectFromSceneManagerList->getUserObjectBindings()->getUserAny("character");
if(!a.isEmpty())
{
    Character* pC = dynamci_cast<Character*>(a);
    // now do stuff...
}
References:
http://www.ogre3d.org/docs/api/latest/c ... 8132f2f01f
http://www.ogre3d.org/docs/api/latest/c ... 24e89bf4b6

Re: Selection box result

Posted: Mon Mar 02, 2015 5:26 pm
by Warl33k
Hello,

after trying again and again, It finally work !

Thanks for you help on this, and also thanks for the advice (not inherit class from Ogre::Entity)

You made my day ! :mrgreen: