I went ahead and tried adding my own custom character movement component to my custom character class. I was successful for the most part but have a few questions.
In the wiki article he has, within the custom movement component’s header file, defined:
My question is where I’m I suppose to call the InitializeComponent() function? Or in what situations do I even need to call it? When I place this function within my custom movement component’s constructor UE4 crashes so I don’t think that is the place. Any insight on this issue would be greatly appreciated.
That happens because your component must have been registered (with RegisterComponent()) first. InitializeComponent also must NOT have been executed before.
So overall I don’t think you should call InitializeComponent() yourself at all. In UActorComponent::RegisterComponentWithWorld, which is called from UActorComponent::RegisterComponent, InitializeComponent will be called automatically for you if it hasn’t been initialized yet (see above) and the component wants to be initialized indicated by bWantsInitializeComponent.
I don’t know if/where the MovementComponent is registered. Assuming it is registered “somewhere” automatically there should be no need to do this anywhere else manually. All you’d have to do is replace the MovementComponent class with your custom version. On the other hand assuming it is NOT registered anywhere a safe place to call RegisterComponent() should be the Owner Actor’s BeginPlay(). At this point the Component should have this Actor as its Owner and the Owner should have a valid World.
Of course another option could be to run your own initialization function…
Pretty much everything @UnrealEverything said. There is a flag you set with the constructor “bWantsToInitialize”. This will have the system automatically call Initialize on the component.
The Component will be registered automatically when the object enters the world, so you shouldn’t call any of those functions manually anywhere or you could mess up the initialization process which follows a certain chain.
Not actually sure why that’s in that tutorial to be honest…
Are you saying it is also not necessary to call RegisterComponent() manually at all (in any case not in this specifc MovementComponent scenario)? Would you mind confirming this specifically for ActorComponents?
I’m asking this because I’ve seen at least one place in engine code where an ActorComponent derived class is created using NewObject<> and then has RegisterComponent() called on it.
Ah yeah good point - If you call NewObject<>, you do in fact have to manually call ‘RegisterComponent’ after that, which I think also calls ‘AddToRoot()’ so it doesn’t get GC’d.
If you create it as part of ObjectInitializer though in the constructor, you shouldn’t have to do anything.
Yep was about to say that. Any components built int he constructor using the objectInitializer will have their Register automatically called. Any Component built during runtime using NewObject will need to be manually initialized.