Getting a reference to a child actor, that works even after detaching/re-attaching the child actor (solved, clumsily)

How do I get a reference to a child actor (that I’ve attached to my character using the editor) that keeps working even after I detach the child actor from my character, and re-attach it later?

The problem I am having is that my cannon, which becomes visible, moves to a location, and then detaches itself from my player character, will not become visible, move to a location, and detach itself if I had done so before.

This is due to not being able to get a proper reference to the cannon child actor that will work with the 3 states of the child actor cannon that it needs to deal with:
1: Attached to the player character BP through the editor’s Component menu.
2: Dettached from the player character.
3: Re-attached to the player character using a “AttachToComponent - Target is Actor” node.

I have found the solution to this problem, and could not find it anywhere else. I will post the answer below. If you’re pretty new. Do yourself a favor and follow the instructions instead of trial and error-ing yourself through this problem.

I found the solution to my problem of getting a reference to a child actor of a player character and having that reference keep working even after detaching the child actor, and re-attaching it afterward:
What I achieved:

  • When I press the “A” key, the child actor (it’s a cannon) is made visible, it’s location is set to in front of the player, scale set to 4.0, and then detached using a “DetachFromComponent - Target is Scene Component” node.
  • When I press the B key a custom event is fired that resets everything about the child actor cannon, back to the way it was before I pressed A.

How I did it:

  • Player character blueprint, with a child actor cannon component added through the component menu and dragged onto character mesh.

  • The player character blueprint has a variable (let’s name it cannon reference) with the “variable type” set to the name of the BP of the child actor cannon. It should be listed under “Object types”, and when you find it, you can choose between a few types: click object reference. (Important: Do not select child object reference, actor reference, or any other. It needs to be the name of the BP of the child actor, and under that name select object reference)

  • In the BP of the child actor: On the “Event Beginplay” node; Cast to playercharacter, get the “cannon reference” variable, and then get a “reference to self” and plug that into the cannon reference.

  • Also in the BP of the child actor: Make an event (let’s name it “attach cannon”) that does a cast to the player character, and sets the “cannon reference” variable to “reference to self”, and then using that same cast, do a “get mesh” and plug that into the “AttachToComponent - Target is Actor” node

  • Back in the player character BP: When pressing the “B” key, do a “Get All Actors of Class” and point it to your Cannon BP. From the output, make a node by typing “get(a copy)”, and from the output of that start the “attach cannon” event that we made in the child actor BP.

Now things should have come full circle, and you can use the “Cannon reference” and plug it into (it will create a “Target Root Component” conversion node for you) set visibility, set size, set location, or anything else you want, and it should keep working even after multiple detaches and attaches.