I’m playing with the first person template and trying to add multiple props that work like guns to my game. But when I pick up multiple guns, they get stuck together and act like I’m holding them both (like shooting two bullets at once). Maybe the problem is an “Add BP Weapon Component” node I put there to allow multiple guns. Is there perhaps a way to get the old gun to go away when I pick up a new one? The “Add BP Weapon Component” node is connected with the input Exec and Target to a “GET First Person Character Reference” node and with the output Exec and Return Value connected to a “Set Skeletal Mesh Asset” node. the node is in the BP_Pickup_Rival Blueprint Class. Does a Change or Remove BP Weapon Component node exist? I am new to game development and I still have to learn a lot about unreal. Thanks for your help!
The original BP_Weapon_Component is still attached when you add another, which also has its own shooting logic which causes the double shot.
The reason the “Add BP Weapon Component” node itself exists is because the BP_Weapon_Component created for the template is a child of Skeletal Mesh Component. Components, such as a Skeletal Mesh Component, can be added to other BPs. (If you open the BP_FirstPersonCharacter, you can see it is made up of several components already such as a capsule component, camera component, arrow component, etc.)
Instead of being called an “Add Component” node, it adopts the name of the component being added, thus displaying as “Add BP Weapon Component.” BP_Weapon_Component is just a component that was created for the template as an example of what could be done to represent a “weapon.”
What happens once the BP_Weapon_Component is spawned into the world (by overlapping the pickup that runs that add node):
-
The BP_Weapon_Component that’s been attached has its own Begin Play event then looks at its owner (the BP it was added to, your first person character) and attaches itself to the owner character’s “First Person Mesh” at the “GripPoint” socket of its mesh
-
Sets the HasRifle boolean to true so the FirstPersonMesh’s animation blueprint adjusts the animations accordingly to appear to hold a rifle.
-
Adds an input mapping context to the player controller controlling/possessing the first person character, which sets up the input for being able to shoot on the controller via the enhanced input system.
It knows what the “First Person Mesh” is to attach to, because of the “cast” of the owner to the BP_FirstPersonCharacter so it can assume the owner is that BP. If the owner was not a BP_FirstPersonCharacter/some other BP, the cast would fail.
There is no additional logic already written in this template to remove or replace the added component. So removal/replacement will need to be created.
Removing the weapon would usually involve undoing/reversing what was done when adding, prior to adding another. You might want to put that logic in the character itself, rather than letting the weapon do it, though it’s up to you.
Some example logic looks like this (Note: These are multiple ideas to essentially do the same thing to get the component you want to remove, but all of them will lead to undoing what was done (ie: destroying the component that got added, setting HasRifle to false, and removing the input mapping context that was added). This isn’t an exhaustive list, but it should give you an idea.)
You could put it into an event of the BP_FirstPersonCharacter via “Add Custom Event” and call it something like “RemoveWeapon”, then in each weapon pickup, you could just pull a pin off the First Person Character Reference in their begin overlap and call Remove Weapon prior to adding the next BP_Weapon_Component.
Example:
Thanks! The code works! You are an absolute legend! I hope that one day, when my game is done, i can put you in the credits! Unreal engine however, does give this error code: “Blueprint Runtime Error: “Attempted to access NODE_AddBP_Weapon_Component-0_0 via property CallFunc_Array_Get_Item, but NODE_AddBP_Weapon_Component-0_0 is not valid (pending kill or garbage)”. Node: Destroy Component Graph: EventGraph Function: Execute Ubergraph BP First Person Character Blueprint: BP_FirstPersonCharacter”.
I could not find the Validated Get Node, CAn you explain to me step by step how te get it and how to store the current weapon into a variable? Thanks for all the amazing work!
Sorry if the multiple options I scrambled together in my previous screenshot was a bit cluttered and confusing.
To save the added component to a variable, you can just drag off the output pin from the add BP Weapon Component node and select “Promote to variable” from the context menu. This will create the variable and also create a “set” node for it.
That way it’ll grab the newly created component and save it to the variable immediately for you to keep track of.
To get a “validated get” you can make a normal “get” of the variable and then right-click the variable in your graph and select Convert to Validated Get
from the context menu.
You could use it in the newly created “RemoveWeapon” event like this:
I added an “is component being destroyed” check since I noticed your error above mentioning something was already in the process of being destroyed.
I tried to implement the new code but it gave more error messages. I think I have misunderstoud the code.
Blueprint Runtime Error: “Attempted to access NODE_AddBP_Weapon_Component-0 via property CallFunc_Array_Get_Item, but NODE_AddBP_Weapon_Component-0 is not valid (pending kill or garbage)”. Node: Destroy Component Graph: EventGraph Function: Execute Ubergraph BP First Person Character Blueprint: BP_FirstPersonCharacter
Blueprint Runtime Error: “Attempted to access NODE_AddBP_Weapon_Component-0_0 via property CallFunc_Array_Get_Item, but NODE_AddBP_Weapon_Component-0_0 is not valid (pending kill or garbage)”. Node: Destroy Component Graph: EventGraph Function: Execute Ubergraph BP First Person Character Blueprint: BP_FirstPersonCharacter
Thanks for your efford!
You’ve got it! The problem is actually just from the alternatives I showed previously. They got included in your code, and even though they’re not fully hooked up on the exec/execution (white) path, the other connections with the pins that are connected are confusing the system and throwing errors. This is a good example of needing to be cautious with leftover blueprints.
You just need the following (also note that the validated get is doing the same thing as the previously used “is valid” node, making the “is valid” node unnecessary, and the “get child component” node is also unnecessary since the component is now already available as the variable):
It worked! Thank you so much! Thank you for helping me!
Happy to help!
Quick little note regarding my previous screenshot, in immediate retrospect, the “is component being destroyed” node and the connected branch are unnecessary as an “is valid” or “validated get” already accommodates checking for pending destruction.
The Is Not Valid path could also even be skipped on that note.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.