How to detach child from parent on "death" in Blueprint character

I have a very specific question.

I want to make a character blueprint that has other character blueprints as “child actors”.
These child actors have a float value that, when it reaches 0, they “die”.

I want to make them DETACH from their parent when they “die”. Is that possible?

what does detach mean. And what is the importance of this parent/child relationship?

I mean that when the “child actor” "dies, " it will be detached from the parent of the blueprint.
So, when I move the parent, the child won’t move.

To be honest, I don’t understand the “importance of the relationship” between parent/child.

Hi,
tbh, I’m not a huge fan of Child Actors. The idea is great, but proved to be troublesome in my experience.
Anyway, in your case it seems you should just spawn the actor you want and attach it manually to have a full control over it. This way you can detach it anytime you want with simple Detach function.
You can try to detach child actor but not sure if that’s possible (I can’t open UE right now), but that just doesn’t sounds like a good idea to detach component from its owner.

2 Likes

I was asking for clarification about what you intend for this relationship to mean, as in, does the child follow the parents transformation?

That is typically what it would mean but you were vague so I wanted to clarify.

I understand what you mean now though, and I agree with above: don’t bother with the child actor thing because it will add more complications than necessary.

In the blueprint of the thing that can die and follows some other actor, just use a Attach Actor to Actor (there are some other attach nodes as well) node and get a reference to the one that you wish to be the parent.

Here is example from my project:

When the death event fires, you can add this:

image

In my case, I used Attach Actor to Component, that way I can use a scene component to define a specific place to attach the actor.

Another important benefit to keeping the actors separated this way is that you’ll be able to refactor this all so that the two classes never need to know about each other. In blueprint you can accomplish that sort of decoupling using interfaces. I don’t believe you would be able to accomplish that using the Child Actor feature.

3 Likes

Ok, I think I need to be more specific.

I have a unit of soldiers that is character blueprints.

I’m not using any AI; I just parent all 100 or more characters into a single character and move them around with keyframes.

The problem is that when a character “dies”, the whole unit moves, dragging the dead character with it.

Using methods other than parenting is problematic because I would need to figure out how to make the characters move in formation, utilising some form of navigation. I tried a lot to achieve something like that to no avail.

So, is there a way to make 150-character blueprints, following a single-parent character blueprint, using your above method?

I can’t precisely understand how to implement such a system method in my specific situation.

Yeah, this method will 100% work in your case. You just need to spawn your 100+ characters instead of adding them as components.

However, sorry for little sidetrack: Character class is quite heavy. If you are making RTS style game, or something with couple of houndreds of characters, it can turn out to be a performance issue.
It’s ok for prototyping as it’s easy to use with a lot of built-in systems, but as you move further into development consider using Pawns or just Actors, depending on functionalities you need.

Thank you very much for replying.

Sorry for asking, but I have no idea how to “spawn” characters in a specific formation around a parent blueprint…

As for the performance, I’m usually making videos, so I’m not really bothered about the performance as long as I’m able to animate the whole thing.

It’s messy, but it works for my needs.

Ok, so that’s not really a game and you are not really comfortable with scripting math?

In that case, I think you could just replace your child actors with Scene Component and tag them as “UnitSpawnPoint” or something, then on Begin Play gather all Scene Components with this tag (GetComponentsWithTag) and in ForEachLoop call SpawnActorFromClass using the transform of every scene component.
Remember to set Actor Class to your own character class.

Then, on unit death use DetachFromActor as shown by BIGTIMEMASTER above

Just keep in mind, this whole spawning from scene components is really hacky, it’s not something I would do in real game :slight_smile:

2 Likes

Yeah if i understand what lordlolek is describing correctly, you essentially just setup your formation with scene components (and you can animate them or w/e you want to do).

For each scene component, spawn an actor and then attach the actor to the scene component.

if its not a game you dont have to worry about optimizing it beyond that really, I wouldn’t think.

This will work the same as child actors, except you will be able to use the detach node and it should work.

2 Likes

Thank you very much both for your helpful replies!

The problem is that I’m not exactly sure how to achieve this without a step-by-step guide…lol

So, if I understand correctly,

  1. I’ll have to create my parent character blueprint.
  2. I’ll create multiple scene components within the parent character blueprint.
  3. Then I’ll tag all those components with a specific name.

Here is where I’m not sure what I’m supposed to do though .
In the event begin play (I’ll make a custom one since I’m already using the default), I need to add the code you are showing in the screenshot below. But I see there is a set from an array.

Is this array automatically available from the tagged components?

Also, I reckon that I’m using the “Character_Blueprint”.class.

These scene components are to be positioned by me within the blueprint, I guess?

Finally, I don’t precisely understand how I will replace the scene components with my desired character_blueprints -soldiers.

There are many things I don’t precisely understand with this solution.

I would have tried to complete my knowledge gaps with YouTube tutorials, but try as I might, I could not find anything even remotely useful for my specific situation.

Thanks very much for your patience.

Ok, for eg here is a problem that i have.
In the “spawnactorcharacter” I do have 4 or 5 different blueprints for “soldiers” that I use.

But, since it is a general class and not a specific blueprint name, how do I use all 5 different blueprints of soldiers as spawned actors?

you can make an array of actor classes,like this:

image

For each of the array indexes, you can enter whatever class that you want.

To spawn them along with scene componenets and attach them, like this:

If the number of scene components and number of entries in the Soldiers array is not the same, you’ll need some additional code to handle that, otherwise you’ll get some errors about indexes not found. That’s another problem, but if you search for something like “loop through array” I think you’ll find answers. A simple solution is just to add more entrees to the array so that they both have the same number.

1 Like

Thank you very much for replying!

I’ll try to thoroughly examine what you are describing here and let you know about the results.
Still not exactly sure how to do a few things but only testing this will give me specific answers.

1 Like

So, I’ve tried to replicate your method.
Some things work, and others don’t, unfortunately.

Here is what I did.

The pros are that the Character blueprint I’m using successfully spawns other character_BPs around it where I’ve placed the scene components.

The cons, though, are that 1) I can’t get the “death” reference from the spawned Character_BP’s since their “death” occurs within their blueprint event graph.

  1. The spawned character blueprints don’t have their gravity nor follow a landscape’s sloping surface. Instead, they blindly follow the “parent” and their position dictated by the scene component.

Any short of help will be very appreciated.

I believe you are struggling to link up the soldiers’ death events because you’re trying to call DetachFromActor in the parent blueprint, rather than the children. From the tooltip:

“Detaches the RootComponent of this Actor from any SceneComponent it is currently attached to.”

Below I have created a character blueprint since I assume that’s what you’re using for your individual soldiers:

Soldier BP

I call DetachFromActor from my OnDeath custom event, which you would replace with whatever you are using to manage your death system.

Gravity is faked by tracing down from above the spawn location, and setting the soldier’s relative location accordingly. I use a sphere trace with a radius equal to that of the character’s collision. When setting the relative offset I use

hit location + capsule half height - capsule radius

in order to account for the radius included in the sphere trace.

The variable SpawnPoint is a Scene Component Object Reference, with Instance Editable and Expose on Spawn both checked - this is so that I can pass in a reference to the appropriate scene component when I spawn the soldier actor from within the parent blueprint, as shown below:

Spawner BP

I hope this helps.

ADDENDUM

Upon further testing I discovered that detaching a soldier from the spawner caused it to teleport into the floor.

Adding a branch to prevent the soldier from performing the line trace, and setting the detachment rules to Keep World prevents any untoward behaviour, although I must admit the idea of the soldiers going literally six feet under upon death is rather amusing.

1 Like

Thank you very much for replying!

The so-called “death” event in my character blueprints is just an animation that plays, followed by disabling movement and destroying its collision.

So, it is not a separate custom event.

Well, in that case you would put the contents of my OnDeath event in the same place as your animation.

1 Like

This needs a lot of investigation.
There a few things that I’ll need to figure out here in trying to adjust this system to my own.
I’ll test it thoroughly and let you know about the results.