Respawning Actor on Player Death

A problem I’m having right now, is trying to respawn an actor after jumping on top of it, and destroying it ‘mario style’. After the player dies, I can’t seem to figure out how to respawn the actor back into the game.

For example after collecting a checkpoint, I destroy an actor soon after. After destroying the actor I die, thus spawning back at the previous checkpoint. For some reason after respawning, the actor that was destroyed, stays dead.

This is probably a dumb question, but any help would be appreciated thank you!

Actor

Player

When you destroy an actor that instance of code is gone.

You’d need to spawn another one with spawn actor from class.

If you want the world actors to respawn you may want to reload the map or go through a system of checks to respawn the actors.

How would that look in the blueprints? Would it just be beginoverlap -> cast to sidescrollercharacter -> spawn actor along with a player controller?

Thanks for the reply.

Are you having trouble respawning the player or the actors which the player is killing?

If you want to respawn the player, then you would need to ‘destroy’ it, create a new instance using the ‘spawn actor from class’ node, and then ‘possess’, so it can be controlled.

If you want to respawn the actors which the player is killing, then you have two easier methods which you can take (that I see anyways)

  1. When an actor is ‘killed’ by the player, just hide it and turn off collisions until its ready to ‘respawn’. At that point, make it visible again and reenable collisions.
  2. Maintain an array or map of where the actors are supposed to spawn and also which type of actor is supposed to spawn. Then you can use this data to respawn the right type of actor using ‘spawn actor from class’ and settings its ‘transform’ using the data from the array/map

Hopefully this helps, but I’m not sure I fully understand what you are trying to do.

I want to respawn the actor that the player is killing. Option 1 seems to be the easiest, but I’m not sure how to go about it. Thanks for the reply.

I believe the nodes you are looking for are ‘Set Visibility’ and ‘Set Actor Enable Collision’. I’m not currently near a computer with Unreal Engine on it, so I can’t confirm that for you.

You could probably hook these up to your begin overlap event and your respawn event.

Your suggestion was spot on for the actor disappearing. Thank you for the help and patience. It’s just getting it to reappear after the player dies that I am unsure of. Sorry for my lack of knowledge. Here are my connections.

Actor Blueprints

Player Blueprints

The way I would approach this would be to create an array of actors in your game mode blueprint. In the construction script of your actors (or event onBeginPlay), I would suggest adding a reference to that actor into the array in your game mode. Then, when your player dies and respawns, you can trigger an event or function in your game mode called something like ‘resetMap’, and in here you can loop through each item in the array. As you loop through each item, call ‘Set Visibility’ and ‘Set Actor Enable Collision’ for each one.

Does this make sense? I’m not sure if I explained it very well.

Kind of, but I could definitely use a visual if you get the chance.

I’m not near a PC equipped with Unreal right now, but I can try and visualize it a bit with text:

On your gamemode, create an array variable of type ‘Actor’. For this example, I’ll call this array variable ‘arrActors’

Additionally, in your gamemode blueprint, create a custom event which I’ll call ‘ResetMap’. Add the follow sequence of nodes:
‘Get arrActors’ -> ‘For Each Loop’ and on the ‘Array Element’ pin of the ‘For Each Loop’ connect a ‘Set Visibility’ node and a ‘Set Actor Enable Collision’ node.

In your ‘Actor’ blueprints, off of event onBeginPlay, add the following sequence of nodes:
‘Get Gamemode’ -> ‘Get arrActors’ -> ‘Add’ node. As well, the add node should have a second pin representing what you want to add into the array. I can’t remember if this defaults to ‘self’ but if it does, you’re golden. If not, you will need to use a ‘self’ node and plug that in.

When your character respawns, add the additional sequence of nodes:
‘Get Gamemode’ -> ‘ResetMap’

I ran into a couple issues when trying to connect the get game mode and when trying to connect the array element unfortunately.

GameMode Blueprints

Actor Blueprints

Ah! That’s my fault for forgetting to mention: You have to cast your game mode to your custom game mode blueprint. The node will be something like ‘Cast to <insert_your_gamemode_blueprint_name_here>’ then you should have better luck! Also, in your first screenshot, your ‘Set Actor Collision’ node should work fine because it’s expecting an input type of ‘Actor’. You can see what it’s expecting as an input in the italicized text under the node name. The reason ‘Set Visibility’ isn’t working is because that specific node is expecting a scene component. To make this work, you need to ‘Get Static Mesh’ from your actor and use that to plug it into ‘Set Visiblity’.

This screenshot of yours actually did it right in a different event, so you can check it for more details on what I mean:

&d=1486587923

Hopefully this helps you finish it up! :slight_smile:

These are my connections. I tried to get a reference to my actor by using ‘Get Static Mesh’ for the gamemode blueprints, but no luck. Instead I tried a ‘Cast to’, but no change.

GameMode Blueprints

Actor Blueprints

I think most of your problem lies here:
6aa21244fc201728d543d4c294cbaa51.png

You only want to call ‘Reset Map’ when your player dies. Or, in other words, after the ‘DestroyActor’ node in your PlayerDies commented area.

The other problem is that you aren’t actually adding anything into your array of actors. The bottom input on the add node needs to have a ‘self’ node attached to it.

This is a screenshot from an unrelated thread which shows what I mean:

I’m getting the same target error from before when I disconnect the ‘Cast to’.

Oh you still need to keep the cast. The screenshot above was only supposed to highlight the self node in case it wasn’t clear.

Ah, ok thank you. Now after connecting it all, the actor still stays invisible, but now I also get a error message after ending the game. It seems to be referencing the visibility nodes in the gamemode blueprints.

GameMode Blueprints

Actor Blueprints

Error

A couple things to try:

  1. Try setting the visibility of your array variable to ‘public’.

In other words, make it so the eye is green:
a8f8cf96b1eec9ac5e2e4cebd42ea0c9.png

  1. Try testing different scenarios. Try one where the player immediately dies prior to killing any enemies. Try one where the player kills several enemies before dying. Does anything change?

  2. Add an ‘isValid’ node after the cast node in the for loop area. If this doesn’t work, try moving the isValid node before the cast node.

Accessed none typically means it’s trying to run an operation on an input which is ‘null’. In simpler terms it means its trying to ‘Set Visibility’ on nothing. It does specifically mention trying to run it on something ‘pending kill’ which might mean somewhere else in your blueprints you’re attempting to destroy some of the actors in the array.

Just double checking, the player which you control isn’t also an actor of type ‘flat’ is it? If it is, then this is the root cause of the error.

No, the player has its default name of SideScrollerCharacter. I think that pending kill error had to do with the destroy actor in the player dies comment. The error did go away when setting up IsValid after the ‘Cast To flat’ though, but I received a new error when putting it before ‘Cast To’. This error would occur if I died and respawn, but if I just exited the game without dying, no error. Other than that it’s all still the same.

After Cast to

Before Cast to

Error

In the case where you put the isValid before the cast node, the ‘Array Element’ output on the foreach loop is what should be connected to the input on the ‘isValid’ node.

When you had the isValid node after the cast, were any of the actors reappearing?

This thread might give you some help:

This thread may be a simpler way to do what I described… This method would negate any need for the array. You would still turn off collisions and make them invisible in the same way, but this method would replace the way your ‘Reset Map’ event is setup: