Ue4 - matlab tcp api

So i am trying to do a MATLAB to UE4/C++ TCP connection which i can send data and use the data in UE4 to do things like gettin actor location and rotation, changing actor location and rotation, taking screenshot etc. I’ve installed " GitHub - getnamo/TCP-Unreal: Convenience TCP wrapper for Unreal Engine 4 " as a plugin and all i can do is add TCPServer as component to the actor and i really don’t know how to use the data that is coming from my MATLAB client code. I also made a blueprint map for my actor so on Event BeginPlay the view switches to my actor so i can take a screenshot from the actor i’m sending data to.

MyActor.h

[SPOILER] // Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “MyActor.generated.h”

UCLASS()
class TCP_CLR_TSS_API AMyActor : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
AMyActor();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

};
[/SPOILER]

MyActor.cpp

[SPOILER]// Fill out your copyright notice in the Description page of Project Settings.

#include “MyActor.h”
#include “Engine.h”
#include “UnrealClient.h”

// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

FVector NewLocation = GetActorLocation();

FRotator NewRotation = GetActorRotation();

NewLocation.X = -180;
NewLocation.Y = 160;
NewLocation.Z = 200;
SetActorLocation(NewLocation);

NewRotation.Roll += 0.0f;
NewRotation.Pitch += 0.0f;
NewRotation.Yaw = 0;
SetActorRotation(NewRotation);

GEngine->GameViewport->Viewport->TakeHighResScreenShot();

}[/SPOILER]

Client.m

[SPOILER] t = tcpip(‘127.0.0.1’, 3000, ‘NetworkRole’, ‘client’);
disp(‘Connecting’);
fopen(t);

n = 1;

prompt = 'Enter Data: ';
prompt3 = 'Enter location in the format X,Y,Z: ';
prompt4 = 'Enter rotation in the format X,Y,Z: ';
prompt5 = 'Enter the desired name for the screenshot: ';

con0 = ‘Stop’;
con1 = ‘1’;
con2 = ‘2’;
con3 = ‘3’;
con4 = ‘4’;
con5 = ‘5’;

while n>0
disp(‘1- Get Camera Location’)
disp(‘2- Get Camera Rotation’)
disp(‘3- Change Camera Location’)
disp(‘4- Change Camera Rotation’)
disp(‘5- Take Screenshot’)
data = input(prompt, ‘s’);
fprintf(’
');

cond0 = strcmpi(data,con0);
cond1 = strcmpi(data,con1);
cond2 = strcmpi(data,con2);
cond3 = strcmpi(data,con3);
cond4 = strcmpi(data,con4);
cond5 = strcmpi(data,con5);


if (cond0 == 1)
    fprintf('

');
disp(‘Disconnected’)
fclose(t);
break
else
end

if (cond1 == 1)
    fwrite(t, data);
    pause(2)
    newData = fread(t);
    newData2 = char(newData);
    fprintf('%s', newData2);
    fprintf('

‘);
fprintf(’
‘);
end
if (cond2 == 1)
fwrite(t, data);
pause(2)
newData = fread(t);
newData2 = char(newData);
fprintf(’%s’, newData2);
fprintf(’
‘);
fprintf(’
‘);
end
if (cond3 == 1)
fwrite(t, data);
data3 = input(prompt3, ‘s’);
dataArray = strsplit(data3,’,');
fwrite(t, data3);
xVal = dataArray(1);
xx = char(xVal);
yVal = dataArray(2);
yy = char(yVal);
zVal = dataArray(3);
zz = char(zVal);
fprintf('Location of Camera Set to: ');
fprintf('X = ‘);
fprintf(’%s, ', xx);
fprintf('Y = ‘);
fprintf(’%s, ', yy);
fprintf(‘Z = ‘);
fprintf(’%s ‘, zz);
fprintf(’
‘);
fprintf(’
‘);
end
if (cond4 == 1)
fwrite(t, data);
data4 = input(prompt4, ‘s’);
dataArray = strsplit(data4,’,’);
fwrite(t, data4);

    xVal = dataArray(1);
    xx = char(xVal);
    yVal = dataArray(2);
    yy = char(yVal);
    zVal = dataArray(3);
    zz = char(zVal);
    fprintf('Rotation of Camera Set to: ');
    fprintf('X = ');
    fprintf('%s, ', xx);
    fprintf('Y = ');
    fprintf('%s, ', yy);
    fprintf('Z = ');
    fprintf('%s ', zz);
    fprintf('

‘);
fprintf(’
‘);
end
if (cond5 == 1)
fwrite(t, data);
data5 = input(prompt5, ‘s’);
fwrite(t, data5);
fprintf(’
');
end
end[/SPOILER]

TCPServerComponent.h

[SPOILER]#pragma once

#include “Components/ActorComponent.h”
#include “Networking.h”
#include “Runtime/Sockets/Public/IPAddress.h”
#include “TCPServerComponent.generated.h”

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTCPEventSignature);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTCPMessageSignature, const TArray<uint8>&, Bytes);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FTCPClientSignature, const FString&, Client);

UCLASS(ClassGroup = “Networking”, meta = (BlueprintSpawnableComponent))
class TCPWRAPPER_API UTCPServerComponent : public UActorComponent
{
GENERATED_UCLASS_BODY()
public:

//Async events

/** On message received on the receiving socket. */
UPROPERTY(BlueprintAssignable, Category = "TCP Events")
FTCPMessageSignature OnReceivedBytes;

/** Callback when we start listening on the TCP receive socket*/
UPROPERTY(BlueprintAssignable, Category = "TCP Events")
FTCPEventSignature OnListenBegin;

/** Called after receiving socket has been closed. */
UPROPERTY(BlueprintAssignable, Category = "TCP Events")
FTCPEventSignature OnListenEnd;

/** Callback when we start listening on the TCP receive socket*/
UPROPERTY(BlueprintAssignable, Category = "TCP Events")
FTCPClientSignature OnClientConnected;

/** Default connection port e.g. 3001*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP Connection Properties")
int32 ListenPort;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP Connection Properties")
FString ListenSocketName;

/** in bytes */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP Connection Properties")
int32 BufferMaxSize;

/** If true will auto-listen on begin play to port specified for receiving TCP messages. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP Connection Properties")
bool bShouldAutoListen;

/** Whether we should process our data on the game thread or the TCP thread. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TCP Connection Properties")
bool bReceiveDataOnGameThread;

UPROPERTY(BlueprintReadOnly, Category = "TCP Connection Properties")
bool bIsConnected;

/** 
* Start listening at given port for TCP messages. Will auto-listen on begin play by default
*/
UFUNCTION(BlueprintCallable, Category = "TCP Functions")
void StartListenServer(const int32 InListenPort = 3001);

/**
* Close the receiving socket. This is usually automatically done on end play.
*/
UFUNCTION(BlueprintCallable, Category = "TCP Functions")
void StopListenServer();

/**
* Emit specified bytes to the TCP channel.
*
* @param Message    Bytes
*/
UFUNCTION(BlueprintCallable, Category = "TCP Functions")
void Emit(const TArray&lt;uint8&gt;& Bytes, const FString& ToClient = TEXT("All"));

virtual void InitializeComponent() override;
virtual void UninitializeComponent() override;
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

protected:
TArray<FSocket*> Clients;
FSocket* ListenSocket;
FThreadSafeBool bShouldListen;
TFuture<void> ServerFinishedFuture;

FString SocketDescription;
TSharedPtr&lt;FInternetAddr&gt; RemoteAdress;

};[/SPOILER]

I also don’t know how i can create conditions/events from c++ or blueprints such as:

[SPOILER]If data == ‘5’ { GEngine->GameViewport->Viewport->TakeHighResScreenShot(); }[/SPOILER]

Or events from c++ such as

[SPOILER]// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();

}[/SPOILER]

This is how i set the view to the Actor Component Camera:
[SPOILER]


[/SPOILER]

These are the TCPServer events which don’t work properly:

[SPOILER]
TCPServer-Events.png
[/SPOILER]

Hey! I know this post is from 3 years ago, but who knows… Did you solve this? I’d like to get the same pipeline as the one of yours… I actually would like to get frames back into simulink/matlab server. At the moment I’m exploring websockets and tcp, I hope this is something you considered as well…

Hey scalabro12
did you fine a solution? I try to do it now for unreal engine 5.

did you solve this?.. ㅠㅠ