TorqueScript Quick Reference 3
From TDN
Contents |
Engine Interfacing
|
The engine provides a powerful set of tools to expose the core engine functionality and structures in the console (to TorqueScript). In addition, usage of these console functions and classes sometimes involves enumerated types. The following problem-solution matrix summarizes tasks relating to the Console and how to accomplish them. |
| Need to… | Solution |
| Expose Member as Field. | addField() addFieldV() |
| Expose named Member as Field. | addNamedField() addNamedFieldV() |
| Expose/Remove global C++ Variable or static Member as Local Variable | Con::addVariable() Con::removeVariable() |
| Create Console Function from C++ | ConsoleFunction() |
| Create a Console Class's Method from C++. | ConsoleMethod() |
| Create a Console Class from C++ | IMPLEMENT_CONOBJECT() |
| Create a Console Datablock from C++ | IMPLEMENT_CO_DATABLOCK() |
| Invoke a Console Callback Method from C++ | Con::executef( this, ... ) |
| Invoke a Console Callback Function from C++ | Con::executef( ... ) |
| Create a Enumerated Data Type from C++ | EnumTable::Enums |
|
[edit] addField |
||||||||||||||||||||||||||||||||
|
Purpose Provides a means to expose C++ class members as TorqueScript object fields. This function is normally called in a class’ initPersistFields() method. Syntax
where,
class GuiControl : public SimGroup
{
...
StringTableEntry mClassName;
StringTableEntry mSuperClassName;
...
}
In ./gui/core/guiControl.cc:
void GuiControl::initPersistFields()
{
...
addField( "class", TypeString, Offset( mClassName, GuiControl),
"Script Class of object." ) ;
addField( "superclass", TypeString, Offset( mSuperClassName, GuiControl),
"Script SuperClass of object." ) ;
...
}
See Also |
|
[edit] addFieldV |
|||||||||||||
|
Purpose Augments addField with ‘Type Validator’ classes. Type Validator Classes are used to restrict the values a field may be given. If illegal values are assigned to a validated field, the validator function will print an error to the console and set the field to a valid value. The two most commonly used Type Validator Classes are FRangeValidator and IRangeValidator. New validators may be derived from either of these, or their base class TypeValidator. Note: addFieldV does not support arrays or enumerated types.
where
Example: Field lightRadius
addFieldV( "lightRadius",
TypeF32,
ProjectileData,
new FRangeValidator(1, 20) );
See Also |
|
[edit] addNamedField |
|
Purpose
The addNamedField is a macro that makes use of the simple version addField function and provides a short-hand method of adding fields. It does not support enumerated types or arrays. Also, it does not provide a means of adding documentation strings. It is worth noting that the macro uses the same name for the field as the variable. addNamedField( fieldname , type , className ) ;
addNamedField( isBallistic , TypeBool , ProjectileData ); See Also |
|
[edit] addNamedFieldV |
|
Purpose
The addNamedField is a macro that makes use of the addFieldV function and provides a short-hand method of adding validated fields. addNamedFieldV( fieldname , type, className, validator );
addNamedFieldV( lightRadius,
TypeF32,
ProjectileData,
new FRangeValidator( 1 , 20 ) );
See Also |
|
[edit] addVariable |
|
Purpose
The Con::addVariable() function provides a means to expose global C++ variables or static class Members as Torque Script global variables. This function is normally called in a class’ consoleInit () method. Con::addVariable( const char * name , t , void * dp );
// From camera.cc Con::addVariable( "Camera::movementSpeed" , TypeF32 , &mMovementSpeed ); See Also |
|
[edit] removeVariable |
|
Purpose
The Con::removeVariable function provides a means to un-expose global C++ variables or static class Members previously exposed as Torque Script global variables with Con::addVariable. removeField( const char* in_pFieldname );
// 1. TerrainBlock is inherited from SceneObject. // 2. SceneObject links member mObjToWorld to the // Torque Script field position. // 3. TerrainBlock undoes this(in terrData.cc) removeField( "position" ); See Also |
|
[edit] ConsoleFunction |
|
Purpose
The ConsoleFunction macro provides a means to create function from C++ in the console.
ConsoleFunction( name , returnType , minArgs , maxArgs , usage )
{
// Function body
}
// From main.cc
ConsoleFunction( getSimTime , S32 , 1 , 1,
"getSimTime() – Time since game started.")
{
return Sim::getCurrentTime();
}
See Also |
|
[edit] ConsoleMethod |
|
Purpose
The ConsoleMethod macro provides a means to create a Console Method from C++ in the console. The static variant of ConsoleMethod is for methods that you want to be able to call statically. For example:, GameConnection::getServerConnection().
ConsoleMethod(
className , scriptname , returnType ,
minArgs , maxArgs , usage
)
{
// Method body
}
or
ConsoleStaticMethod(
className , scriptname , returnType ,
minArgs , maxArgs , usage )
{
// Method body
}
From SimBase.cc:
ConsoleMethod( SimObject , getId , S32 , 2 , 2 , "obj.getId()" )
{
argc; argv;
return object->getId();
}
See Also |
|
[edit] IMPLEMENT_CONOBJECT |
|
Purpose The C++ macro-instruction used to define a Console Class. In TGB, there are over 170 such classes. In ./gui/core/guiControl.cc, the console class GuiControl is defined: IMPLEMENT_CONOBJECT(GuiControl); See Also |
|
[edit] IMPLEMENT_CO_DATABLOCK |
|
Purpose The C++ macro-instruction used to define a Console Datablock. In TGB, there are about a dozen such classes. In ./source/T2D/t2dBaseDatablock.cc, the console datablock t2dBaseDatablock is defined: IMPLEMENT_CO_DATABLOCK_V1(t2dBaseDatablock); See Also |
|
[edit] executef |
|
Purpose Class method Con::executef() invokes a console callback function. In .game/net/serverQuery.cc, the console function onServerQueryStatus is invoked:
static void processMasterServerQuery( U32 session )
{
...
Con::executef( 4, "onServerQueryStatus", "update", "Switching master servers...", "0" );
...
}
See Also |
|
[edit] executef(this) |
|
Purpose Class method Con::executef(this...) invokes a console class's callback method.
bool GuiControl::onWake( )
{
...
if ( isMethod( "onWake" ) )
Con::executef( this, 1, "onWake" ) ;
...
}
See Also |
|
[edit] EnumTable::Enums |
|
Purpose Declare a new static variable in the EnumTable::Enums scope for use as an enumerated data type. For example, in ./gui/containers/guiStackCtrl.cc:
static EnumTable::Enums stackTypeEnum[] =
{
{ GuiStackControl::stackingTypeVert, "Vertical" },
{ GuiStackControl::stackingTypeHoriz,"Horizontal" },
{ GuiStackControl::stackingTypeDyn,"Dynamic" }
};
static EnumTable gStackTypeTable(3, &stackTypeEnum[0]);
Note that employing the table in the console requires further work; consult the source code. |



