Unreal Engine blueprints finally gets an "Add Component by Class" function!!

Adding components to an actor at runtime has always been kind of messy if you've been using blueprints in Unreal Engine. There was never a generic function like "Spawn Actor from Class" where you could just happily plug in the required class and be done with it. Instead when it came to components, we always had to specifically add each type of component by name. For example, given below is a scenario where I'm adding a couple of components to provide an actor with Damage Buff and HP Regen abilities:

As you can see from the screenshot, I'm using an enum to first determine which component has to be added, and then adding the associated component on a case by case basis. This might not seem like an issue here given that we have only two of these, but it feels like redundant busywork once we start dealing with a dozen or more of these components.

The UE4 blueprint community has been asking for a generic class based component spawning system for quite some time. And finally about a few months back, we got our wish in the form of a new Add Component by Class function.

The Add Component by Class as the name suggests lets us spawn in any component by simply specifying the required class. That's it. No longer do we have to add each type of component individually. The process is now just as easy as spawning an actor at runtime. Shown below is an example of the new function in action:

Unlike the previous screenshot, here we have a very straightforward execution flow. No enums, no switch case, and no need to have a whole bunch of add component nodes.

When I first noticed this node a few months ago, I was surprised to find no mention of it anywhere in the latest Unreal Engine release notes. But whoever was responsible for adding this node, thanks a lot for exposing this feature to blueprint users.

Comments