Calling a function from a component on another component controlled object in C++?
by Dave Calabrese · in Torque Game Builder · 11/29/2009 (2:26 am) · 1 replies
NOTE: This has been duplicated from my response in the TorqueX forums at www.garagegames.com/community/forums/viewthread/56233
I'm trying to do this on the C++ side and I'm getting some funky errors. Here is how I'm setting things up... (did some searching in the forums and didn't see any responses, so hopefully I didn't miss someone already explaining this!)
First, I have a gCurrentPlayer object which is an extern so I can easily use it from anywhere in the engine, as almost all the game code hinges off of it. So at the top of another component that I want to access function from the player component, I do:
And of course, gCurrentPlayer is assigned in the playerComponent.cpp file.
Now is where I get a little stuck. I can see 2 ways to do this, and I'm having problems with both. The first is of course just to call functions on the gCurrentPlayer object...
As one might expect, this complains that doMyFunkyFunctions() is not a method in t2dSceneObject. Ok, that makes sense. So I try and grab the behavior from the gCurrentPlayer object so I can do things with that..
And this gives me the error of "error C2275: 'playerComponent' : illegal use of this type as an expression"
So, how does one call methods on a component controlled object from inside another component on the C++ side?
I'm trying to do this on the C++ side and I'm getting some funky errors. Here is how I'm setting things up... (did some searching in the forums and didn't see any responses, so hopefully I didn't miss someone already explaining this!)
First, I have a gCurrentPlayer object which is an extern so I can easily use it from anywhere in the engine, as almost all the game code hinges off of it. So at the top of another component that I want to access function from the player component, I do:
extern t2dSceneObject* gCurrentPlayer;
And of course, gCurrentPlayer is assigned in the playerComponent.cpp file.
Now is where I get a little stuck. I can see 2 ways to do this, and I'm having problems with both. The first is of course just to call functions on the gCurrentPlayer object...
gCurrentPlayer->doMyFunkyFunctions();
As one might expect, this complains that doMyFunkyFunctions() is not a method in t2dSceneObject. Ok, that makes sense. So I try and grab the behavior from the gCurrentPlayer object so I can do things with that..
playerComponent* activePlayer = gCurrentPlayer->getComponent<playerComponent>();
And this gives me the error of "error C2275: 'playerComponent' : illegal use of this type as an expression"
So, how does one call methods on a component controlled object from inside another component on the C++ side?
About the author
Associate Phillip O'Shea
Violent Tulip
// Add this to the SimComponent class declaration (SimComponent.h). template<class T> T* getComponent( void ); // Add this to SimComponent.cpp template<class T> T* SimComponent::getComponent( void ) { for( SimComponentIterator nItr = mComponentList.begin(); nItr != mComponentList.end(); nItr++ ) { // Cast to Template. T *component = dynamic_cast<T*>( *nItr ); // Valid? if ( component ) { // Return Component. return component; } } // No Component of Specified Type. return NULL; }I've not tested it out, but it looks like it should work.