How to create a simple object in C++ with UE4?

Hi. I’m trying to create a simple object in C++.
It’s not an entity from the world, just a class with variables and functions which I have in the the player character.
But I needed to make it as a Pawn so I can access it from the blueprints.

This should be so simple, but I can’t find the way to do it.
I have the declaration in the header as this:

APLInput * _testObject;

And then the problem is in the creation: I tryed different ways in the player’s constructor and all of them have errors:

_testObject = new APLInput ();
or
_testObject = APLInput ();
or
_testObject = new APLInput::StaticClass();
or
_testObject = APLInput::StaticClass();

So, how should I create it?

Thank you.

If you are trying to create an new AActor you must Spawn the actor itself. Spawning will put the actor physically in the level you are in. The following is an example:

FActorSpawnParameters SpawnInfo;
// Spawninfo has additional info you might want to modify such as the name of the spawned actor.
AMyActor* MyActor = GWorld->SpawnActor<ACloudCubeNative>(ACloudCubeNative::StaicClass(), SpawnPosition, FRotator::ZeroRotator, SpawnInfo);
if (MyActor != NULL)
{
    // Actor has been spawned in the level
}
else
{
    // Failed to spawn actor!
}

If in turn you would like to create a new UObject class you must construct the class itself:

// For older UE versions (Pre 4.9) you may use `ConstructObject` instead
UMyObject* MyObject = NewObject<UMyObject>(UMyObject::StaticClass());

if (MyObject != NULL)
{
    // Object has been created!
}
else
{
    // Failed to create object
}

If you would like to just create a new instance of a class that is neither an AActor or an UObject you use the standard new/delete keywords as in C++:

// Case 1: Single pointer
MyClass * pt = new MyClass;
delete pt;

// Case 2: Array of pointers
MyClass * pt;
pt = new MyClass[3];
delete[] pt;

Just as a side note, there are many different overload methods for SpawnActor and Construct object. I have posted the most common cases but you should check the API to what you can use and what not.

Cheers,
Moss

Thanks for the answer, Moss.

I actually ended using the UActorComponent so I can use it directly from the blueprint.
But all your information is more than usefull :slight_smile:

Thanks again!

Remember to accept the answer ^^ The Answerhub unresolves it when adding a comment :smiley:

Oh sure! I’m doing it now :slight_smile:

Note that ConstructObject is deprecated in UE4.8, use NewObject instead. In addition, the class argument has been made optional, and is set to T::StaticClass by default, so you can just use NewObject< UMyObject >().

(I cannot type < something > without spaces, it seems to turn into a tag and gets automatically removed. Didn’t find how to escape < and > either.)

thanks Moss
but now a little different about option 2
‘ConstructObject’: ConstructObject is deprecated. Use NewObject instead Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.

<something> is typed like this: &lt;something&gt;. Also, to type an & you have to use &amp;. Try to figure out the complete text of my comment as practice :smiley: