![]() |
VR-Forces 4.1 Class Documentation
|
This subclass of the DtMovingObjectParameters adds a new parameter (maximum hull temperature) to our new entity
Now that we have a new state repository class for the space entity (DtSpaceEntityStateRepository ), we need to create an class to hold parameters specific to space entities. An instance of this object parameters class will be created by the state repository and used to initialize itself. This enables the entity to be configured with the values specified in the object parameters database.
Each state repository class creates a new instance of a DtVrfObjectParameters object in its createParameters() method. The derivation of the "parameters" class must parallel the derivation of the "state repository" class. For instance, in this case, DtSpaceEntityStateRepository is derived from DtVrfMovingObjectStateRepository. Since DtVrfMovingObjectStateRepository::createParameters() instantiates a DtMovingObjectParameters object, DtSpaceEntityParameters must be derived from DtMovingObjectParameters. DtSpaceEntityStateRepostiory overrides createParameters() to create an instance of this new class.
As in the new state repository class, the new parameters class needs to override the constructor, type(), and creator() member functions from its parent.
The parameter class constructor sets up the object as read/ write-capable. The parameters for the object can then be read in from the object parameters database.
DtSpaceEntityParameters::DtSpaceEntityParameters(const DtString& name, DtReaderWriterRegistry* parentRegistry) : DtMovingObjectParameters(name, parentRegistry), myMaxHullTemperature("max-hull-temperature", 1260.0, &myRegistry) { }
The type() function returns a string that uniquely identifies the parameter class. It is used as a key in the object parameters factory. Object parameter types are defined in vrfParamTypes.h. All strings must be unique, so care must be taken to ensure that strings returned by new type() functions do not conflict with existing types.
const char DtSpaceVehicleParamType[] = "space-param"; DtString DtSpaceEntityParameters::type() const { return DtSpaceVehicleParamType; }
The static creator() function is registered with the object parameters factory. It should return a newly created instance of your derived class.
DtVrfObjectParameters* DtSpaceEntityParameters::creator() { return new DtSpaceEntityParameters; }
The creator() function must be registered with the DtSpaceEntityParameters type with the state repository factory.
We are adding one readable, writable member variable to the class – myMaxHullTemperature. To make the variable readable and writable, we must choose a type of member variable derived from DtReaderWriter and register the data member with our parameters class. Since it conceptually represents a continuous real quantity, we chose to make myMaxHullTemperature a DtRwReal, which is a readable, writable double precision floating point number (there are numerous readable, writable classes for representing basic data types).
To make the myMaxHullTemperature data member capable of being read from a file, we need to register it with the parameters class. This is done in the constructor:
DtSpaceEntityParameters::DtSpaceEntityParameters(const DtString& name, DtReaderWriterRegistry* parentRegistry) : DtMovingObjectParameters(name, parentRegistry), myMaxHullTemperature("max-hull-temperature", 1260.0, &myRegistry) { }
Initialization of myMaxHullTemperature in this manner ensures that when the parameter "max-hull-temperature" is encountered in the object parameters database for this entity, the corresponding value can be stored in the myMaxHullTemperature member variable.
For example, a new space entity parameter entry might look like the following:
(new-space-entity
(parameter-type "space-param")
(object-type 1 (1 5 0 0 0 0 0))
(max-hull-temperature 1500)
. . .
)
The parameter-type variable indicates that the kind of parameters class to be created for this object is of type DtSpaceEntityParameters (the same type as returned by its type() member function). The max-hull-temperature parameter is used to initialize the myMaxHullTemperature member variable to 1500 C.
In the DtSpaceEntityStateRepository class, we need to override the createParameters()member to instantiate DtSpaceEntityParameters.
DtBoolean DtSpaceEntityStateRepository::createParameters() { if (myParameters) { delete myParameters; myParameters = NULL; } myParameters = new DtSpaceEntityParameters("parameters"); if (myParameters == NULL) { return DtFalse; } return DtTrue; }
Override initializeFromParameters() to initialize our new data member, myMaxHullTemperature from our new parameters object. Make sure to call down to the base class to ensure that the rest of the object is initialized. This initializes the myMaxHullTemperature member with any values specified for the parameter in the object parameters database.
void DtSpaceEntityStateRepository::initializeFromParameters() { DtVrfMovingObjectStateRepository::initializeFromParameters(); myMaxHullTemperature = spaceEntityParameters()->maxHullTemperature(); }