No cookie for

VR-Forces 4.6: User-Defined State Properties

Posted by on VR-Forces

Simulation objects in VR-Forces have many state properties, such as speed, heading, altitude, force, and so on. You can set many of these properties using set data requests. In past releases, if you wanted to add a new type of state property, you had to use the VR-Forces Toolkit to write a plug-in or update the application. VR-Forces 4.6 lets you add new state properties without writing code. 

How can this feature help you? Suppose you have a scenario in which ground vehicles need to travel from one location to another. There are several routes they can follow. One route can only be traveled by smaller wheeled vehicles. A second can be traveled by large wheeled vehicles, defined as any vehicles with more than four wheels. A third can be traveled by tracked vehicles.

To make this all work, we need to know if a vehicle has wheels, and if so, how many. The state properties provided by VR-Forces can’t tell you this information. The configuration and code snippets in this tech tip show how you can add this information to VR-Forces and use it.

To add a new state property, you add a statement to the OPE file for the object type you are updating. The following example adds two state properties – hasWheels and  numWheels:

(state-data

 (DtRwBoolean hasWheels $has-wheels publish)

 (DtRwInt numWheels $number-of-wheels (default "4") publish)

)

These properties are bound to variables, which lets us add them to the Simulation Object Editor. They will be published, so any application that cares about them can use that information. numWheels is set to a default of 4.

If you want to see the value for a state property in an Information dialog box, you can add an informationitem to the DtEntityInformationPage pageitem element in ./appData/settings/vrfGui/default_guiConfiguration.gui. Here is what our example properties would look like:

<informationitem informationitemname="DtStatePropertyPageItem:hasWheels:Has Wheels"/>

<informationitem informationitemname="DtStatePropertyPageItem:numWheels:Number of Wheels"/>

 

Of course, no values will be displayed unless you set them. You can set a state property in the Simulation Object Editor or using a Scripted Set  lua script. Here is the metadata for a script that uses our properties:

Here is a script that checks to see if a vehicle has wheels and prints the results to the console. Then it reads the input from the Set Wheels dialog box for these properties and prints the new state property status to the console:

require "vrfutil"

function init()

      vrf:setTickWhilePaused(true)

-- get the current values for the state properties and print to console

      local haswheels = this:getStateProperty("hasWheels")

      local numwheels = this:getStateProperty("numWheels")

      if haswheels then

print("This vehicle has wheels.")

print("Number of wheels is: ", numwheels)

      else

-- if no wheels print this instead

print("This vehicle does not have wheels.")

      end

 

      finished = 0

      vrf:setTickPeriod(0.5)

end

function tick()

-- this happens after we complete the Set Wheels dialog box and click OK

-- get the haswheels value from the dbx and set the state property

      haswheels = setParameters.hasWheels

      this:setStateProperty("hasWheels", haswheels)

-- get the number of wheels from the dbx and set the state property

      wheels = setParameters.numberOfWheels

      this:setStateProperty("numWheels", wheels)

 

-- get the property values and print the results to the object console.

      newhaswheels = this:getStateProperty("hasWheels")

      newnumwheels = this:getStateProperty("numWheels")

 

      if newhaswheels then

            print("This vehicle has wheels.")

            print("Number of wheels is: ", newnumwheels)

      else

            print("This vehicle does not have wheels.")

      end

      finished = 1

 

      if finished == 1 then

            vrf:endSet()

      end

end

 

Now lets write a scripted task that uses these new state properties. Assume that the scenario has three routes – Tracked, Heavy, and Light, that lead to the destination (Base). Our task, Move to Base, gets the hasWheels and numWheels state properties from the selected entity and based on these values gives a Move Along Route task to the entity using the appropriate route. There is no input to the task, so the New Scripted Task dialog box just sets up the name of the task and puts it on the menu.

Here is the Lua script:

require "vrfutil"

function init()

   moveAlongRouteSubtaskID = -1

 

-- get the properties

   haswheels = this:getStateProperty("hasWheels")

   numwheels = this:getStateProperty("numWheels")

   vrf:setTickPeriod(0.5)

end

 

function tick()

if moveAlongRouteSubtaskID == -1 then

-- see if the vehicle has wheels. If yes, give proper move along task

-- based on the number of wheels

   if haswheels then

      if numwheels <= 4 then

         moveAlongRouteSubtaskID =
           vrf:startSubtask("move-along",{route = "Light"} )

      end

      if numwheels > 4 then

         moveAlongRouteSubtaskID =
           vrf:startSubtask("move-along",{route = "Heavy"} )

      end

   -- if no wheels, assume tracked and send on tracked route.

      else

         moveAlongRouteSubtaskID =
           vrf:startSubtask("move-along",{route = "Tracked"} )

      end

   end

 

   if vrf:isSubtaskComplete(moveAlongRouteSubtaskID) then

vrf:endTask(true)

   end

end

 

For complete details about creating user-defined state properties, adding them to the Simulation Object Editor, and using them in a task, see Section 73.6 “State Properties and Parameter Properties”, in VR-Forces Users Guide.

Note: When you add a user-defined state property, the VR-Forces GUI and simulation components do not know anything about them unless you write code to integrate them. They are only actionable through Lua scripting.

Last modified on
Trackback URL for this blog entry.

Comments

  • No comments made yet. Be the first to submit a comment

Leave your comment

Guest
Guest 16 July 2019