Download

Realy noob question but how to make a New() of my unreal object ?

Sorry for such a basic question. I’ve searching in the doc, the forum, but dont find the correct search terms and I’m stuck.

I’m in the doc and all syntaxe I find and try didnt work.

Really basic trouble :

  • Create a Actor class Cloison
  • Create a Actor class Cellule

in Cellule, I want a new Cloison

ACloison MyCloison = ACloison();
or
ACloison MyCloison = ACloison::ACloison();
=> Compile error , Acloison::Acloison is private (in fact,no, it is in a public bloc)

I’ve seen that for some Unreal object, NewObject is used
ACloison* MyCloison = NewObject<ACloison>();
=> Compile, but Unreal crash when I put my Cellulle in the level editor.

ACloison* cloison = Cast<ACloison>();
Compiler can’t cast

ACloison* cloison = new ACloison();
=> Crash

I’ve seen for other object CreateDefaultSubobject() that I use on another class for creating a mesh
Compile, but same crash

If you have a few seconds to give me the correct syntax, or a link for this basic things, you’ll save me a great amout of time :slight_smile:
How tho make a new object, and the rule to use NewObject and CreateDefautSubObject ?

I had similar problem like you, but NewObject is only way to implement it safely.

(I also tried New, but it crashed in my project too).

My case was UObject containing UObject, it could be similar.

But did you made local variable (Closion) inside Cellue class?

In h. file, I would put ACloison* Closion; and Cellue(const FObjectInitializer& ObjectInitializer);

In cpp. file,

Cellue::Cellue(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
Closion = NewObject<AClosion>();
}

That’s what I come up with. I hope it is helpful

Thank’s for your answer.

I’ve tested both
ACloison* MyCloison = NewObject<ACloison>(); (local declaration, in the Cellule constructor)

and
ACloison* MyCloison; // in .h

MyCloison = NewObject<ACloison>(); // in cpp

Same result : compile but crash when used in editor.

My Cloison object seem to be ok, it just add a cube in the scene, it work when use in editor
=> I comment the code in Cloison constructor, in cass of trouble in the context here. But same result. Even whith an empty constructor for Cloison.

I tried on a new empty project and same result.

are you sure about using constructor I wrote as FObjectInitializer with Super(ObjectInitializer) ? beside that I am also clueless

You should try SpawnActor (https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/Spawning/index.html). AActor is not a simple UObject.

hope it help.

Yes I Do :
ACellule::ACellule(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
Compile but crash.

Anyway, a big thank for your help. I’ll try again.

I’ll try that this evening, many thanks for your answer Fafahh_unreal83.

And I’ll try another thing : in fact, I dont need an Actor for my Cellule. It just add a UStaticMeshComponent.

But in Unreal, I can’t make a C++ class for UStaticMeshComponent. What I have to take, a scène component ?

Concerning your first question:

Concerning your second question:
You declare a UStaticMeshComponent and then create it with CreateDefaultSubObject<UStaticMeshComponent>(…)

Try searching for the Pickup C++ project from Unreal Engine on Youtube. This series explains setting up actor components in c++ quite nicely.

Eco

Many thank’s EcoRI. I’ll read this post this evening.

For the UStaticMeshComponent , I’ve found CreateDefaultSubObject<UStaticMeshComponent>, that’s what my Cellule do (that’s all it is doing).
What I was wondering is ; can I create a Unreal object wich inherit of UStaticMeshComponent instead of Actor
But perhaps there’s no need.

I 've reading a few tutorial and explore a few C++ code of exemple, but for now, I undersand really slowly what I read :slight_smile:
I’ve use C language a lot there 30 year, I use Java for a long time, but discover C++ is not really easy (and my head is not efficient as it was :slight_smile: )
And the Unreal API have a lot of syntax and spécific macro which not help.

wonderfull Work it is, but hard to lear. But I love it, even if I broke my nose on trap on all line of code (for now).

Many thank’s all for your help.

This evening, I’ll program a Cube that is turning turn Youhouuuuuu
(Self control and low expectation are the rules for begining) :wink: