Adding an interact button to open a door with a key

Hi

Following on from this thread: - Help with getting door to close after a few seconds from opening - I’m now trying to figure out how to incorporate a button/key press to open the door if I have the right key card. I’ve been wracking my brain trying to figure out how to get it to work, but I’m not very good at this. :sweat: :sweat_smile:

I pick up the right key, I approach the door, and being in the trigger opens it. Now I want to only be able to open the door by pressing an action key. I would prefer all controls to be within my FirstPersonCharacter Blueprint. That’s where all the other controls are for movement, sprinting, jumping, firing a weapon are etc. I’m just not sure how to go about setting up the action function. The further I got was this node:

UnrealEditor_c5lj6bJ7Df

:sweat_smile:

Then in my Keycard Door Blueprint, I have this code to make sure I have the right one for the door before the open/close animation plays. I’m guessing I would need to call an interact function, from the FPC BP, here before the function to open the door triggers?


As well as that, there’s a small issue with the door if I stand underneath it before it closes; it closes with me there and pushes me slightly out of the way rather than stopping. Would I need to add some kind of a trace so it detects the player? I’m not sure how best to implement that.

Thanks

Le bump.

To open with a key id recommend adding a key array or TMap variable for keys to your inventory system. If you don’t have one setup you could just place it in your characters bp as a variable. All you’d have to do is on interact, query the inventory or key array, and open it if the desired key is held. For interacting with the door, you could setup an interface. On interact action call a line trace or get overlapping actors, query whether they implement the door interact interface, and if so call the interact event(Wich would be in the doors BP and execute the above opening functionality)

Well, that’s certainly the theory behind getting it to work, but as a newbie to Blueprints, I’m struggling to visualise and work out what nodes are needed and where. :sweat_smile:

I do have a key array set up that checks if the player has the right one before the door opens:

I’m just struggling to figure out how exactly I’d set up the action key. I’d ideally want it in the FirstPersonCharacter_BP, so it can be checked and called within this Keycard_Door_BP, but I’m just not sure how to proceed.

How do you want it to work?

  • by proximity - the player approaches the door and if the card is present it automatically opens?
  • by looking at the door and pressing the E to open?

One way or another:

  • have the enumerator in the door indicating which key is needed for it to open
  • have a tMap with keys in the player BP
  • either overlap or trace the door, send the interface message to the HitActor, pipe in the player’s key map
  • the door implements the interface and checks the received map against its own enum, and either opens or not

By looking at the door and pressing E to open. It has an OnComponentBeginOverlap trigger, too. I have an Enum set up within the door’s BP to indicate which key is needed, along with a Construction Script on the door to tell it what keycard is assigned to which door with the corresponding coloured material - red, blue, orange

As above then, trace ahead when you press E, send a message to the hit actor. No need for an overlap, seems you do not need it.

Any tutorial on Interface Interaction covers this. It’s one of the more common things to do.

Okay. So is an Interface Interaction Blueprint definitely needed in this case? Or can it be done without? I haven’t had to make one so far, and I suspect this will be the only type of interaction the player can do. Thanks.

You can do it without, if it’s just doors. Trace → Cast → Call. Interface is just neat, hassle free, easily expandable, versatile.

1 Like

Okay, thanks. I’m giving it a try now anyway, and I’ll report back. :slight_smile:

1 Like

No, still struggling to get it working right. :-\ It recognises the interact key being pressed, but the door is still opening once the right key is obtained.

Did you implement the line trace as @Everynone proposed? If so, can you show us the implementation? Just to get a sense of where you’re at right now :slight_smile:

Not a line trace, no. But the Interaction Interface. I did it based on this tutorial: https://www.youtube.com/watch?v=dkeVrlRFJDk

Just not the door animation as I already have that done.

So that’s the part I copied from the video, which is in the FirstPersonCharacter_BP:

And then I’m trying to work out how best to call that function after the right key has been picked up before the door opens:

Alright, let me see if I get this straight:

Your character is somehow overlapping the door (I suppose there’s some sort of collision box or sphere around the door) which indicated that the player is close enough to interact with the door.

Furthermore, your character picked up the correct key to open that specific door (hence your switch).

Now you’ve created the interface. Did you implement the interface in the door actor? The seconds image you’ve uploaded in your previous comment, is that in the event graph of your door actor?

To clarify my assumptions:
It looks to me like your character (in which you have your IA_Interact event) is correctly set up. Your looping your overlapping actors and calling the interface on all actors, if they implement said interface.

But it also looks like your door actor (the first image in your previous comment) have not yet implemented the interface, but instead has a custom event in the event graph, in which you check whether the correct key have been picked up, and here you also try to call the interface.

The first image of your previous post, is that from an event on your door actor, or it that image also from your character?

Consider the following:

  • the interface:

  • door:

  • player:

That’s it, no need for anything else. All pseduoscript but should :crossed_fingers: work as is.

1 Like

That’s correct, yeah - a box collision is around the door. Yep, the character picks up the correct key - a red one for the red door. The Interact_Interface was added in the FirstPerson Blueprints folder and the code in the FirstPersonCharacter_BP (the first image in my last post). The second image is in the Door_BP/Actor.

I’m confused with the first image concerning the Interface. Specifically with the Owned Keys type. I see it has green dots and red lines. Is that… a Map…? This is the TMap you were referring to earlier? I can’t select the Map option, only Single and Array. And if I set that to my Enum containing the three keycards, I can select the Map option, but then it becomes three green dots with yellow lines instead of red. So I’m not sure what I’m doing regarding that. Or if I’ve even done that right.

Plus, the Interact function in my Door_BP doesn’t show that map.

Add it to the Interface function as input, see screenshot above. And here’s working with maps:

You could add just bools, but that’s awkward. Once you wrap your head around tMaps, there’s no looking back. They have too many very efficient applications.

1 Like

On input Event: line trace for objects or channel whatever you choose: hit actor: == (door class) : if true access functions that check for key and open

Okay, I’ve managed to figure out how to set up the Map - Enum and Boolean together - but do I have to create that again in order to use it in the Interact function of my Door_BP and again for the FirstPersonCharacter_BP? The guide you’ve linked does it all within the same Blueprint, so it shows up as a useable variable. In your screenshots it shows up and is used in the three different Blueprints. I’ve noticed you have a section on the left that says Interfaces and you have the Interact function in there. But I’m not sure how you get that.