Pick up an item / Interacting with the item with line trace

Hi, I’m trying to figure out the most optimal solution for picking up they item (Key) and in general interacting with items (Key/Door etc…)
In the beginning I had everything piled up in Level Sequencer as they were interacting somehow quite well, but recoded everything using Blueprint Interface , so I could better attach it to the line trace.
Image for BP_Character : BP-Character — ImgBB
Image for BP_Door: BP-Door — ImgBB
Image for BP_Key: BP-Key — ImgBB

For now everything seems to be working fine, I’m able to pick up the item, get the icon widget working, but I feel like there’s a better way to do this and just somehow made my way around it.
I’d like to know from your experience what kind of problems I’d encounter with this approach, what can be adjusted/fixed/optimized.

Here’s a Video to make it clearer what I’m talking about : Help

Thanks! <3

Also sorry for the mess in blueprints :smiley:

You definitely don’t want to add a widget to the viewport on tick. That will add an infinite number to your screen on top of each other and eventually crash your system.
Instead, what I suggest is use a custom event to add the widget to the viewport and after you interact with the key, you can set the “Has Key” bool in your player character and then run your widget event. You want to use EventTick as rarely as possible and instead run checks after calling other code. For example, every time you press the Interact button, you could check to see if they have the key. Then you’re only calling it when the player interacts with something rather than every frame.

In your door BP, you’ll want to play the sound before you Play the timeline to avoid the sound playing a dozen times and then you won’t need that do once node. Update from a timeline acts similarly to a tick in that it will be called every frame as the timeline updates or plays. So either run code before Play, or run it on complete. Don’t run it from Update unless you actually need the code to update every frame (like the door’s rotation) Keep in mind that blueprint nodes will all be processed in less than 0.1 seconds typically, so it doesn’t matter if the sound is triggered before you trigger the door animation. It will seem like they’re happening at the same time.

Otherwise, most of your code looks fine. The few things I pointed out are just issues everyone runs into when learning BP. There really isn’t a more optimal way to do it beyond that.

2 Likes

Thanks for the reply <3 much appreciate it.

I will try out everything you’ve pointed out and fix everything you’ve pointed out and if I encounter any more complications I’ll let you know.
Thanks again <3

Also , I had this in an event tick earlier so it could always check if the player is looking at the set actors (Interactable objects) to display [Press E to interact]

That caused some problems.
Mainly if I didn’t “Destroy the actor” then the message would persist, for example : The door was open , and [ [Press E to interact] was still showing when looking at it, by the love of jesus I couldn’t erase it from the screen :smiley:
If you could suggest me something how to implement that , considering your advice, preferably without the tick.

1 Like

Well yeah, you don’t ever want to call a function from a tick that you don’t want the game calling multiple times. If a tick fires every frame, and your game is running at 60 fps, then in that small window where your bool is true, your Interact event is going to be called 60 times in a 1 second span. I’ve actually written a tutorial on a simple interaction system for doors, draws etc.

What I do is I create a Master “Interactable” object and then from my line trace, I see if the hit actor is one of the “Interactable” objects. If it is, then run the Interact function. If it’s not, check to see if it’s the next useable thing.

And you don’t need the line trace to fire on tick, just when the user presses the Interact button. This way it’s less expensive. The only reason you’d run the line trace on tick is if you’re adding a highlight shader to the objects that are interactable. Then you could run the trace on tick, so you’d always have a reference to the object the player is looking at but you’d run the interact code on a button press so the line trace is more light weight.

1 Like

To add a bit, this could be an option if you want to have multiply doors each with their respective key:

  • You could have both the key and door communicate with the GameInstance; in GI have a list variable where the FName is the id that will be shared with both a specific key and door.
    image

  • In Game Instance 2 functions that will be the setter and the getter:
    Store what key player has picked up:


    Search if player has picked up the requiered key:

  • In the key and door BP have a FName variable with an id e.g. ‘Door_A001’, that on key overlap, will set the bool to true in the map in Game Instance:

  • Now the door bp only has to check with game instance if the FName was added:


DoorWKey


This should work nicely across levels and save game too.

Thanks for all the info, I would be lying if I said I understood everything tho :smiley:
The functions are a bit confusing for me, for now, as I almost never touched them, it’s like an unexplored area for me.

So… before you replied what I did to somehow manage this was , created a billboard in front of my character and on event tick line trace checks if it’s an interactable object and simply set visibility and then invisibility.

I wanted to somehow do that without the tick but wasn’t able to, Is there anyway to… I dont know, “Imitate constant check” without using the tick to not overload the system?

You could achieve something similar with overlaps and a bool:


This would be how to BP handles the overlap:

DoorWKeyOverlap


Updated the above with some comments.