Player not valid after respawn

For some reason I am getting runtime errors when my player interacts with game objects after a respawn. When I check the Message Log I get these kinds of errors indicating my Player is not valid.

This only occurs after a respawn and I’m not sure what needs fixing. The message mentions “pending kill or garbage”, so I’m thinking the original pawn is still being pointed to while no longer in the game and this may be causing the validation issue. I’m really not sure though.

If anyone can give me some tips, that would be great. Here are snippets from my blueprints if that helps:
Player blueprint:
Gamemode blueprint:

Is the player pawn being referenced in any other actors?

Yes. That’s where all the problems are happening - in the other blueprints.
Now that you mention it, those references are created on the Begin Play node, so are probably still referencing the old player pawn. I’m not sure if this is the right way to go, but is there a way to replace the old reference? Like reassigning it to the new player pawn?

What I’d do is make a custom event, let’s call it “OnPlayerRespawn”, for the enemies/actors that need it. It should take a pawn reference as input. Then set that as your stored player reference.

Also, its good practice to do IsValid checks for object reference variables to avoid errors like this. Which can lead to crashes when your project is packaged.

1 Like

Thank you Tuomo, you helped solve my issue! I think I will share in detail what I did in case it is helpful to others experiencing the same problem.

Since BP_PlayerPawn is being referenced by a number of different blueprints, I decided to create an interface that could be implemented by all the relevant blueprints containing the function On Player Respawn. There I added an input for a BP_PlayerPawn Object.

In the Class Settings of all the relevant blueprints I made sure to add the interface in the Details panel. Each of these blueprints have a BP_PlayerPawn reference created on the Event Begin Play, so I was able to attach a setter for the Event On Player Respawn function.

My BP_GameMode has a Get all Actors with Interface node referencing BI_PlayerRespawn and the resulting array gets put through a For Each Loop which calls the On Player Respawn function.

Just some questions regarding the Is Valid checks. Is it advised to do it every single time the reference is used? For instance, for BP_GameMode I put one within the body of the For Each Loop, but I could have done it prior to the loop so it is only done once.

Also, since I am doing a cast to BP_PlayerPawn earlier in that same thread of execution and setting the result, is it necessary to check validation if the cast did not fail? If the cast did fail for whatever reason, it wouldn’t even get to the validation check anyway.
Is it possible that something unforeseen could happen to the reference between the cast and the validation check?

Nice touch making it an interface, excellent work!

For the IsValid question, do it where you know there’s a chance of a reference becoming invalid.

Putting the check before a loop is definitely going to save you on performance and likely won’t result in issues.

1 Like

In your opinion, would you say that it’s unnecessary to place it like I have here? It seems to me that it would only be invalid if the cast failed, and the Cast Failed execution pin more or less does the same thing, so using it might be redundant in this case.

Yeah, see my note about having a chance to be invalid. And as you said, the failure pin on the cast achieves the same result.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.