Main Content

Get Started Communicating with the Unreal Engine Visualization Environment

R2026b

This example shows how to set up communication between Unreal Engine® and Simulink®. You create a cone actor in Unreal® Editor and use Simulink to set and get the cone location. You can set up communication with Unreal Engine by using the Simulation 3D Message Get and Simulation 3D Message Set blocks:

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 Customize 3D Scenes for Vehicle Dynamics Simulations.

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: double, single, int8, uint8, int16, uint16, int32, uint32, and Boolean. The Simulation 3D Actor Transform Set and Simulation 3D Actor Transform Get blocks can send and receive only the single data type.

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.

  • Simulation 3D Message Get receives data from an Unreal Engine environment C++ actor class. In this example workflow, you use the block to receive the cone location from Unreal Editor.

  • Simulation 3D Message Set sends data to an Unreal Engine C++ actor class. In this example, you use the block to set the initial cone location in the Unreal Editor.

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 Customize 3D Scenes for Vehicle Dynamics Simulations.

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.

Simulink model with connected blocks

Step 3: Configure Blocks

Use these block settings to configure blocks to send and receive cone data from the Unreal Editor.

BlockParameter Settings

Constant

  • Constant value[-500,10,50]

    Sets the initial cone location in the Unreal Editor coordinate system (in cm, left-handed, in Z-up coordinate system)

  • Interpret vector parameters as 1-Doff

  • Output data typesingle

Simulation 3D Scene Configuration

  • Scene SourceUnreal Editor

  • Project — Project path

    Path to project, for example the support package project.C:\Local\AutoVrtlEnv\AutoVrtlEnv.uproject

  • Open Unreal Editor — Select to open the editor

Simulation 3D Message Get

  • Signal name, SigNameConeLocGet

  • Data type, DataTypesingle

  • Message size, MsgSize[1 3]

  • Sample time-1

Simulation 3D Message Set

  • Signal name, SigNameConeLocSet

  • Sample time-1

C++ Workflow: Set Up Unreal Engine to Send and Receive Data

Step 4: Open Unreal Editor in Editor Mode

  1. Create an Unreal Engine C++ project. Name it TestSim3dGetSet. For steps on creating C++ projects, see Create Empty Project in Unreal Engine.

  2. In the Unreal Editor, on the Edit tab, select Plugins. Make sure that the MathWorks Interface plugin checkbox is selected. If it is not, select it.

  3. Close the Unreal Editor.

  4. If Visual Studio® is not open, open it.

  5. 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", "EnhancedInput", "MathWorksSimulation"}); 

  6. Save the change and close the TestSim3dGetSet project.

  7. Close Visual Studio.

  8. In your model, open the Simulation 3D Scene Configuration block.

    1. Set Project to Your_Project_path\TestSim3dGetSet.uproject.

    2. Select Open Unreal Editor.

Step 5: Create Actor Class

  1. In the Unreal Editor, on the Content Browser tab, under Settings, select Show Engine Content and Show Plugin Content.

    Unreal Editor options

    Tip

    If the Content Browser is not in the Unreal Editor layout, click Content Drawer to display Content Browser and select Dock in Layout.

  2. In the Unreal Editor, from the MathWorks Interface C++ classes directory, select Sim3dActor.

    Unreal Editor content browser

    Right-click and select Create C++ class derived from Sim3dActor.

    Unreal Editor class actions

  3. Name the new Sim3dActor SetGetActorLocation. Select Public. Click Create Class.

  4. 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:

  1. In the Solution Explorer, under the Games folder, right-click TestSim3dGetSet.

  2. Select Build.

  3. After the solution builds, open SetGetActorLocation.h. Edit the file as shown.

    This is the replacement code for SetGetActorLocation.h.

    // Copyright 2019-2022 The MathWorks, Inc.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "Sim3dActor.h"
    #include "SetGetActorLocation.generated.h"
    
    UCLASS()
    class TESTSIM3DGETSET_API ASetGetActorLocation : public ASim3dActor
    {
    	GENERATED_BODY()
    	
    	void *SignalReader;
    	void *SignalWriter;
    
    public:	
    	// Sets default values for this actor's properties
    	ASetGetActorLocation();
    
    	virtual void Sim3dSetup() override;
    	virtual void Sim3dRelease() override;
    	virtual void Sim3dStep(float DeltaSeconds) override;
    };
    

    Note

    The class name must match the project name with _API appended to the end in SetGetActorLocation.h.

Step 7: Open SetGetActorLocation.cpp

Open SetGetActorLocation.cpp and replace the block of code.

This code allows you to set a pointer to the parameter Signal Name parameter for the Simulink blocks Simulation 3D Message Set and Simulation 3D Message Get, respectively.

// Sets default values
ASetGetActorLocation::ASetGetActorLocation():SignalReader(nullptr), SignalWriter(nullptr)
{
}

The following code allows you to access the tag name of this actor after it is instantiated in the scene with an assigned tag name. The code also initializes the pointers SignalReader and SignalWriter, to initiate a link between Unreal Editor and Simulink. The variables represent these block Signal Name parameter values:

  • SignalReaderTagSimulation 3D Message Set

  • SignalWriterTagSimulation 3D Message Get

void ASetGetActorLocation::Sim3dSetup()
{
Super::Sim3dSetup();
       if (Tags.Num() != 0) {
              unsigned int numElements = 3;
              FString tagName = Tags.Top().ToString();

              FString SignalReaderTag = tagName;
              SignalReaderTag.Append(TEXT("Set"));
              SignalReader = StartSimulation3DMessageReader(TCHAR_TO_ANSI(*SignalReaderTag), sizeof(float)*numElements);

              FString SignalWriterTag = tagName;
              SignalWriterTag.Append(TEXT("Get"));
              SignalWriter = StartSimulation3DMessageWriter(TCHAR_TO_ANSI(*SignalWriterTag), sizeof(float)*numElements);
              }
}

Add this code to allow Unreal Engine to read the data value set by Simulation 3D Message Set and then write back to Simulation 3D Message Get during run time. Unreal Engine uses this data to set the location value of the actor.

void ASetGetActorLocation::Sim3dStep(float DeltaSeconds)
{
       unsigned int numElements = 3;
       float array[3];
       int statusR = ReadSimulation3DMessage(SignalReader, sizeof(float)*numElements, array);
       FVector NewLocation;
       NewLocation.X = array[0];
       NewLocation.Y = array[1];
       NewLocation.Z = array[2];
       SetActorLocation(NewLocation);
       float fvector[3] = { NewLocation.X, NewLocation.Y, NewLocation.Z };
       int statusW = WriteSimulation3DMessage(SignalWriter, sizeof(float)*numElements ,fvector);
}

Add this code so that Unreal Engine stops when you press the Simulink stop button. The code destroys the pointer SignalReader and SignalWriter.

void ASetGetActorLocation::Sim3dRelease()
{
       Super::Sim3dRelease();
       if (SignalReader) {
              StopSimulation3DMessageReader(SignalReader);
       }
       SignalReader = nullptr;     

       if (SignalWriter) {
              StopSimulation3DMessageWriter(SignalWriter);
       }
       SignalWriter = nullptr;
}

This is the entire replacement code for SetGetActorLocation.cpp.

// Copyright 2019-2022 The MathWorks, Inc.
#include "SetGetActorLocation.h"

// Sets default values
ASetGetActorLocation::ASetGetActorLocation():SignalReader(nullptr), SignalWriter(nullptr)
{
}

void ASetGetActorLocation::Sim3dSetup()
{
Super::Sim3dSetup();
       if (Tags.Num() != 0) {
              unsigned int numElements = 3;
              FString tagName = Tags.Top().ToString();

              FString SignalReaderTag = tagName;
              SignalReaderTag.Append(TEXT("Set"));
              SignalReader = StartSimulation3DMessageReader(TCHAR_TO_ANSI(*SignalReaderTag), sizeof(float)*numElements);

              FString SignalWriterTag = tagName;
              SignalWriterTag.Append(TEXT("Get"));
              SignalWriter = StartSimulation3DMessageWriter(TCHAR_TO_ANSI(*SignalWriterTag), sizeof(float)*numElements);
              }
}

void ASetGetActorLocation::Sim3dStep(float DeltaSeconds)
{
       unsigned int numElements = 3;
       float array[3];
       int statusR = ReadSimulation3DMessage(SignalReader, sizeof(float)*numElements, array);
       FVector NewLocation;
       NewLocation.X = array[0];
       NewLocation.Y = array[1];
       NewLocation.Z = array[2];
       SetActorLocation(NewLocation);
       float fvector[3] = { NewLocation.X, NewLocation.Y, NewLocation.Z };
       int statusW = WriteSimulation3DMessage(SignalWriter, sizeof(float)*numElements ,fvector);
}

void ASetGetActorLocation::Sim3dRelease()
{
       Super::Sim3dRelease();
       if (SignalReader) {
              StopSimulation3DMessageReader(SignalReader);
       }
       SignalReader = nullptr;     

       if (SignalWriter) {
              StopSimulation3DMessageWriter(SignalWriter);
       }
       SignalWriter = nullptr;
}

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 File > Save Current Level. Name the level TestMap. Add this level as the default to Project Settings by clicking Edit > Project Settings > Maps&Modes. Then, select TestMap as the default value for the Editor Startup Map and Game Default Map. To save the default values, close Project Settings.

Unreal Editor project settings

Step 9: Place and Check Actor

  1. In the Unreal Editor, find the Set Get Actor Location in the Place Actor icon, and place it in the TestMap.

    Unreal Editor Place Actors tab

  2. On the Outliner tab, check that the new instantiated actor, SetGetActorLocation, is listed.

    Unreal Editor World Outliner tab

Step 10: Add Mesh

Click the actor that you created in Step 9: Place and Check Actor.

  1. In the Details tab, click Add to add a mesh to the actor SetGetActorLocation. Choose Cone as the default mesh.

    Unreal Editor add component

  2. Find the property Tags for actor SetGetActorLocation. Add a tag by clicking the plus sign next to 0 Array elements. Name it ConeLoc.

    Unreal Editor add component

    Tip

    For Simulink to communicate with the Unreal Engine environment, the C++ code appends Get and Set 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.

Unreal Editor set cone location

Step 12: Set Parent Class and Save Scene

Set the parent class.

  1. Under Blueprint icon, click Open Level Blueprint.

    Unreal Editor blueprint

  2. Select Class Settings. In the Class Options, set Parent Class to Sim 3d Level Script Actor.

    Unreal Editor class settings

Save the Unreal Editor scene.

Step 13: Run Simulation

To 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: Open Unreal Editor in Editor Mode

  1. Create an Unreal Engine blueprint project. Name the project TestSim3dGetSet. For steps on creating blueprint projects, see Create Empty Project in Unreal Engine.

  2. 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 and restart the Unreal Editor for your changes to take effect.

  3. Close the Unreal Editor.

  4. In your model, open the Simulation 3D Scene Configuration block.

    1. Set Project to Your_Project_path\TestSim3dGetSet.uproject.

    2. Select Open Unreal Editor.

In the Unreal Editor, save the current level by clicking File > Save Current Level. Name the level TestMap. Add this level as the default to Project Settings by clicking Edit > Project Settings > Maps&Modes. Then, select TestMap as the default value for the Editor Startup Map and Game Default Map. To save the default values, close Project Settings.

Unreal Editor project settings

Step 5: 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.

  1. In the Unreal Editor, instantiate the Cone actor.

    Unreal Editor place actors tab with cone

  2. In the Details tab, set the actor Mobility property to Movable.

    Unreal Editor details tab with mobility property set to movable.

  3. In the Details tab, add the Sim3DSubscriber component to the cone. Components manage complex hierarchies and support double-precision data transmission.

    Unreal Editor Modes tab

  4. Click Sim3DSubscriber in the Details tab and specify a value for Topic name that matches the Simulation 3D Message Set block Signal name parameter value. For this example, add the topic name ConeLocSet.

    Unreal Editor Read Array Size

  5. Navigate to the level blueprint.

  6. Set the parent class.

    1. Under Blueprint icon, click Open Level Blueprint.

      Unreal Editor blueprint

    2. Select Class Settings. In the Class Options, set Parent Class to Sim 3d Level Script Actor.

      Unreal Editor class settings

    Set the game mode of the project to Sim3dGameMode. In the Unreal Editor toolbar, select Edit > Project Settings > Project > Maps & Modes. Select Sim3dGameMode.

  7. Drag the cone to the level blueprint. Right-click the graph and find the blueprint method Get Component by Class and set the Component Class as Sim3DSubscriber. Click and drag from the Return Value output. From the list of blueprint method options, find the blueprint method based on the data type and size that you want to receive from the Simulink model.

    For this example, select Read Vector Float under Sim3DSubscriber.

    Unreal Editor blueprint connections

    Note

    Depending on the number of elements you need to read, use one of these methods:

    • Read Scalar <DATA_TYPE> — Read one element.

    • Read Vector <DATA_TYPE> — Read a vector of three elements.

    • Read Array <DATA_TYPE> — Read an array of X elements.

  8. Compile and save the scene.

Step 6: Configure Scenes to Send Data

To configure scenes in the Unreal Engine environment to send data to the Simulink model:

  1. In the Unreal Editor, select the cone from the Outliner tab. In the Details tab, add the Sim3DPublisher component to the cone.

    Unreal Editor Modes tab

  2. Specify the Topic name for Sim3DPublisher that matches the Simulation 3D Message Get block Signal name parameter value. For this example, add the topic name ConeLocGet.

    Unreal Editor Read Array Size

  3. Navigate to the level blueprint.

  4. Right-click the graph and find the blueprint method Get Component by Class and set Component Class as Sim3DPublisher. Click and drag from the Return Value output. From the list of blueprint method options, find the blueprint method based on the data type and size specified by the Simulation 3D Message Get block Data type and Message size parameters.

    For this example, select Write Vector Float under Sim3DPublisher.

    Unreal Editor blueprint connections

    Note

    Depending on the number of elements you need to write, use one of these methods:

    • Write Scalar <DATA_TYPE> — Write one element.

    • Write Vector <DATA_TYPE> — Write a vector of three elements.

    • Write Array <DATA_TYPE> — Write an array of X elements.

  5. Compile and save the scene.

Step 7: Create Blueprint

In the Unreal Editor, open the level blueprint to connect the components.

In the level blueprint, make the connections, this image shows example connections:

Unreal Editor blueprint connections

Step 8: 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.

  1. 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.

  2. 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 actors and other assets in the Unreal Engine 3D environment.

  3. In the Unreal Editor, click Play. The simulation runs in the scene currently open in the Unreal Editor.

Unreal Editor Modes tab

You can also change the value of the Constant block in the Simulink model during run time to see the corresponding cone location change in the Unreal Editor environment.

You can send and receive these data types: double, single, int8, uint8, int16, uint16, int32, uint32, and boolean. The code in Step 7: Open SetGetActorLocation.cpp reads single data type values (or float values) from Simulink.

See Also

| | | | | |

Topics

External Websites