How to use Ogre::Any ?

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16

How to use Ogre::Any ?

Post by KungFooMasta »

I have a class called 'GameObject', and I want my entity objects to store a pointer to a GameObject.

I've tried various things but I keep getting compile errors. Here is my setup:

Code: Select all

...
mMovableObject->setUserAny(Ogre::Any(mGameObject));
...

Code: Select all

...
Ogre::Any any = entity->getUserAny();
rc.hitGameObject = any<GameObject*>();
...
Compiler error:

Code: Select all

1>d:\game_dev\gaia\src\gaiascene.cpp(702) : error C2059: syntax error : '>'
I've also gotten this:

Code: Select all

1>d:\game_dev\gaia\src\gaiascene.cpp(702) : error C2783: 'ValueType Ogre::Any::operator ()(void) const' : could not deduce template argument for 'ValueType'
1>        d:\game_dev\dependencies\include\ogre\ogreany.h(190) : see declaration of 'Ogre::Any::operator ()'
For reference, here is the function I'm trying to use:

Code: Select all

00181     public: 
00182 
00183         template<typename ValueType>
00184         ValueType operator()() const
00185         {
00186             if (!mContent) 
00187             {
00188                 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
00189                     "Bad cast from uninitialised Any", 
00190                     "Any::operator()");
00191             }
00192             else if(getType() == typeid(ValueType))
00193             {
00194                 return static_cast<Any::holder<ValueType> *>(mContent)->held;
00195             }
00196             else
00197             {
00198                 StringUtil::StrStreamType str;
00199                 str << "Bad cast from type '" << getType().name() << "' "
00200                     << "to '" << typeid(ValueType).name() << "'";
00201                 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
00202                      str.str(), 
00203                     "Any::operator()");
00204             }
00205         }
Am I even storing the pointer in the Any correctly? How can I retrieve the pointer from it?

Thanks for any help.
Creator of QuickGUI!
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58

Re: How to use Ogre::Any ?

Post by CABAListic »

You are trying to give a template argument to a variable name. That's illegal, and that's what the compiler is telling you.

Anyway, the correct way to access the object stored inside the Any is via any_cast:

Code: Select all

rc.hitGameObject = any_cast<GameObject*>(any);
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16

Re: How to use Ogre::Any ?

Post by KungFooMasta »

Awesome, that did it. Thanks!

By the way, how is the overloaded () operator used? If the intention is to use any_cast, maybe it should be documented somewhere.
Creator of QuickGUI!
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Re: How to use Ogre::Any ?

Post by nullsquared »

KungFooMasta wrote: By the way, how is the overloaded () operator used? If the intention is to use any_cast, maybe it should be documented somewhere.
I think whoever wrote it thought it could be used as someAny<int>() when it actually needs to be someAny.operator()<int>();
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16

Re: How to use Ogre::Any ?

Post by KungFooMasta »

Ah interesting, I've never used operator overloads explicitly like that. Thanks for the information all. :D
Creator of QuickGUI!
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: How to use Ogre::Any ?

Post by cool_thomas »

Should this be in a different forum then since it's Ogre specific?

I am trying to use Ogre::Any to reference an object inside of an Ogre::SceneNode

I have class like this:

Code: Select all

class Object : public Ogre::Any
I then create the node like this:

Code: Select all

void Object::createNode(std::string objectName, Ogre::SceneNode *parentNode)
    {
    Ogre::String nodeName = "NODE" + objectName + Ogre::StringConverter::toString((int)ID);
    if(parentNode)
    {
         ObjectNode = parentNode->createChildSceneNode(nodeName);
    }
    else
    {
        ObjectNode = APP->SceneMgr->getRootSceneNode()->createChildSceneNode(nodeName);
    }
    ObjectNode->setUserAny(Ogre::Any(this));
}
Then in my collision code, which is just MOC (Minimal Ogre Collision) from Artifex Terra I do this:

Code: Select all

Ogre::Entity *pentity = static_cast<Ogre::Entity*>(query_result[qr_idx].movable);
Ogre::Any any = pentity->getUserAny();
SpellByte::Object *obj = any_cast<SpellByte::Object*>(any);
std::cout << "Name: " + obj->getName();
Then I get this:
ORE EXCEPTION(2:InvalidParametersException): Bad cast from type 'v' to 'PN9SpellByte6ObjectE' in Ogre::any_cast at ..\include\OGRE/OgreAny.h (line 429)

Anyone know what I might be doing wrong?

Thanks
Slicky
Bronze Sponsor
Bronze Sponsor
Posts: 614
Joined: Mon Apr 14, 2003 11:48 pm
Location: Was LA now France
x 26

Re: How to use Ogre::Any ?

Post by Slicky »

It looks to me like you are storing the cast in the node and retrieving it from the entity. You have to use the same store/recall object.
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: How to use Ogre::Any ?

Post by cool_thomas »

Oh man :oops: you are absolutely right. Works perfectly now.

Thanks for pointing out my serious error!