Download

How to get C++ Actor Component working?

Hi,

I’ve created a C++ Actor Component class and I am going through endless agony to get it working. I’ve spent over two hours searching, trying all sorts of different obscure functions such as RegisterComponent(), InitializeComponent(), OnRegister, or FObjectInitializer, but no matter what I try, the Actor Component just does not do anything. No BeginPlay, no Tick.

To be super clear, I am not creating the component and runtime. I’ve added the component in a Blueprint editor outliner. I want it to work the same way FloatingPawnMovementComponent does. You just add that one to your blueprint, and it does its thing. The custom one I created doesn’t, despite me having done absolutely everything that’s in FloatingPawnMovement cpp and h files the exactly same way… I am really lost here…

I am even more confused now :frowning:

Yesterday, I spent all day trying to get the actor component to work, and had no luck. Even tried restarting Unreal Engine. Today, I just opened the project, hit play, and the debug message in the component started firing without me doing any changes to the code since yesterday.

So I guess the already steep C++ learning curve is made even steeper by unpredictable bugs :frowning:

Something tells me you didn’t rebuild the project after adding your code… there’s no reason it wouldn’t work one day and then it would the other.

I did build it several times, but probably not rebuilt from scratch. I am beginner when it comes to C++, and for example adding an Actor C++ class did not require a rebuild from scratch in order for it to work, just regular build. That itself creates some implications which beginners like me can form misunderstandings based on.

Here’s a tip from back when I learned C++ using another engine with full source code: look at other places in the engine source for things similar to what you want to do and see how they do it, this way you minimize beginner errors and reduce the amount of boilerplate code you need to learn to get something working at first.

That’s exactly what I did. I looked at FloatingPawnMovement component and its parent classes. They all did some things, but none of them really made my actor component work. It just started to work on its own after restart, even when I did build the project several times. I just had no idea I had to do full rebuild every time I add a new class, because sometimes, new actor classes would work immediately with just regular build, without complete rebuild. It still leaves me very confused.

You shouldn’t have to do a full rebuild each time you add a new class.

According to TheJamsh I should. The confusion increases… :expressionless:

You shouldn’t need to do a complete rebuild, but you will have to build whatever new files you have added. Hot Reload doesn’t work very well to be honest, especially when adding new reflected typed or properties - so it’s safer to just hit ‘Stop’ then ‘Local Debugger’ in visual studio.

Oh, so the issue is that when I do a regular build, it does just a hot reload which may not reliably register new classes? What confuses me mainly is that when I created a new C++ Actor Component, it got immediately added in the component list of my blueprint actor. After that, I defined few new blueprint editable variables in the component, and when I did a quick build (Ctrl+Shift+B), the variables became available in the blueprint UI afterwards. But when I put simple message logging in either BeginPlay or Tick, they did not work. I thought I had to add some functions to initialize them, but they did nothing. Then after I restarted the engine, suddenly the logging messages in BeginPlay nad Tick started to work with no changes.

The only way I can explain that is that hot reload doesn’t reliably update everything.