The Add Event example demonstrates the following:
- How to add new system event to communicate between the sim and the network threads in a thread-safe manner.
- How to send a new interaction type from the sim engine.
- How to receive a new interaction type in the sim engine.
Adding a New Event
The VR-Forces simulation threads do not have direct access to the network. In order for a sim component to signal to the network thread that a system event has occurred, it must use the DtEventManager class to pass a subclass of DtEvent. Similarly, the same mechanism is used by the network threads to signal to the sim that a system event was received on the network. Each type of event has an event translator class that knows how to translate the DtEvent between the threads.
This example creates a template class that can be templated on any subclass of the VR-Link DtInteraction class to allow the sim to send and receive any interaction type that is not already supported. The example uses the Event Report interaction to demonstrate. It contains the following template classes.
- DtInteractionEvent - A template class based on an interaction and a unique event id. It stores the interaction to be passed between the network and simulation.
- DtVrlinkInteractionTranslator - A template class to handle passing the event between the sim and network threads and vice versa.
It also registers the following functions.
- receivedEventReport - Processes new Event Report interaction events by printing a message to the console.
- objectAddedCallback - Called for the first lifeform entity created on this sim engine. Registers the checkForKia function.
- checkForKia - Called after every tick to check if the first lifeform entity has been destroyed. If so, sends a new Event Report.
Usage
In the Launcher:
-
Set the addEvent plugin to load.
-
Start VR-Forces.
-
Start a second instance of the VR-Forces sim engine with a different application ID.
To view the new behavior:
-
Create a new scenario
-
Create a new lifeform entity
-
Select the entity and Set Destroyed
-
Look at the consoles of both sim enginges. They should both print the message "Received KIA event report for <object name>"
#include <vl/exerciseConn.h>
#include <vl/eventReportInteraction.h>
void objectAddedCallback(
const DtLocalObject* simObject,
void* usr);
void checkForKia(void* usr);
extern "C" {
{
}
{
if (vrlinkInterface)
{
vrlinkInterface->
addEventTranslator(EventReportEventId,
new DtEventReportInteractionTranslator);
}
return true;
}
{
DtObjectType(1, 3, -1, -1, -1, -1, -1, -1));
return true;
}
{
if (vrlinkInterface)
{
}
}
}
{
const DtEventReportInteractionEvent* eventReport =
static_cast<const DtEventReportInteractionEvent*>(&event);
if (eventReport->interaction().eventType() == DtEventKilledInAction)
{
if (obj)
{
DtWarn <<
"Received KIA event report for " << obj->
objectName() << std::endl;
}
else
{
DtWarn << "Received KIA event report from an unknown entity" << std::endl;
}
}
}
void objectAddedCallback(
const DtLocalObject* simObject,
void* usr)
{
UuidOfObjectToMonitor = simObject->
uuid();
}
void checkForKia(void* usr)
{
{
DtEventReportInteractionEvent event;
event.setSenderId(obj->
uuid());
DtEventReportInteraction inter;
inter.setEventType(DtEventKilledInAction);
event.setInteraction(inter);
}
}