Thomas Mountainborn

Unreal quick tip: Simulating Cable Components in local space

Ever needed to have a Cable Component attached to a moving actor? Chances are it looked like this:

A regular Cable Component moving through the world.
A regular Cable Component moving through the world.

This is because the cable is simulated in world space; as the cable moves through the map, it’s as if it is constantly whipped with great force.

Luckily, it’s very easy to get the cable to be simulated locally. We do need to add a tiny bit of C++ code for this to work though, but don’t run off screaming if you normally wouldn’t touch code with a 10 foot pole. There’s no Blueprint alternative, and it really isn’t all that scary.

Just in case you’ve never added C++ to UE4: install Visual Studio Community 2017 with these packages.

Once that’s set up, go File > New C++ Class…, select “Show All Classes” on the right, search for CableComponent, and click Next. Enter “LocalCableComponent” as name, and add “\Components” to the path.

Creating a child cable component class.
Creating a child cable component class.

Once Visual Studio loads up, take a look in the LocalCableComponent.h file. You’ll see this line: class YOURPROJECTNAME_API ULocalCableComponent : public UCableComponent . Copy the “YOURPROJECTNAME_API” to someplace safe, we’ll need it in a moment.

Now, replace LocalCableComponent.h’s contents with this:

All we’re doing here is marking that we want to override the component’s tick, and declare a Vector variable. This is also where you’ll have to copy the PROJCETNAME_API you safely copied earlier back in to place, otherwise your code won’t compile.

Next up, LocalCableComponent.cpp:

This is all that’s required to get the cable to simulate in local space: each tick, we add the delta world movement to all cable particles. ApplyWorldOffset() is already implemented in Unreal’s UCableComponent, it’s just not called from anywhere.

Save both files, hit compile in Unreal, and start using the component! Here’s a comparison:

Comparison between local and regular cable components
Top: local cable. Bottom: regular cable.

Leave a Reply

Notify of