How to get player reference for weapon actor?

I have a simple system where the player can pick up firearms that are on the floor; if the player enters a sphere collision that surrounds the weapon actor they are able to pick up the weapon by looking at it and pressing F. When the player enters that collision sphere it sets a player variable to be the player actor and then it can use that variable to get the referenced player actor whenever it needs it.

When the weapon actor is picked up: The weapon that was on the flor is destroyed and it spawns 2 new child BPs of the weapon that was picked up on the player, one is visible to the player but hidden for all other actors, and the other is hidden for the player but visible for all other actors. It is set up this way because there are 2 rigs on the player, one being first person arms, and the other being a 3rd person full body model.

The problem I’m having is that the player variable inside the weapon BP gets set to null when the weapon is picked up, and I’m really not sure why. my best guess is that it gets set to null because the player variable is only set when the player enters the sphere collision, and since the sphere’s collision is set to false so that other players cant loot it, it sets the player variable to null.

I’ve tried testing if the guns BP works after it is destroyed via blueprint interfaces and it seems to work, but its just that player variable that doesn’t work.

I’m currently stuck but if anyone has any ideas I’d love to hear them! I’ve attached some images of code below just in case it’s something hidden in plain sight…

[Overlap code, Player ref is a BPI that gets a reference of the player from the target player. This is inside the Weapon BP]

[Interaction code, called from the player’s BP after pressing the interaction key. This is inside the Weapon BP]

[Loot Weapon event that was shown in the previous screenshot. This is inside the Player BP]

[Set w/Notify Looted Weapon, Ive cut out most of this notify because most of it its just spawning the guns based on different conditions. Main thing relating to the problem is this part shown. This is inside the Player BP]

[Set Weapon Player Ref Event, This is inside the Weapon BP]

It always initially returns the player upon pickup. As shown below (Green and dark green messages)

image

but when i call it to print the name via a debug key, it returns null…

If anyone could help me in understanding this it would be great. Thanks.

Even if the weapon might technically work it is very, very bad practice to use anything from a destroyed actor. I don’t really think that I’m capable of stressing that point enough… I assume this is the reason the player variable is null.

Now the other thing that struck me as a bit odd is the fact that the weapon has the responsibility to actually put itself on the player but lets not dwell on that too much.

What I think you should do is just make two player variables in the two newly spawned child BPs, expose them on spawn and set them up before the pickup is destroyed.

How I’ve actually seen it implemented:
Going through a pickup of any kind actually record the pickup in an “inventory” component on the player character and that inventory is then responsible for attaching different meshes and effects on the player character depending on the pickup. That “inventory” is also responsible for destroying the pickup because in the case of “not enough space”, “already at full health” or any other case of inability, the pickup might be left for other characters to pick.

Hope that this helps you somewhat.

P.S.
Don’t ever access destroyed objects and make sure you always validate your references if there is any such possibility.

Hey, hanks for the response. I wanna quickly go over one thing and then try layout some of my understandings as I’m not super experienced in UE coding.

Firstly, the weapon BP isn’t what controls putting the weapon on the player; when an interaction is detected it checks if any conditions may block the weapon from being looted such as the weapon inventory being full or something else like that from within the weapon blueprint, but then it calls an event that resides in the player BP that takes care of spawning the weapons, placing them in the hands, etc…

Second, I’m quite conflicted on how to destroy the actor, I would love to just move it onto the player but with the way I’ve set it up it cant exactly be done because the first person rig needs to have a weapon with only owner see enabled and the third person one needs one with owner no-see enabled… But the most confusing part of it for me is that I’m able to trigger code from the weapon BP using interfaces connecting it to the player, despite it already being destroyed. I assume this is from the weapon I spawned in and set as the main equipped weapon (main equipped weapon is the third person one right now), but since I can call code from it my best guess as to why the player reference is being cleared is because it doesn’t save to the newly spawned ones.

I’m about to go to bed bc its 12AM rn for me, but in the morning ill experiment with this a bit, and ill also take some of what you said in mind and try re-write it to be more optimal.

Sucram237:

I’m quite conflicted on how to destroy the actor…

Usually you don’t “move” the weapon to the player because the pickup thing and the actual weapon differ a ton in terms of functionality even if they look the same. (in reality they often don’t even share a model because the actual weapon needs more detail than the pickup) The other thing is that attaching and moving actors together has a bit of an overhead so what usually happens is you just attach mesh components and weapon components that have the capability to shoot. As explained one is different and the other is not needed in the pickup.

Sucram237:

But the most confusing part of it for me is that I’m able to trigger code from the weapon BP … despite it already being destroyed…

UE5 uses garbage collection, which means that deleted objects are just “marked” for deletion until the garbage collector … ahem… collects them. You have no way of knowing (and you shouldn’t care) when they will actually be deleted from memory but IsValid() will return false if the object is marked or if it’s missing.

I would suggest you look into the First Person Template that comes with UE5. There is a weapon pickup you can explore.

P.S.
The other reason you might be able to call a deleted weapon is if on some of your clients the weapon is not actually destroyed.