x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

alt text

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. :

alt text

I have this scripting in each actor blueprint. (Apple):

alt text

And battery:

alt text

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:

alt text

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!

Product Version: UE 4.14
Tags:
more ▼

asked Jul 17 '18 at 12:42 AM in Blueprint Scripting

avatar image

IsJosh_Baker
1 1 3

avatar image Nebula Games Inc Jul 17 '18 at 10:48 PM

I think you have severely over complicated both your inventory and collection system. But if you prefer to use that inventory system my suggestion is you start from scratch on the "collection" part. It shouldn't require 2 or 3 different booleans if you are using a line trace. Each "pick up" should have a sphere collision around it, on overlap display the widget, on end overlap hide the widget. That doesn't have to be tied to whether or not the player is looking at the object, just have the widget pop up when within range period. Then use a line trace to see where the player is looking. If the line trace hits an object you use the "hit actor" to identify the object hit and add that to the player inventory. So, player walks into an area, overlaps a few spheres, maybe 3 items nearby they all display "Press E to Pick Up". Player can only actually see 1 or 2 of these widgets...no matter. Player presses E, a line trace is sent out and hits an object, that object is added to the inventory and destroyed. Problem solved. To prevent random "E presses" from collecting items the player isn't within range of, you can either have the line trace only go as far as the sphere is in diameter or you could have 1 boolean that is set to true on each actor when the player overlaps the sphere that allows the "E button" line trace to fire.

avatar image mightyenigma Jul 20 '18 at 06:31 PM

Yes this is a good example of why you should learn class inheritance and other object-oriented principles.You can have one blueprint to create many types of items, or have multiple blueprints inherit from one, and have them share the parent class' functionality so you don't have to branch and switch for every single little thing.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question