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

**URL:** <https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554>\
**Category:** Blueprint\
**Tags:** unreal-engine, Blueprint, CPP, question\
**Created:** [October 7, 2014, 11:46pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554 "2014-10-07T23:46:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasRusjan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tobiasrusjan/32/1004445_2.png) [@TobiasRusjan](https://forums.unrealengine.com/u/TobiasRusjan)\
**Post date:** [October 7, 2014, 11:46pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/1 "2014-10-07T23:46:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [October 8, 2014, 7:40am UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/2 "2014-10-08T07:40:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TobiasRusjan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tobiasrusjan/32/1004445_2.png) [@TobiasRusjan](https://forums.unrealengine.com/u/TobiasRusjan)\
**Post date:** [October 8, 2014, 2:04pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/3 "2014-10-08T14:04:05Z")

</div>

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 🙂

Thanks again!

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [October 8, 2014, 2:08pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/4 "2014-10-08T14:08:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TobiasRusjan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tobiasrusjan/32/1004445_2.png) [@TobiasRusjan](https://forums.unrealengine.com/u/TobiasRusjan)\
**Post date:** [October 8, 2014, 2:10pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/5 "2014-10-08T14:10:28Z")

</div>

Oh sure! I’m doing it now 🙂

---

<div class="post-metadata">

**Author:** ![huulong](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@huulong](https://forums.unrealengine.com/u/huulong)\
**Post date:** [September 6, 2015, 5:12pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/6 "2015-09-06T17:12:08Z")

</div>

Note that ConstructObject is deprecated in UE4.8, use [NewObject](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/Creation/index.html) 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.)

---

<div class="post-metadata">

**Author:** ![kenshin1987](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kenshin1987](https://forums.unrealengine.com/u/kenshin1987)\
**Post date:** [November 14, 2016, 6:52am UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/7 "2016-11-14T06:52:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Arshia001](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Arshia001](https://forums.unrealengine.com/u/Arshia001)\
**Post date:** [July 25, 2017, 1:58pm UTC](https://forums.unrealengine.com/t/how-to-create-a-simple-object-in-c-with-ue4/295554/8 "2017-07-25T13:58:26Z")

</div>

\<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 😃
