CPP code to connect Unreal to Matlab
A server is created on the Unreal side with variables that can be accessed from Unreal via local variable structure, and from Matlab through a socket. Communication with socket is included.
This project's code is available on GitHub.
C/C++ compiler for MATLAB:
-
Microsoft Visual Studio 2015+ (recommended for Windows)
-
GCC, available from MinGW
Run ConnectionProject\ConnectionProject.uproject
in your UE4. Run matlab_client.mexw64
in MATLAB.
Create new classes in your project with names same to classes in ConnectionProject\Source\ConnectionProject\Private.
Replace the code within them with code from ConnectionProject classes, changing the CONNECTIONPROJECT_API to YOURPROJECT_API in each header file.
Add these libraries to your YourProject.Build.cs in ConnectionProject\Source\ConnectionProject
:
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { "Networking", "Sockets" });
Run the src/makeClient.m from MATLAB IDE. It should create matlab_client.mexw64 file.
First start UE server, then connect/disconnect from MATLAB.
All of the communication goes via ConnectionActorUberStorage
class instance. Just spawn it wherever you need it and pass it to the server functions.
To start the server, run startServer
command from a blueprint. To kill the server, run shutDownServer
.
To send the data, update the ConnectionActorUberStorageInstance.variableOutArray
, and pass the Storage to updateOutDataAfterWriting
. Don't forget to update array's length. To receive the data, run updateInDataBeforeReading
and read it from ConnectionActorUberStorageInstance.variableInArray
.
Use matlab_client('help')
. matlab_client('connect')
connects to the server. Avoid running without UE server on. matlab_client('close')
closes connection. 'request_data'
and 'send_data'
allow you to request and send data to the server.
mjDataReturnTime
and mjData
both have two fields, n
- length and dat
- data.
Add the Plugins folder (located in the project ConnectionProject2 folder) and place it in the root folder of your project. Compile the project files and it is ready to be used. Information about pluggins located: https://docs.unrealengine.com/en-us/Programming/Plugins An example of creation is here: https://wiki.unrealengine.com/An_Introduction_to_UE4_Plugins
The project uses code from matlab_c_sockets repository.
Special thanks to Rama for multi-threading tutorial.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.