getting this error (pending kill or garbage)

So basically i have a widget that shows a battery percentage for a flashlight for the character, and i get this error after my character gets destroyed and i don’t know how to fix it, my widget for the battery percentage stays at 0 and stops working after. Death is setup with just an overlapping collision. P.S I’m still a beginner in Unreal Engine.
[Widget]



[Gamemode]

The quick answer is, when your player dies, the PlayerRefr in GetBatteryLifePercent is no longer pointing to a “valid” actor. You need to drag off that node and place an IsValid check. If it’s valid, update the battery, if not, simply set the battery life to 0…or do nothing, whatever works for your game/tastes.

This is a matter of implementing good programming practices. On EventConstruct, you’re doing a GetAllActorsOfClass and assuming that it actually finds it by immediately following that with a Get on the first element…and then not checking the output of that. Next in the GetBatteryLifePercent you’re using the PlayerRefr without checking if it’s valid. What’s essentially happening is when your player dies, that reference (PlayerRefr) is no longer valid. Trust but verify is the motto when it comes to references. Always check to make sure they’re still valid before making use of them.

Hey, appreciate the answer that actually makes a lot of sense, I just have to find a way to update the battery after the isValid. Thanks

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