I have been working on a simple weapon pickup system for my third person game. Basically, I have a hidden “sword” component in my ThirdPersonCharacter, and when you find a sword in the game and pick it up, it sets the component to visible, and then destroys the sword pickup in the game. The sword pickup is sensed with a box collision component called WeaponSensor, the sword pickup is Weapon_sword.
It works about 50% of the time, but the other 50% everything works but the pickup is not destroyed. I get this error:
Blueprint Runtime Error: Accessed None trying to read property K2Node_DynamicCast_AsWeapon_Sword2 from function: ‘ExecuteUbergraph_ThirdPersonCharacter’ from node: DestroyActor in graph: EventGraph in object: ThirdPersonCharacter with description: Accessed None trying to read property K2Node_DynamicCast_AsWeapon_Sword2
What can I do to make the sword pickup be destroyed every time?
Thanks for any help.
Below is my third person character blueprint.
You’re using a pin from a different execution path. DestroyActor is called in the keypress path but the cast-to node is in the end-overlap path. That isn’t recommended.
You should be saving the overlapped pickup when BeginOverlap is triggered and clearing it when EndOverlap is triggered. This gets more complicated when there can be more than one pickup overlapping at the same time. Then you need to keep a list of all the pickups that are currently in range. Then the keypress logic needs to pick one of them from the list somehow (i.e. first closest, random, targeted, etc.).
Thanks for your answer! I’m not sure I understand though.What would that look like in blueprints? For my purposes, there will never be multiple pickups overlapping at the same time. What kind of functions should I use to save the overlapped pickup?
Thanks.
Ok, but what type of variable is PickupInRange? How do I store an actor reference as a variable?
Sorry, I’m still learning. Thanks for all your help though.
You need to add an OnComponentBeginOverlap, cast that actor to “Weapon Sword” and store it in a variable (let’s call it “PickupInRange”). OnComponentEndOverlap should set PickupInRange to None (no input will clear it).
Then on the “E” keypress, Check “IsValid?” on PickupInRange. If it is valid then do your logic to get the sword in the hand of the player, call DestroyActor on PickupInRange, and set PickupInRange to None (no input).
That should do what you want. It’s a good start but the minute there’s two or more in range, this will stop behaving well.
And you won’t need your bool that tells you a pickup is in range. That’s what we’re doing when we set PickupInRange to an actor or None, and check “IsValid?”.
Add a variable to the character blueprint the same way you added the bool “CanPickup”. The type should be the same as the cast-to node. You call it “WeaponSword”.
You should really get a background in using UE4 before attempting to make anything. These are old but they should still be relevant.
Hey,
Thanks for all your help, I figured out what type of variable you were talking about. Sorry for all the questions.
No problem. It’s just I’ve been where you are so I can tell you that it’s much easier to get good at the fundamentals first and then take on challenges. It’s like how you need to be good at dribbling before you should worry about trying to sink baskets. I see a lot of new devs on here trying to make complex games and learning how to do it post by post. It’s not an efficient way to learn and it’s not really what this forum is for. Tutorials and experimentation are how I got better at this stuff.