Download

Problem with movement components

Hi!

I’m a total newbie at game programming even though I’ve been programming for a while now and I’ve been playing around with creating essential paper2D stuff for my game. However, there’s a problem I’ve been trying to solve for days now. I can’ t understand why all the movement components of my game when I debug my game remain NULL even though I do some stuff with them. All the other components work just fine, the weird problem appears to be only with the movement components.

Anyways, here’ s a screenshot of my projectile’s movement component while debugging my game in VS2015. I felt it’s not that neccessary to add pictures of my .cpp and .h files. You can see the part of .cpp file I’ve created for my projectile, the .h file just declares the components I want to use for my projectile.
a40ea3690d4fb562ea7b915f6eb56b82.png

It seems I’m not the only one having this problem as there was a thread about that created earlier: https://forums.unrealengine.com/showthread.php?16514-Adding-custom-movement-component
However, I couldn’ t find the answer I seeked there. In the UE4 I had the similar log output as you can see in the following picture (the message is copied from the thread I was pointing out earlier):
fda06edc14c3e788e51e0d3f5d30f83f20c73bd4.png

I hope this problem is solvable, because it has quite an impact for the coding I want to do in my future plans.
Thanks in advance!

In my quick test on 4.12.5 it doesn’t return NULL, works just fine here:



	auto const Movement = CastChecked<USK_MovementComponent>(GetCharacterMovement());
	Movement->SetNetAddressable(); Movement->SetIsReplicated(true);


Hey!

I tried to debug it myself by printing some log messages and it seems that the movementcomponent of my player pawn is NULL. How could this be, even though I created suboject for this component?

In .h file I once again declare the movement component and in .cpp file I do the following things:
60342ca5b30116c6fc6201995f2f9c2120fc4275.png

When I press the Play button in UE4 then it goes straight into if (m_movementComponent == nullptr) clause.
90cf8f8c0853bc3956af0a1f69131619d8b50baa.png

Here’s the image of UE4 debug output:
44b691091048d4b759a34ad06a8bc35e.png

You need to use the Object Initializer constructor, or it won’t work. Don’t use custom constructors unless you know what you’re doing, or if you want to create sub-objects.

Similar to this:



AMyPlayerPawn::AMyPlayerPawn(const FObjectInitializer& OI) : Super(OI)
{
    m_movementComp = OI.CreateDefaultSubobject<UPawnMovementComponent>(this, TEXT("Movement"));
}


Oh. Cool. Thanks a lot for the solution. I wasn’ t aware of that…
Could you tell me where can I find examples, tips how to write good code for UE4 games? There are alot of tutorials for visual script out there, but it seems not so many C++ tutorials…

Thanks again :slight_smile:

Sorry… But I just read from that post that using ObjectInitializer is deprecated? According to this post:

PCIP is deprecated, ObjectInitializer is the correct version.

Deleting and recreating the blueprint could help. I’ve found at times that the blueprints do not update correctly if new objects are added to the class later in the process, no idea why but recreating the BP fixed errors like those.

Thanks guys for help. I managed to find solution for my problem by using default Paper2D code project and trying out the code that was generated there. I wonder why I didn’t use this earlier…

I also tried out your help.
“Deleting and recreating the blueprint” - I tried that repeatedly, didn’t solve the problem for me. I guess I used my movementComponent wrong.
Apparently, using ObjectInitializer didn’ t also solve the problem for me.