C++ programming guidance for Noob

Hello everyone!
Adhordal here. I have picked up Unreal Engine 4 and after enjoying the wonderful editor, i decided to jump into programming gameplay. I have worked with Unity 5 before and became quite proficient with it, but now that i have switched to UE4 i am having difficulty adjusting.
I have some questions for basics things in c++ and It’d be great if you could help me. I have searched quite a lot for both of the following problems but was unable to find anything to help me understand/overcome the problems.

  1. How to do I make a function in c++ be called from an Anim Notify I have set up in the Animation Blueprint of the blueprint that derives from the c++ class. I was able to work around this by doing specific things related to the notifies in the blueprint and connect them to the c++ class but I’m curious as to how i can get that to work in pure c++.
  2. I have placed a box collider component in the blueprint class viewport that derives from my c++ class. It will be used to detect collision with an enemy and apply damage on contact. The problem is that i don’t know how to reference that box in my c++ code. I tried setting a pointer in the code with TSubobjectPtr but an error came up saying that that was deprecated. I tried with: class UBoxComponent* swordCollider … Set to public and editable in the blueprint so I could set it to the swordCollider within the blueprint, But no luck. It doesn’t even appear in the blueprint properties(even after compiling and restarting ue4)

Please, if anyone could help me with these problems I would be grateful.

Thank you
Adhordal

Hi Adhordal,

Welcome to UE4’s C++ programming. First thing you should probably look at would be these two pages:

One of the biggest things to understand is the reflection system which is what is explained in that second link. To access a variable or a function in the editor/blueprints, you’ll need to declare UPROPERTY or UFUNCTION first with the correct specifiers, such as EditAnywhere or BlueprintReadWrite for UProperties and BlueprintCallable for UFunctions.

As for your second question, the reason that this UBoxComponent can’t be accessed in code when added to the blueprint is because the class itself doesn’t know that the blueprint exists, let alone anything added to it. If you wish to use the UBoxComponent in code, you’ll need to declare it in the .h like so:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Box Component)
UBoxComponent* BoxComponent;

After that, you’ll need to initialize it in the .cpp, inside of the constructor like so:

BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));

This should allow it to be seen in any blueprints based off this class while also being editable/callable in C++.

Another good link that would be useful if you’re familiar with Unity would be this: Unreal Engine for Unity Developers | Unreal Engine 5.1 Documentation

I hope this information helps!

, thank you for helping me! The getting started from unity page was a gold mine. I feel I know a lot more now, although experimentation will dictate how much i progress now. I managed to get the box component to work, but I must confess i still find the Custom Anim Notify quite difficult to be used in c++. Thank you for you help.

Practice makes perfect and that has never been more true than with C++. If you run into any more issues, please feel free to post another question here and either we or the community will try to help you.