Getting some external C++ code to be used and work in Unreal Engine 4.0

Not being familiar with Unreal Engine or any Game engine, And just starting out, I have just spent two days reading a lot of the online documentation and other sources, and watching a dozen videos or so to get grips with how UE4 works to assess If I can use it to represent data nodes of a data structure I am experimenting with in a 3D graphical form as UE4 actors within a 3d environment instead of a 2d environment similar to how Blueprints displays nodes for UE4. My assessment is that I am confident it can be done and Unreal is capable to meet my needs to do this, as all I need to do is display text coupled with a mesh to act as a 3d Widget, and to use the GUI interface to conduct menu choices to initiate a event to perform a certain task that is defined in these non UE4 classes. I feel am getting a grasp on how everything works and how to put some basic things together which I can build on quite quickly.

So I have created my first C++ actor class with a couple of added components. A modified box mesh and text. As a test to see if I can modify the text defined by the text component from another class, I have creating a very simple normal C++ class with a text variable and constructor to initiate that text variable. Now I had hoped and thought there would be a very simple procedure to have the C++ .h file included in an UE 4 .h file and/or .cpp file if I could not include without modification, and more so that there would be extensive documentation or tutorials available. Alas, this does not seem to be the case after a 5 hour ordeal of searching. The best I could Come up with is this link, Taking a plain C++ Program into Unreal - C++ Gameplay Programming - Unreal Engine Forums and doing what was said there just does not work. VS2015 reports that it cannot open the “test_code.h” file even though it exists in the same directory as the EU class .h and .cpp files created by UE.

So I ask the question, If any one can help me. Can UE4 use native C++ code that is created outside of UE4, and if so, what are the tasks I have to do, or the extra code needed to include to do so?

I have read so many conflicting things about modifying the .cs and .uproject files, and adding other code, I am not sure what really needs to be done and I don’t want to fiddle with these, not knowing what I am doing, as again I could find little information. The trivial code I am using to perform this test is below. Thank you in advance for any help.

      ****** My test code ***********

test_code.h

#ifndef TEST_CODE_H
#define TEST_CODE_H

class test_class{

public:
char *test_string = “default_test_string”;

test_class() {
	test_string = "constructor_test_string";
}

~test_class() {
}

private:
};
#endif

test_code.cpp

#include “AllowWindowsPlatformTypes.h” // does not work ???
#include “test_code.h”
#include “HideWindowsPlatformTypes.h”

   ******** UE4 code*****************

Node_display_element.h

#pragma once
// My stuff

#include “AllowWindowsPlatformTypes.h” // Does not work
#include “test_code.h” // VS2015 message : Cannot open source file
#include “HideWindowsPlatformTypes.h”
//
#include “GameFramework/Actor.h”
#include “Node_display_element.generated.h”

UCLASS()
class NODE_EDITOR_API ANode_display_element : public AActor
{
GENERATED_BODY()

public:
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “My test”) // One forum post said do this but does not work
test_class tc; //** VS2015 Message identifier test class is undefined**

// Sets default values for this actor's properties
ANode_display_element();

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

// Called every frame
virtual void Tick( float DeltaSeconds ) override;

// following two lines prevents the variable from being reset when the game is launched, 
//or when the project or level is closed and reloaded.
UPROPERTY(EditAnywhere)
	USceneComponent* OurVisibleComponent;

};

Node_display_element.cpp

#include “Node_editor.h”
#include “Node_display_element.h”

// Sets default values
ANode_display_element::ANode_display_element()
{
// 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;

// Our root component will be a box that reacts to physics
UBoxComponent* BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
RootComponent = BoxComponent;
BoxComponent->InitBoxExtent({10.0f,40.0f,40.0f }); // box dimentions x, y, z
BoxComponent->SetCollisionProfileName(TEXT("Node Body"));

// Create and position a mesh component so we can see where our sphere is
UStaticMeshComponent* BoxVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
BoxVisual->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> BoxVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
if (BoxVisualAsset.Succeeded())
{
	BoxVisual->SetStaticMesh(BoxVisualAsset.Object);
	BoxVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -3.5f)); // relative location to the root location
	BoxVisual->SetRelativeScale3D({ 0.3f,0.8f,0.075f });        // box scale of rootcomponent x, y, z
	//BoxVisual->SetMaterial(10);

// BoxVisual->SetWorldScale3D(FVector(0.1f)); // Scale of the box **** Do not use with SetRelativeScale3D ***
}

// UBoxComponent* BoxVisual = NewObject <UBoxComponent>(this); // for spawning new objects ?

UTextRenderComponent* TextVisual = CreateDefaultSubobject&lt;UTextRenderComponent&gt;(TEXT("TextRepresentation"));
TextVisual-&gt;AttachTo(RootComponent);

TextVisual-&gt;SetVisibility(true);
TextVisual-&gt;SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);
TextVisual-&gt;VerticalAlignment = EVerticalTextAligment::EVRTA_TextCenter;
TextVisual-&gt;SetText((FString) &tc.test_string); //*******************  Test displaying normal CPP class string  **********
//TextVisual-&gt;SetText("TEST");
TextVisual-&gt;SetRelativeLocation(FVector(-15.0f, 0.0f, 0.0f)); // relative location to the root location
TextVisual-&gt;SetWorldScale3D(FVector(0.4f));                  // Scale of the box
TextVisual-&gt;SetRelativeRotation({ 0.0f,0.0f,180.0f,0.0f });  // Rotation of text x, y, z axis W ?
TextVisual-&gt;TextRenderColor = { 255 ,0 ,0 ,0};               // text color Red, Green, Blue, alpha

}

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

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

*********************** SOLVED *******************************

On a brainstorm, looking at the .h & .cpp files, I concluded that one cannot just add code to the project but one must go through the editor to create all the .h and .cpp files and have it “registered” by the engine. I had a look at the panel to create a c++ class and lo and behold, there was a selection to create a blank c++ class.

I selected it and let UE4 do its thing.

A new class was created, I put the above test code in and substituted the char* with a TString and wham. it worked.

I have no idea why I need to do this as I do not yet know what is going on in the background, but this fundamental procedure is not mentioned or documented anywhere that is prominent and I could find. I am also finding many things, fundamental things that are not documented in a concise, clear manner and with good coding examples for C++. The C++ coding Wiki looks disoraganised and the topic headers not very concise. Maybe its me but my first impressions of UE having good documentation is in decline. The API documentation may be extensive, but like a car or a watch, one cannot put it together without clear instructions if they did not build it. This probably because UE is Blueprints and editor centric which don’t need such in depth knowledge.

As I go on my journey with UE4 I hope I will not find too many more humps like this one that take hours to sort out. Eg like that mentioned in my code above. it took 2 hours to figure out that SetWorldScale3D does not work with SetRelativeScale3D because my thinking was that I first scale a cube into a rectangle, and then that rectangle further scaled to a particular size, not that the first scale in the order is ignored if a second scale was present similar to setting a variable. And I suspect this will be the same with many other functions. I will now keep that in mind.

Well, any way. Back to my assessment and experimentation on UE4. It looks like there is a lot more stuff under the hood I am yet to discover.

All the best.

DominioN