Custom object
by Steven Hine · in Torque Game Builder · 05/28/2008 (3:54 am) · 6 replies
Is it possible to create a custom object not derived from an exsisting object? Something that is exposed so you can use it when you need it. Kind of like the echo(); function.
About the author
#2
You don't have to have an instance of a class you can do this...
Things to note here are the subtle differences in the argument indexes for passed parameters and the definition of argument min/max counts in the "ConsoleMethod" and "ConsoleFunction" definitions.
Also I added the "OnAdd" and "OnRemove" in which I don't implement anything. These are virtual hooks that are used when the object gets added/removed from the simulation and are very handy places to setup your state. I would recommend not implementing anything in the constructor as you will find the engine iterates the classes and constructs objects during initialisation. Funky things can happen if you're doing anything other than basic state resetting.
You can find more information in a post I made back in 2002 (ooh now I feel old) here. It may be slightly outdated but it still provide valuable insight.
Hope this helps,
Melv.
05/28/2008 (6:52 am)
Here's a quick example I put together for you that demonstrates most things to get you started:#ifndef _SIMBASE_H_
#include "console/simBase.h"
#endif
// Simple Math Object Example.
class SimpleMath : public SimObject
{
// Handy Parent Reference.
typedef SimObject Parent;
public:
SimpleMath();
~SimpleMath();
// SimObject Hooks.
virtual bool OnAdd();
virtual void OnRemove();
// Our Simple Math Function.
S32 calculateSum( const S32 a, const S32 b );
// Declare Class as a Console Object.
DECLARE_CONOBJECT(SimpleMath);
};
// Implement the Console Object Code.
IMPLEMENT_CONOBJECT(SimpleMath);
// Standard C'tor.
SimpleMath::SimpleMath()
{
}
// Standard D'tor.
SimpleMath::~SimpleMath()
{
}
// Called when adding to the simulation.
bool SimpleMath::OnAdd()
{
if ( !Parent::onAdd() )
{
return false;
}
return true;
}
// Called when removing from the simulation.
void SimpleMath::OnRemove()
{
Parent::OnRemove();
}
// Returns the sum of two integers.
S32 SimpleMath::calculateSum( const S32 a, const S32 b )
{
return a + b;
}
// Class Based Summate Method.
ConsoleMethod( SimpleMath, calculateSum, S32, 4, 4, "( valueA , valueB) Returns the sum of valueA and valueB.\n"
"@param valueA First value to be summed.\n"
"@param valueB Second value to be summed.\n"
"@return Sum of valueA + valueB\n" )
{
argc;
return object->calculateSum( dAtoi(argv[2]), dAtoi(argv[3]) );
}... compile this and you can then do the following in the scripts...%simpleMath = new SimpleMath(); echo( "MathHelper:" SPC %simpleMath.calculateSum( 1000, 500 ) ); %simpleMath.delete();... which should output "1500".
You don't have to have an instance of a class you can do this...
// Static Based Summate Method.
ConsoleFunction( calculateSumStatic, S32, 3, 3, "( valueA , valueB) Returns the sum of valueA and valueB.\n"
"@param valueA First value to be summed.\n"
"@param valueB Second value to be summed.\n"
"@return Sum of valueA + valueB\n" )
{
return dAtoi(argv[1]) + dAtoi(argv[2]);
}... compile this and you can then do the following in the scripts...echo( "Static MathHelper:" SPC calculateSumStatic( 2000, 500 ) );... which should output "2500".
Things to note here are the subtle differences in the argument indexes for passed parameters and the definition of argument min/max counts in the "ConsoleMethod" and "ConsoleFunction" definitions.
Also I added the "OnAdd" and "OnRemove" in which I don't implement anything. These are virtual hooks that are used when the object gets added/removed from the simulation and are very handy places to setup your state. I would recommend not implementing anything in the constructor as you will find the engine iterates the classes and constructs objects during initialisation. Funky things can happen if you're doing anything other than basic state resetting.
You can find more information in a post I made back in 2002 (ooh now I feel old) here. It may be slightly outdated but it still provide valuable insight.
Hope this helps,
Melv.
#3
05/28/2008 (7:24 pm)
Thanks Melv, this is exactly what I'm looking for. I can't wait to dive into some ideas I have...
#4
Keep us updated on your progress! :)
Melv.
05/29/2008 (2:39 am)
You're welcome. I really enjoyed myself when I first starting going through this stuff back in the day. Opens you eyes to so many possibilities.Keep us updated on your progress! :)
Melv.
#5
Can I split the code into .h and .cpp after the class definition?
Steve
06/08/2008 (5:37 pm)
Melv,Can I split the code into .h and .cpp after the class definition?
Steve
#6
06/08/2008 (5:52 pm)
Never mind I figured it out. It splits after the IMPLEMENT_CONOBJECT(SimpleMath);
Associate Phillip O'Shea
Violent Tulip