Data Descriptions: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (added russian otherlang)
(cleanup)
Line 1: Line 1:
The <i>data description</i> table is a repository of definitions for a class’s data and function members that allows the engine to link, save/restore and otherwise understand the intention of those members. Failure to define these members in the table can lead to unexpected results at runtime.
A '''data description''' table tells Source the purpose of each function listed in it. Anyone familiar with web technology will recognise it as [[Wikipedia:Metadata|metadata]].


Table elements are declared inside of a <code>BEGIN_DATADESC</code> and <code>END_DATADESC</code> code block. Any number of the following macros can be placed within the table as needed, like so:
In practice, tables are most often used is to define what internal information about an entity should be stored when the game is saved.


<pre>
== Usage ==
BEGIN_DATADESC( CMyClass )


  DEFINE_FIELD( . . . )
Table elements are declared between <code>BEGIN_DATADESC</code> and <code>END_DATADESC</code>:


END_DATADESC()
BEGIN_DATADESC( CMyClass )
</pre>
    DEFINE_FIELD( ... ),
    DEFINE_FIELD( ... ),
END_DATADESC()


The following is a list of the most commonly used table element types.
Note the absence of semicolons. Datadesc tables must be declared in the constructor with <code>DECLARE_DATADESC();</code>.


=DEFINE_FIELD=
== Element types ==
 
===DEFINE_FIELD===


This macro definition is used for saving and loading in the engine. Any member variable properly defined in this macro will be saved and restored automatically. The macro is declared as:
This macro definition is used for saving and loading in the engine. Any member variable properly defined in this macro will be saved and restored automatically. The macro is declared as:


<pre>
DEFINE_FIELD( variableName, variableType )
DEFINE_FIELD( variableName, variableType )
</pre>


The <code>dataMemberType</code> parameter can be any of the following types:
The <code>variableType</code> parameter can be any of the following types:


{|
;FIELD_VOID:No type or value (used for function parameters)
| <code>FIELD_VOID</code> || No type or value (used for function parameters)
;FIELD_FLOAT:Any floating point value
|-
;FIELD_STRING:A string ID (return from ALLOC_STRING)
| <code>FIELD_FLOAT</code> || Any floating point value
;FIELD_VECTOR:Any vector, QAngle, or AngularImpulse
|-
;FIELD_QUATERNION:A quaternion value
| <code>FIELD_STRING</code> || A string ID (return from ALLOC_STRING)
;FIELD_INTEGER:Any integer or enum value
|-
;FIELD_BOOLEAN:Boolean value (represented as an integer)
| <code>FIELD_VECTOR</code> || Any vector, QAngle, or AngularImpulse
;FIELD_SHORT:2 byte integer
|-
;FIELD_CHARACTER:One byte
| <code>FIELD_QUATERNION</code> || A quaternion value
;FIELD_COLOR32:8-bit per channel [R,G,B,A] (32-bit color)
|-
;FIELD_EMBEDDED:An embedded object with a data description, recursively traverse and embedded | class/structure based on anadditional type description
| <code>FIELD_INTEGER</code> || Any integer or enum value
;FIELD_CUSTOM:A special type that contains function pointers to its read/write/parse functions
|-
;FIELD_CLASSPTR:CBaseEntity pointer
| <code>FIELD_BOOLEAN</code> || Boolean value (represented as an integer)
;FIELD_EHANDLE:Entity handle
|-
;FIELD_EDICT:edict_t pointer
| <code>FIELD_SHORT</code> || 2 byte integer
;FIELD_POSITION_VECTOR:A world coordinate value, which is fixed up across level-transitions automatically
|-
;FIELD_TIME:A floating point time value, which is fixed up across level-transitions automatically
| <code>FIELD_CHARACTER</code> || One byte
;FIELD_TICK:An integer tick count, which is fixed up similarly to FIELD_TIME
|-
;FIELD_MODELNAME:Engine string that is a model name (Must be precached)
| <code>FIELD_COLOR32</code> || 8-bit per channel [R,G,B,A] (32-bit color)
;FIELD_SOUNDNAME:Engine string that is a sound name (Must be precached)
|-
;FIELD_INPUT:A list of inputted data fields, all derived from CMultiInputVar
| <code>FIELD_EMBEDDED</code> || An embedded object with a data description, recursively traverse and embedded | class/structure based on anadditional type description
;FIELD_FUNCTION:A class function pointer (Think, Use, etc.)
|-
;FIELD_VMATRIX:A VMatrix {{note|Output coordinates are NOT worldspace}}
| <code>FIELD_CUSTOM</code> || A special type that contains function pointers to its read/write/parse functions
;FIELD_VMATRIX_WORLDSPACE:A VMatrix that maps some localspace to worldspace (translation is fixed up on level-transitions)
|-
;FIELD_MATRIX3X4_WORLDSPACE:matrix3x4_t that maps some localspace to worldspace (translation is fixed up on level-transitions)
| <code>FIELD_CLASSPTR</code> || CBaseEntity pointer
;FIELD_INTERVAL:A start and range floating point interval ( e.g., 3.2->3.6 == 3.2 and 0.4 )
|-
;FIELD_MODELINDEX:A model index
| <code>FIELD_EHANDLE</code> || Entity handle
;FIELD_MATERIALINDEX:A material index (using the material precache string table)
|-
| <code>FIELD_EDICT</code> || edict_t pointer
|-
| <code>FIELD_POSITION_VECTOR</code> || A world coordinate value, which is fixed up across level-transitions automatically
|-
| <code>FIELD_TIME</code> || A floating point time value, which is fixed up across level-transitions automatically
|-
| <code>FIELD_TICK</code> || An integer tick count, which is fixed up similarly to FIELD_TIME
|-
| <code>FIELD_MODELNAME</code> || Engine string that is a model name (Must be precached)
|-
| <code>FIELD_SOUNDNAME</code> || Engine string that is a sound name (Must be precached)
|-
| <code>FIELD_INPUT</code> || A list of inputted data fields, all derived from CMultiInputVar
|-
| <code>FIELD_FUNCTION</code> || A class function pointer (Think, Use, etc.)
|-
| <code>FIELD_VMATRIX</code> || A VMatrix {{note|Output coordinates are NOT worldspace}}
|-
| <code>FIELD_VMATRIX_WORLDSPACE</code> || A VMatrix that maps some localspace to worldspace (translation is fixed up on level-transitions)
|-
| <code>FIELD_MATRIX3X4_WORLDSPACE</code> || matrix3x4_t that maps some localspace to worldspace (translation is fixed up on level-transitions)
|-
| <code>FIELD_INTERVAL</code> || A start and range floating point interval ( e.g., 3.2->3.6 == 3.2 and 0.4 )
|-
| <code>FIELD_MODELINDEX</code> || A model index
|-
| <code>FIELD_MATERIALINDEX</code> || A material index (using the material precache string table)
|}


=DEFINE_KEYFIELD=
===DEFINE_KEYFIELD===


This macro works in the same manner as <code>DEFINE_FIELD</code> with regards to saving and restoring data members. It extends this functionality by also defining a reference name for the data member which links it to the keyvalue identifier defined in the FGD entry for an entity class. Once this is properly declared, the data member will be initialized to the value defined in the map upon that entity’s creation.
This macro works in the same manner as <code>DEFINE_FIELD</code> with regards to saving and restoring data members. It extends this functionality by also defining a reference name for the data member which links it to the keyvalue identifier defined in the FGD entry for an entity class. Once this is properly declared, the data member will be initialized to the value defined in the map upon that entity’s creation.


<pre>
DEFINE_KEYFIELD( variableName, variableType, "keyvalueName" )
DEFINE_KEYFIELD( variableName, variableType, "keyvalueName" )
</pre>


As an example, the following definition would link the <code>m_bEnabled</code> data member to the '''"enabled"''' keyvalue identifier in Hammer.
As an example, the following definition would link the <code>m_bEnabled</code> data member to the '''"enabled"''' keyvalue identifier in Hammer.


<pre>
DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" )
DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" )
</pre>


{{note|The keyvalue’s identifying name is case insensitive.}}
{{note|The keyvalue’s identifying name is case insensitive.}}


=DEFINE_OUTPUT=
===DEFINE_OUTPUT===


This macro is used to link an output event to a named identifier used by Hammer. It is defined as:
This macro is used to link an output event to a named identifier used by Hammer. It is defined as:


<pre>
DEFINE_OUTPUT( outputVariable, outputName )
DEFINE_OUTPUT( outputVariable, outputName )
</pre>


{{note|The outputEventVariable in this case must be of type COuputEvent.}}
{{note|The outputEventVariable in this case must be of type COuputEvent.}}


=DEFINE_INPUTFUNC=
===DEFINE_INPUTFUNC===


This macro is used to link named inputs from Hammer to functions in the engine. This also defines the type of parameter being passed in to the function from the Entity I/O system. The macro is defined as:
This macro is used to link named inputs from Hammer to functions in the engine. This also defines the type of parameter being passed in to the function from the Entity I/O system. The macro is defined as:


<pre>
DEFINE_INPUTFUNC( parameterType, "inputName", InputFunction )
DEFINE_INPUTFUNC( parameterType, "inputName", InputFunction )
</pre>


The <code>parameterType</code> can be any of the following FIELD_ types:
The <code>parameterType</code> can be any of the following FIELD_ types:


<code>FIELD_VOID</code>
*<code>FIELD_VOID</code>
 
*<code>FIELD_INTEGER</code>
<code>FIELD_INTEGER</code>
*<code>FIELD_FLOAT</code>
 
*<code>FIELD_STRING</code>
<code>FIELD_FLOAT</code>
*<code>FIELD_VECTOR</code>
 
*<code>FIELD_COLOR32</code>
<code>FIELD_STRING</code>
*<code>FIELD_BOOLEAN</code>


<code>FIELD_VECTOR</code>
===DEFINE_INPUT===
 
<code>FIELD_COLOR32</code>
 
<code>FIELD_BOOLEAN</code>
 
=DEFINE_INPUT=


This macro is a shorthand version of <code>DEFINE_INPUTFUNC</code>. It automatically sets the described data member to the parameter passed in from the <code>Input()</code> function. This bypasses the need to create an input function whose sole purpose is to set a data member to a passed value. The macro is declared as:
This macro is a shorthand version of <code>DEFINE_INPUTFUNC</code>. It automatically sets the described data member to the parameter passed in from the <code>Input()</code> function. This bypasses the need to create an input function whose sole purpose is to set a data member to a passed value. The macro is declared as:


<pre>
DEFINE_INPUT( variableName, variableType, "keyvalueName" )
DEFINE_INPUT( variableName, variableType, "keyvalueName" )
</pre>


=DEFINE_ARRAY AND DEFINE_AUTO_ARRAY=
===DEFINE_ARRAY AND DEFINE_AUTO_ARRAY===


As their names suggest, these macros deal with saving and restoring [[array]] values. The number of elements in the [[array]] must be declared when using <code>DEFINE_ARRAY</code>, whereas <code>DEFINE_AUTO_ARRAY</code> will cause the code to automatically determine the size of the array at runtime.
As their names suggest, these macros deal with saving and restoring [[array]] values. The number of elements in the [[array]] must be declared when using <code>DEFINE_ARRAY</code>, whereas <code>DEFINE_AUTO_ARRAY</code> will cause the code to automatically determine the size of the array at runtime.
Line 145: Line 103:
The macros are declared as:
The macros are declared as:


<pre>
DEFINE_ARRAY( variable, variableType, numElements )
DEFINE_ARRAY( variable, variableType, numElements )
</pre>
<pre>
DEFINE_AUTO_ARRAY( variable, variableType )
</pre>


=DEFINE_CUSTOM_FIELD=
DEFINE_AUTO_ARRAY( variable, variableType )
 
===DEFINE_CUSTOM_FIELD===


This macro allows custom interpretation of a variable with regards to how it is saved and restored. By passing in the reference to a handler class, the user is able to handle serialization of the data entirely.
This macro allows custom interpretation of a variable with regards to how it is saved and restored. By passing in the reference to a handler class, the user is able to handle serialization of the data entirely.
Line 160: Line 115:
For more information, see uses of <code>CClassPtrSaveRestoreOps</code> within the code base.
For more information, see uses of <code>CClassPtrSaveRestoreOps</code> within the code base.


=DEFINE_THINKFUNC=
===DEFINE_THINKFUNC===


Entities using a custom think function must declare that function via this macro declaration.
Entities using a custom think function must declare that function via this macro declaration.
Line 168: Line 123:
  typedef void (*ThinkFunc)( void);
  typedef void (*ThinkFunc)( void);


=DEFINE_ENTITYFUNC=
===DEFINE_ENTITYFUNC===


Entities using a custom touch function must declare that function via this macro declaration.
Entities using a custom touch function must declare that function via this macro declaration.


Touch functions must be of the type:
Touch functions must be of the type:
  typedef void (CBaseEntity::*ENTITYFUNCPTR)(CBaseEntity *pOther );
  typedef void (CBaseEntity::*ENTITYFUNCPTR)(CBaseEntity *pOther );


=DEFINE_USEFUNC=
===DEFINE_USEFUNC===


Entities using a custom use function must declare that function via this macro declaration.
Entities using a custom use function must declare that function via this macro declaration.


Use functions must be of the type:
Use functions must be of the type:
  typedef void (*UseFunc)(
  typedef void (*UseFunc)(
  CBaseEntity *pActivator,
  CBaseEntity *pActivator,
Line 186: Line 143:
  float value
  float value
  );
  );
=Others=
 
===Others===
*dlls\simtimer.h
*dlls\simtimer.h
**DEFINE_SIMTIMER - Define a [[SimTimer]]
**DEFINE_SIMTIMER - Define a [[SimTimer]]

Revision as of 08:41, 23 February 2008

A data description table tells Source the purpose of each function listed in it. Anyone familiar with web technology will recognise it as metadata.

In practice, tables are most often used is to define what internal information about an entity should be stored when the game is saved.

Usage

Table elements are declared between BEGIN_DATADESC and END_DATADESC:

BEGIN_DATADESC( CMyClass )

   DEFINE_FIELD( ... ),
   DEFINE_FIELD( ... ),

END_DATADESC()

Note the absence of semicolons. Datadesc tables must be declared in the constructor with DECLARE_DATADESC();.

Element types

DEFINE_FIELD

This macro definition is used for saving and loading in the engine. Any member variable properly defined in this macro will be saved and restored automatically. The macro is declared as:

DEFINE_FIELD( variableName, variableType )

The variableType parameter can be any of the following types:

FIELD_VOID
No type or value (used for function parameters)
FIELD_FLOAT
Any floating point value
FIELD_STRING
A string ID (return from ALLOC_STRING)
FIELD_VECTOR
Any vector, QAngle, or AngularImpulse
FIELD_QUATERNION
A quaternion value
FIELD_INTEGER
Any integer or enum value
FIELD_BOOLEAN
Boolean value (represented as an integer)
FIELD_SHORT
2 byte integer
FIELD_CHARACTER
One byte
FIELD_COLOR32
8-bit per channel [R,G,B,A] (32-bit color)
FIELD_EMBEDDED
An embedded object with a data description, recursively traverse and embedded | class/structure based on anadditional type description
FIELD_CUSTOM
A special type that contains function pointers to its read/write/parse functions
FIELD_CLASSPTR
CBaseEntity pointer
FIELD_EHANDLE
Entity handle
FIELD_EDICT
edict_t pointer
FIELD_POSITION_VECTOR
A world coordinate value, which is fixed up across level-transitions automatically
FIELD_TIME
A floating point time value, which is fixed up across level-transitions automatically
FIELD_TICK
An integer tick count, which is fixed up similarly to FIELD_TIME
FIELD_MODELNAME
Engine string that is a model name (Must be precached)
FIELD_SOUNDNAME
Engine string that is a sound name (Must be precached)
FIELD_INPUT
A list of inputted data fields, all derived from CMultiInputVar
FIELD_FUNCTION
A class function pointer (Think, Use, etc.)
FIELD_VMATRIX
A VMatrix
Note.pngNote:Output coordinates are NOT worldspace
FIELD_VMATRIX_WORLDSPACE
A VMatrix that maps some localspace to worldspace (translation is fixed up on level-transitions)
FIELD_MATRIX3X4_WORLDSPACE
matrix3x4_t that maps some localspace to worldspace (translation is fixed up on level-transitions)
FIELD_INTERVAL
A start and range floating point interval ( e.g., 3.2->3.6 == 3.2 and 0.4 )
FIELD_MODELINDEX
A model index
FIELD_MATERIALINDEX
A material index (using the material precache string table)

DEFINE_KEYFIELD

This macro works in the same manner as DEFINE_FIELD with regards to saving and restoring data members. It extends this functionality by also defining a reference name for the data member which links it to the keyvalue identifier defined in the FGD entry for an entity class. Once this is properly declared, the data member will be initialized to the value defined in the map upon that entity’s creation.

DEFINE_KEYFIELD( variableName, variableType, "keyvalueName" )

As an example, the following definition would link the m_bEnabled data member to the "enabled" keyvalue identifier in Hammer.

DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" )
Note.pngNote:The keyvalue’s identifying name is case insensitive.

DEFINE_OUTPUT

This macro is used to link an output event to a named identifier used by Hammer. It is defined as:

DEFINE_OUTPUT( outputVariable, outputName )
Note.pngNote:The outputEventVariable in this case must be of type COuputEvent.

DEFINE_INPUTFUNC

This macro is used to link named inputs from Hammer to functions in the engine. This also defines the type of parameter being passed in to the function from the Entity I/O system. The macro is defined as:

DEFINE_INPUTFUNC( parameterType, "inputName", InputFunction )

The parameterType can be any of the following FIELD_ types:

  • FIELD_VOID
  • FIELD_INTEGER
  • FIELD_FLOAT
  • FIELD_STRING
  • FIELD_VECTOR
  • FIELD_COLOR32
  • FIELD_BOOLEAN

DEFINE_INPUT

This macro is a shorthand version of DEFINE_INPUTFUNC. It automatically sets the described data member to the parameter passed in from the Input() function. This bypasses the need to create an input function whose sole purpose is to set a data member to a passed value. The macro is declared as:

DEFINE_INPUT( variableName, variableType, "keyvalueName" )

DEFINE_ARRAY AND DEFINE_AUTO_ARRAY

As their names suggest, these macros deal with saving and restoring array values. The number of elements in the array must be declared when using DEFINE_ARRAY, whereas DEFINE_AUTO_ARRAY will cause the code to automatically determine the size of the array at runtime.

The macros are declared as:

DEFINE_ARRAY( variable, variableType, numElements )
DEFINE_AUTO_ARRAY( variable, variableType )

DEFINE_CUSTOM_FIELD

This macro allows custom interpretation of a variable with regards to how it is saved and restored. By passing in the reference to a handler class, the user is able to handle serialization of the data entirely.

The handler class must descend from the CClassPtrSaveRestoreOps class; it uses the Save() and Restore() functions for serialization.

For more information, see uses of CClassPtrSaveRestoreOps within the code base.

DEFINE_THINKFUNC

Entities using a custom think function must declare that function via this macro declaration.

Think functions must be of the type:

typedef void (*ThinkFunc)( void);

DEFINE_ENTITYFUNC

Entities using a custom touch function must declare that function via this macro declaration.

Touch functions must be of the type:

typedef void (CBaseEntity::*ENTITYFUNCPTR)(CBaseEntity *pOther );

DEFINE_USEFUNC

Entities using a custom use function must declare that function via this macro declaration.

Use functions must be of the type:

typedef void (*UseFunc)(
	CBaseEntity *pActivator,
	CBaseEntity *pCaller,
	USE_TYPE useType,
	float value
);

Others

  • dlls\simtimer.h
  • game_shared\physics_saverestore.h
    • DEFINE_PHYSPTR - Define a physics pointer
    • DEFINE_PHYSPTR_ARRAY - Define a physics pointer array
  • game_shared\saverestore_bitstring.h
  • game_shared\saverestore_utlmap.h
  • game_shared\saverestore_utlrbtree.h
  • game_shared\saverestore_utlvector.h
  • game_shared\soundenvelope.h
    • DEFINE_SOUNDPATCH
  • public\saverestore.h
    • DEFINE_KEYFIELD_NOT_SAVED
    • DEFINE_ENTITY_FIELD
    • DEFINE_ENTITY_GLOBAL_FIELD
    • DEFINE_GLOBAL_FIELD
    • DEFINE_GLOBAL_KEYFIELD
    • DEFINE_CUSTOM_FIELD
    • DEFINE_CUSTOM_KEYFIELD
    • DEFINE_AUTO_ARRAY2D
    • DEFINE_EMBEDDED
    • DEFINE_EMBEDDED_OVERRIDE
    • DEFINE_EMBEDDEDBYREF
    • DEFINE_EMBEDDED_ARRAY
    • DEFINE_EMBEDDED_AUTO_ARRAY
    • DEFINE_PRED_TYPEDESCRIPTION
    • DEFINE_PRED_TYPEDESCRIPTION_PTR
    • DEFINE_PRED_FIELD
    • DEFINE_PRED_ARRAY
    • DEFINE_FIELD_NAME
    • DEFINE_PRED_FIELD_TOL
    • DEFINE_PRED_ARRAY_TOL
    • DEFINE_FIELD_NAME_TOL
  • public\stdstring.h
    • DEFINE_STDSTRING

Template:Otherlang:en Template:Otherlang:en:ru