Get Started Communicating with the Unreal Engine Visualization Environment
You can set up communication with Unreal Engine® by using the Simulation 3D Message Get and Simulation 3D Message Set blocks:
Simulation 3D Message Get receives data from the Unreal Engine environment.
Simulation 3D Message Set sends data to the Unreal Engine environment.
To use the blocks and communicate with Unreal Engine, make sure you install the Vehicle Dynamics Blockset™ Interface for Unreal Engine Projects support package. For more information, see Install Support Package and Configure Environment.
Next, follow these workflow steps to set up the Simulink® model and the Unreal Engine environment and run a simulation.
Workflow | Description | |
---|---|---|
Set Up Simulink Model to Send and Receive Data | Configure the Simulation 3D Message Get and Simulation 3D Message Set blocks in Simulink to send and receive the cone location from Unreal® Editor. The steps provides the general workflow for communicating with the editor. The Simulation 3D
Message Get and Simulation 3D Message Set
blocks can send and receive these data types:
| |
Set Up Unreal Engine to Send and Receive Data | C++ Workflow: Set Up Unreal Engine to Send and Receive Data | Specific Unreal C++ workflow to send and receive Simulink cone location data.
To follow this workflow, you should be comfortable coding with C++ in Unreal Engine. Make sure that your environment meets the minimum software requirements described in Unreal Engine Simulation Environment Requirements and Limitations. |
Blueprint Workflow: Set Up Unreal Engine to Send and Receive Data | Generalized Unreal Editor blueprint workflow to send and receive Simulink data. | |
Run Simulation | After you set up the Simulink model and Unreal Editor environment, run a simulation. |
Set Up Simulink Model to Send and Receive Data
Step 1: Install Support Package
If you have already downloaded and installed Unreal Engine and the Vehicle Dynamics Blockset Interface for Unreal Engine Projects support package, go to the next step.
To install and configure the support package, see Install Support Package and Configure Environment.
Before installing the support package, make sure that your environment meets the minimum software and hardware requirements described in Unreal Engine Simulation Environment Requirements and Limitations.
Note
Make sure to launch Unreal Engine from Simulink.
Step 2: Set Up Simulink Model
Open a new Simulink model. Connect the blocks as shown.
Step 3: Configure Blocks
Use these block settings to configure blocks to send and receive cone data from the Unreal Editor.
Block | Parameter Settings |
---|---|
Constant |
|
Data Type Conversion |
|
Simulation 3D Scene Configuration |
|
Simulation 3D Message Get |
|
Simulation 3D Message Set |
|
C++ Workflow: Set Up Unreal Engine to Send and Receive Data
Step 4: Open Unreal Editor in Editor Mode
In your model, open the Simulation 3D Scene Configuration block. Select Open Unreal Editor.
Create an Unreal Engine C++ project. Name it
TestSim3dGetSet
. For steps on creating C++ projects, see Create Empty Project in Unreal Engine.In the Unreal Editor, on the Edit tab, select Plugins. Make sure that the
MathWorks Interface
plugin is enabled. If it is disabled, enable it.Close the Unreal Editor.
If Visual Studio® is not open, open it.
In Visual Studio, add the MathWorksSimulation dependency to the
TestSim3dGetSet
project build file.In the Solution Explorer, the project build file,
TestSim3dGetSet.Build.cs
, is located in this folder:...Games\TestSim3dGetSet\Source\TestSim3dGetSet
.
In the build file, edit line 11 to add the
MathWorksSimulation
dependency:PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "MathWorksSimulation"});
Save the change and close the
TestSim3dGetSet
project.Close Visual Studio.
In your model, open the Simulation 3D Scene Configuration block.
Set Project to
.Your_Project_path
\TestSim3dGetSet.uprojectSelect Open Unreal Editor.
Step 5: Create Actor Class
In the Unreal Editor, on the Content Browser tab (located in the bottom left), under Settings, select Show Engine Content and Show Plugin Content.
In the Unreal Editor, from the MathWorks Interface C++ classes directory, select Sim3dActor.
Right-click and select Create C++ class derived from Sim3dActor.
Name the new Sim3dActor
SetGetActorLocation
. Select Public. Click Create Class.Close the Unreal Editor.
Step 6: Open SetGetActorLocation.h
Visual Studio opens with new C++ files in the project folder:
SetGetActorLocation.h
SetGetActorLocation.cpp
Make sure you close the Unreal Editor.
In Visual Studio, build the solution TestSim3dGetSet
:
In the Solution Explorer, under the Games folder, right-click TestSim3dGetSet.
Select Build.
After the solution builds, open
SetGetActorLocation.h
. Edit the file as shown.
Step 7: Open SetGetActorLocation.cpp
Open SetGetActorLocation.cpp
and replace the block of
code.
Replacement Code: Set Pointer to Parameter
Replacement Code: Access Actor Tag Name
Additional Code: Read and Write Data During Run Time
Step 8: Build the Visual Studio Project and Open Unreal Editor
In Visual Studio, select Debug > Start Debugging or press F5 to run the
TestSim3dGetSet
solution. The Unreal Editor opens.
Note
In the Unreal Editor, save the current level by clicking Save
Current (located in the top left) and name it
TestMap
. Add this level as default to Project Settings by
clicking on Edit > Project Settings > Maps&Modes. Then select TestMap
as the default value for
the Editor Startup Map and Game Default Map. Close Project Settings to save the
default values.
Step 9: Place and Check Actor
In the Unreal Editor, find the Set Get Actor Location in the Place Actor tab, and place it in the
TestMap
.On the Outliner tab, check that the new instantiated actor,
SetGetActorLocation
, is listed.
Step 10: Add Mesh
Click the actor that you created in Step 9: Place and Check Actor.
In the Details panel, click Add, to add a mesh to the actor
SetGetActorLocation
. ChooseCone
as the default mesh.Find the property Tags for actor
SetGetActorLocation
. Add a tag by clicking on the plus sign next to 0 Array elements. Name itConeLoc
.Tip
For Simulink to communicate with the Unreal Engine environment, the C++ code appends
Get
andSet
to the tag name to match the Signal name parameter set in the Simulation 3D Message Get and Simulation 3D Message Set blocks.
Step 11: Set Cone Location
On the Details tab, click Cone. Set the
cone to X = 0.0
, Y = 0.0
, and Z =
0.0
. Also set the actor Mobility
property to Movable.
Step 12: Set Parent Class and Save Scene
Set the parent class.
Under Blueprints, click Open Level Blueprint, and select Class Settings.
In the Class Options, set Parent Class to
Sim 3d Level Script Actor
.
Save the Unreal Editor scene.
Step 13: Run Simulation
Run the simulation. Go to Run Simulation.
Reference: C++ Functions for Sending and Receiving Simulink Data
Call these C++ functions from Sim3dSetup
, Sim3dStep
, and Sim3dRelease
to send and receive Simulink data.
Blueprint Workflow: Set Up Unreal Engine to Send and Receive Data
Step 4: Configure Scenes to Receive Data
To use the Simulation 3D Message Set block, you must configure scenes in the Unreal Engine environment to receive data from the Simulink model:
In the Unreal Editor, instantiate the
Sim3DGet
actor that corresponds to the data type you want to receive from the Simulink model. This example shows the Unreal EditorSim3DGet
data types. SelectSim 3d Get Float
.In the Details panel, specify an actor tag name that matches the Simulation 3D Message Set block Signal name parameter.
In the Details panel, set Read Array Float Max Num Element to
3
.Navigate to the Level Blueprint.
Right-click the graph and find the blueprint method for the
Sim3DGet
actor class based on the data type and size that you want to receive from the Simulink model.For this example, the array size is 3. The unreal editor diagram shows that
Read Vector Float
is the method forSim3dGetFloat
actor class that receives float data type of array size 3.Note
Depending on the number of elements you need to read, use one of these methods:
Read Scalar <DATA_TYPE>
— To read 1 element.Read Vector <DATA_TYPE>
— To read a vector of 3 elements.Read Array <DATA_TYPE>
— To read an array of X elements.
Compile and save the scene.
Step 5: Configure Scenes to Send Data
To configure scenes in the Unreal Engine environment to send data to the Simulink model:
In the Unreal Editor, instantiate the
Sim3DSet
actor that corresponds to the data type you want to send to the Simulink model. This example shows the Unreal EditorSim3DSet
data types. SelectSim 3d Set Float
.Specify an actor tag name that matches the Simulation 3D Message Get block Signal name parameter and set Write Array Float Max Num Element to
3
.Navigate to the Level Blueprint.
Find the blueprint method for the
Sim3DSet
actor class based on the data type and size specified by the Simulation 3D Message Get block Data type and Message size parameters.For this example, the array size is 3. The Unreal Editor diagram shows that
Write Vector Float
is the method for theSim3DSetFloat
actor class that sends float data type of array size 3.Depending on the number of elements you need to write, use one of these methods:
Note
Write Scalar <DATA_TYPE>
— To write 1 element.Write Vector <DATA_TYPE>
— To write a vector of 3 elements.Write Array <DATA_TYPE>
— To write an array of X elements.
Compile and save the scene.
Step 6: Create Blueprint
In the Unreal Editor, create a level blueprint connecting the Get and Set actors.
Set the actor tag values.
Sim3dGetFloat
— Simulation 3D Message Set block Signal name, SigName parameter value, for exampleConeLocSet
Sim3dSetFloat
— Simulation 3D Message Get block Signal name, SigName parameter value, for exampleConeLocGet
Set the parent class.
Under Blueprints, click Open Level Blueprint, and select Class Settings.
In the Class Options, set Parent Class to
Sim 3d Level Script Actor
.
In the level blueprint, make the connections, for example:
Step 7: Run Simulation
Run the simulation. Go to Run Simulation.
Run Simulation
After you configure the Simulink model and Unreal Editor environment, you can run the simulation. Change the Constant value in the Simulink model during run time to see the corresponding cone location change in the Unreal Editor environment.
Note
At the BeginPlay event, Simulink does not receive data from the Unreal Editor. Simulink receives data at Tick events. In pause mode, Read Vector Float is still invoked at Tick events, but Simulink does not send messages.
Run the simulation.
In the Simulink model, click Run.
Because the source of the scenes is the project opened in the Unreal Editor, the simulation does not start.
Verify that the Diagnostic Viewer window in Simulink displays this message:
In the Simulation 3D Scene Configuration block, you set the scene source to 'Unreal Editor'. In Unreal Editor, select 'Play' to view the scene.
This message confirms that Simulink has instantiated the vehicles and other assets in the Unreal Engine 3D environment.
In the Unreal Editor, click Play. The simulation runs in the scene currently open in the Unreal Editor.
You can send and receive these data types: double
,
single
, int8
, uint8
,
int16
, uint16
, int32
,
uint32
, boolean
. The code in Step 7: Open SetGetActorLocation.cpp reads single data
type values (or float values) from Simulink.
See Also
ASim3dActor
| Sim3dSetup
| Sim3dStep
| Sim3dRelease
| Simulation 3D
Scene Configuration | Simulation 3D
Message Get | Simulation 3D
Message Set
Related Topics
- Animate Custom Actors in the Unreal Editor
- Place Cameras on Actors in the Unreal Editor
- Send and Receive Double-Lane Change Scene Data