Create Custom Spring Arm Component

Hello Everyone!

I am currently learning C++ programming for UE4, being a programmer, I felt I was kinda cheating using BPs, so I am trying to reproduce all mechanics I had on my original BP project in C++.
One thing I always wanted for my game was the possibility to override the Spring Arm Component collision, so instead of drawing the camera closer, it would rotate the spring arm for a complete top down perspective (the game is kinda 45 degrees looking at the character, just like your typical old school RPG).

After a bit of struggling, I managed to do this via BP, using some workarouds, so the Spring Arm is not the one actually controlling the need for rotating (I created a BP extending from Actor, placed strategically in the level where view would be blocked, and it triggers the rotation of the Spring Arm on the Character, when he enters the volume). But I read somewhere that we can extend our components in C++ ourselves, so I went to do this guerilla style, by my own.

I actually created a new class, extending from USpringArmComponent, and was able to create my own version of BlendLocation (named BlendRotation), and to override the GetDesiredLocation, so that when the Line Trace hits something, it should rotate the spring arm for a 90 degree top view. I am not sure if it will work, because I had a lot of errors when trying to setup this component on my Character class. When I extended the Character class, it came with a predefined attribute for the Spring Arm and for the Camera. I tried switching this declaration, getter, and constructor from the USPringArmComponent to my UModernSpringArmComponent, but without success.

Sorry for not having any prints or code right now, as I am not writing from my PC, I should update with screens or snippets later.
For now, I would like to understand if:

  • Am I able to setup a custom Spring Arm Component? In positive case, how? How can I setup the Character for this trick? Is there another way to achieve what I want?

Thanks a lot for helping, this forum has killed a lot of doubts I had!

I want to know this, too.

In my custom camera system, I have multiple different kinds of cameras with pre-defined settings and custom functionality. Up until yesterday, the system I had in place was working perfectly (I mean technically it still is, but I can’t add the extra functionality I want to add to it. you’ll see why if you keep reading) but only assuming that the cameras were attached to an actor that ALSO already had either a mesh or spring arm also attached to them (depending on the camera type. first person style cameras require a head bone in a mesh to add to to MAKE it first person, others are different varieties of third person cameras that have different settings and functionality and need a spring arm with specific settings to get it to work just right). While this system I set up works out-of-the-box, requiring the person using it to KNOW to attach other components as well feels like it’s just asking for failure. My system already checks to make sure that all the things it needs to work are already also included, or it fails gracefully printing out a string stating what is missing. But that’s not good enough for me.

So I decided that in the event that things that are needed aren’t also included, I started adding code to create the missing things that are needed. In the case of an actor that has no skeletal mesh (or at least has no “head” in the mesh to attach first person cameras to), I simply said “ok I’ll make it a very close-by third person camera, and add a spring arm.” (I can’t exactly add a mesh in case their mesh is just different). This worked fine because in the case of first person cameras there’d be no spring arm needed so this 1 spring arm created in this way is fine. Then I moved onto my third person follow camera, and things started to break. In the case of not having a spring arm at all, I could just add one and it’d be fine right? Well what if they had a spring arm for some other purpose? Well then I just get all components of spring arm class and create an array of them, but then there was no way in the code to know ahead of time how many spring arms they already have or what they’re for, so I couldn’t go “is there a spring arm for x purpose?” and branch off cause there’d BE spring arms but maybe not a free one. OR maybe there WOULD be one meant just for that, but if my code didn’t know ahead of time, it would make a new one. Suddenly I needed to get specific spring arms attached, without knowing ahead of time about some future-available component. I can’t have it go owner → get spring arm by name, because there is no such node. And I can’t just add a spring arm and drag off the reference from the owner because, again, the character could possibly not have a spring arm, and then the code won’t compile or work. Sure I could get a list of all spring arms, assume there’s NEVER one available, make a new one, and add it onto the end of the array, thus increasing the array length by 1 and, having stored the size of the array ahead of time, knowing precisely what array index is my new spring arm created in blueprints for that camera, thus ignoring any and all spring arms made by the user. But that too has it’s problems, in that I can’t see all the values that will be preset when that spring arm is created, nor can I expect the user to see them either for them to modify to THEIR needs because that spring arm does not exist within the editor, only within the memory of the game being run.

TLDR: I could not specifically determine the existence of a spring arm need for my cameras to work without adding the spring arms myself and REQUIRING the end user to start by using MY custom character class and building off of that, or without giving them some heavy “How to Use” instructions telling them they MUST add for each camera type a Spring Arm to the actor that’s getting the cameras in question and then going into the code and manually getting the reference to each spring arm for each camera to attach themselves to.

Shorter TLDR: I can’t assume the end user of my system will know what they’re doing, but I can’t make it as Idiot-Proof as I’d like to without having a way to specifically create Custom Spring Arm Components that I can call by class name (as there’s apparently no other way to reference them except by Get Class and Is Child Of nodes to get a True or False response).