![]() |
VR-Forces 5.0.2 Developer's Guide
|
The State Repository Extension example demonstrates how to install a state respository extension to an existing state repository.
It does so by creating a subclass of the DtVrfStateRepositoryUserExtension class in order to provide additional functionality to an existing state repository.
Instances of DerivedSRUserExtension are an example of an extension to a base state repository class. It provides a way to extend an entity's state information independent at a lower level in the state repository hierarchy. All entities in VR-Forces have at a minimum a DtVrfObjectStateRepository, which can be extended in this way. By extending a state repository through the DtVrfStateRepostioryUserExtension class, you don't have to extend multiple leaf-level state repository classes to add the same kind of extension to different kinds of entities (e.g. ground vehicles and air entities, both of which have different derived state repository classes).
For example, suppose you want to extend all types of entities and objects in VR-Forces (fixed-wing, rotary-wing, ground, and so on) with a new kind of identifier. Rather than extending each type of state repository to include the new identifier, you just need to create a single derived kind of DtVrfStateRepositoryUserExtension class. This allows you to write the extension once, and configure it as part of a variety of different kinds of entities, control objects, or overlay objects. The alternative to this approach would be to subclass each kind of state repository separately, extending each class in exactly the same way.
A state repository extension object (DtVrfStateRepositoryUserExtension) can be initialized from a parameter database, and then saved/restored as part of an entity's state in an order of battle file.
This example of a user-defined state repository extensions class contains a single new data member, myTag (a new kind of identifier). The myTag member variable is a readable, writeable string (DtRwString). myTag is registered with the name "my-tag" in this class' constructor.
Like the DtVrfObjectStateRepository classes, classes derived from DtVrfStateRepository- UserExtension are readable and writeable. Any DtReaderWriter data members you add to your derived DtVrfStateRepositoryUserExtension will be saved to an order of battle file along with the rest of an object's DtVrfObjectStateRepository data.
The class returns a new type string "my-state-repository-user-extension". This is the key that gets registered with the factory so VR-Forces will know how to create an instance of this class when it encounters it in a parameter database file. The configuration of an instance of this type of state repository extension in a parameter database file (an .ope file) would look something like this:
(state-repository-extension-type "my-state-repository-user-extension")
(user-extension
(my-tag "some-useful-info")
)
At runtime, you can access the state repository extension through the entity's state repository member, e.g.
DtVrfObject* entity; DtVrfStateRepositoryUserExtension* extendedState; . . . extendedState = entity->vrfState()->userExtension();
To extend state repositories at the base level:
cgf->factoryManager()->stateRepositoryUserExtensionFactory()-> addCreatorFcn("default-vrf-state-repository-user-extension", DerivedSRUserExtension::create);
(M1A2 (parameter-type "ground-vehicle-param") (object-type 1 (1 1 225 1 1 3 -1)) . . . (state-repository-extension-type "my-state-repository-user-extension") )
(state-repository-extension-type "my-state-repository-user-extension") (user-extension (my-tag "some-initial-value") )
Access the extension as needed in your code. You can obtain a pointer to the variable through the object's state repository. Use dynamic_cast to determine the correct type.
This example demonstrates how extend the entity state repository at the at the DtVrfObjectStateRepository level.
In the Launcher, click the Plug-ins button to bring up the Plug-ins Selection dialog. Enable the plugin.
To view the new behavior:
| DerivedSRUserExtension | This subclass of DtVrfStateRepositoryUserExtension provides extra data to the base state repository. |