How should I create a pick up system?
I am new to unreal engine 4 and I am trying to make a pick up object system like in some games where you will look at an item and then a widget will appear that would say something like "Press E to pick up revolver" and then when you press E the revolver gets added to an inventory. (I already have an inventory, but I'm stuck on the pick up part). I tried using line tracing and a number of other different methods from watch tutorials but I can't seem to find out how to make it correspond to the system I am using of adding it to the inventory which looks to be very unique compared to other people's systems. I really don't want to have to redo my inventory collection system entirely. I'll try my best to explain how it works so maybe someone could help me make a pick up system that works with what I have done.
How it works is for every item I want in the game I add a new actor for it. Right now I have 3 items. A log, an apple, and batteries:
By means of a way I'll explain later, the player will pick it up. (The method I am using is operating with 2 severe bugs which is why I am writing this.).
In the blueprints for each actor I reference variables from the third person characters which are integers: slot 1, slot 2, slot 3, slot 4 and slot 5. The default value of these are set to 0. When the player collects say a log (again I'll explain how this faulty system of collecting works later on into the post) it will check if the value of slot 1 is greater than 1, (meaning that there is already an item in there slot). If there isn't than it will set the value of slot 1 to 1, adding a log to the inventory. If slot 1 is greater than 1, it will move on to slot 2, than slot 3, 4, and to 5. If slot 5 is also full than the inventory is clearly full and nothing will happen after that. Each item has it's own value for the slot. For example, the apple value is always 2, while the log is always 1 and the battery is always 3. :
I have this scripting in each actor blueprint. (Apple):
I also have a system which detects if the inventory is full, with stack able and non stack able items. For example, apples stack up to 4 and batteries stack up to 5 before going into a new slot, but this is immaterial for the problem at hand.
Now, the pick up system.
How it works is first I have a collision sphere attached to the item for every actor BP. When ever the third person character walks into this sphere, a Boolean variable will be set to true, and whenever the third person character exists this collision sphere the Boolean variable will be set to false and will also close a gate but I'll get into that part later. I did this to tell the engine that an item is nearby, by the third person character doesn't necessarily know about, since they could be facing the other direction away from the item. This is how it looks in the blueprints:
After that, in the same blueprint actor for the item I added an event tick, where every tick a branch will detect if that Boolean variable is true. If it is, it will feed into the line trace by component where the target is the component of the item itself.
I placed a branch at the end of this, with the condition being if the line trace has touched the proper item. If it has than I have another Boolean variable that will be set to display the widget which is done through setting a variable true called AppleSeen? Also, I set the subclass to self at the end of this. Which is stored in the third person character. If that branch is false (so the third person character is not touching or has not touched the item with the line trace) than it will remove the widget through the same way of setting the variable of AppleSeen? but this time to false. In this picture you will also see several other variables that will check if the inventory is full but like I said earlier this is not relevant to the problem I have.
After that, a gate will be opened if the item is seen, and closed if the item is not seen. Also the gate will be closed with the third person character leaves the collision sphere because I had encountered a bug where the widget would not disappear. The gate is entered when the third person character presses E. After that, I have some script which we will also skip over that just adds some score to a variable for hunger. Also, this will destroy the actor, and set another Boolean called AppleCollected? but this time it is stored in the blueprints for the item and not the third person character. This will be set to true and then is detected latter through the branch that leads to the slot 1 through 5 inventory system.
Now I have explained how it works I want to explain the issues I am having with the pickup system:
1. The system is collecting more than 1 item at a time. For example, when the Third Person character goes to collect an apple, but there are more than 1 apples near by close to it, all of them will become collected. Which is a very annoying bug.
2. The sphere collision causes an issue to arise when the third person character goes to collect an apple and collects it, I can only pick up the one that is in that sphere collision, forcing me to aim at all nearby apples, to find which exact one I am able to pick up. This bug causes the inventory collection system to become a very forced and unpolished way of picking something up.
If anyone has any idea of how to go about fixing these two issues, or better way to collect these items entirely without having to change the way the inventory system works, please let me know and it would be greatly appreciated. Also, if you would like more information on how the system works just ask and I will provide. Thanks!
Follow this question
Once you sign in you will be able to subscribe for any updates here