Problem with movement components


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.

It seems I’m not the only one having this problem as there was a thread about that created earlier:
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):

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);


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:

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

Here’s the image of UE4 debug output:

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.