UStaticMeshComponent is null ptr how?

okay, so I made a function called ItemMesh from a UStaticComponent inside of Item Header File which is the picture below.
ItemNullPtr1

and I created it inside of the Item constructor which is the picture below

and finally inside of my weapon constructor, I use Item mesh as well. and access it fine but jut like the two previous, it comes back Null. how do I fix this error?

why does it keep coming back null and how do I fix it?

Does AWeapon inherit from AItem? If so does it call it’s parents constructor? You may be missing the super init hence the parent’s createdefaultsubobject might not being called leaving it’s parameters as nullptr.

in constructor

Header:

AWeapon(const FObjectInitializer& ObjectInitializer);

CPP:

AWeapon::AWeapon(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
// your CDO code here
}

This will call the parents init code through the super ObjectInitializer

wait like this? or do I put ItemMesh after super.

That should be enough. But you also have to modify the header definition to contain the object initializer.

So instead of

AWeapon();

swap it out for

AWeapon(const FObjectInitializer& ObjectInitializer);

and the error in the cpp should go away.

yah I did that. the error says "no instance of constructor AItem::AItem matches the argument list argument types are (cosnt:FObjectInitializer

Ahh yes you need to change both AItem and AWeapons constructor for it to work :wink:

Both need to take in const FObjectInitializer& ObjectInitializer otherwise calling super (the parent) won’t work as they have different constructor parameters.

error is still there

its been driving me insane lol btw thank you for your time :slight_smile:

I just learned about these windows, just learning how to read locals… I dunno if thi’ll help you more then me.

i understand what happened but not how to fix it. my item mesh is returning null because it is empty, so all the children are also returning null for that reason, but I can’t add a mesh into it and I see no error in the code that could have caused this.

Try putting a breakpoint at the createdefaultsubobject for the aitem and aweapon and see if aitems constructor is being called during aweapons init. I’ll do a test of my own once I’m at my pc.

If the error happened during the project compile then just add a nullptr test before accessing itemmesh
So if(itemmesh!=nullptr){
}

it did happen during the compile, and so I did what you said and it opened but as per usual, as soon as I press play it crash again.

YourProject.zip (38.9 KB)

I made a quick example project to show Item and weapon implemented with the CDO.

No crashes.

Perhaps show me where you are calling the attach method?

hey, I can’t open your project, i tried to do it in 5.3 and it won’t let me, but here is where I try to attach it to a socket.

and then there is also the spawn

Ok I’ll downgrade it to 5.3. I’m guessing Equip calls AtachMeshToSocket?

IT WAS A BUG!!! I renamed ItemMesh and it fixed it!!! thank you so much for your time!! I apreciate it!! and I now know how to do breakpoints in VS :slight_smile:

1 Like

Congrats on getting it working.

Sometimes it’s best to delete the temp dirs (Intermediate, DerivedDataCache, Binaries and .VS) and rebuild if there are unknown / unexplainable errors

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.