Download

Blueprint opening a door with key

Hi!

I’m searching for a method to make a door unlockeable only with a key which must be found before in the level. If the player finds the key, then he goes to the door, press E and it opens. If he hasn’t have the key, then the door doesn’t open. Anyone has solved anything like this?

Thanks.

Hello,
if you set a boolean variable to false on start and to true when you find the key, then you use this variable as condition in a branch when you try to open the door and set tha variable back to false when the key is used (if it is a one use only key)

Hi , for the exemple i made here you need

a key (can generate overlap events)
a trigger around the door

8dff499824d1f0e042b93bc736ecd356d807a39b.png

f0d1a00831fe3fad57a5e8d92e377708fd509214.png

5f075f174db7d0e8e6e0124173d44669108b0542.png

I would create a key structure, where the key got an unique id. And rather push the key into a keyArray in the controller or Character.
When it is relevant, loop trough, the players keys, check if the id of one of the keys match the id for the lock.

Might be a good blueprint library actually, keymanagment, not quite a inventory but almost.

That also support the idea of consumable keys, aka you could remove it from the array when used.

Yeah, I think we need more details but the above posts describe the two of the three main ways I would handle this from least complex to most complex.

  1. Boolean - The player either has a key or they don’t. The player can only have one key at a time and it can open any locked door, can be removed once the door is opened or left on the player.
  2. Accumulating - The player has a number of keys. The player can open any door as long as they have at least one and one is removed when a door is opened.
  3. Unique - Each key can be used on a specific door or specific type of door.

For 1 and 2, a simple variable is needed (boolean or int). Mhousse1247’s solution can be used for either of these with a slight modification to make it accumulate keys.

For 3, you would need to implement some kind of inventory system. I would say you could make this simple or complex as well.
For a simple solution, create an array of ints. Each int represents a unique key. If a door requires key “142”, run a loop through your array searching for “142”.
For a complex solution, create an array of class “Key”. Make a “Key” blueprint containing a variable with a unique identifier (like an integer with 142). If a door requires key “142”, loop through the array getting the value of the identifier and seeing if it’s 142. The only advantage I can see over the previous method is if you wanted additional properties for the key such as a custom mesh, durability, or… other fun stuff.
You could also change the above to not be restricted to “Key” classes and accept any object reference. You can then use it as a character’s inventory system and have all sorts of stuff in there, not just keys.

I think the nodes are right, but we need a Matinee with the door rotation, right?? Another thing is that i can’t find ‘Cast to MyPlayer’ node. Should I make a Class Blueprint for that?? Thanks a lot for the previous answers!!! :smiley:

You would better use a timeline than a matinee for your door. To have your “cast to” first get a “get player character” then from it drag your "cast to "blueprint character.

In your case ,you cast to your player character bp
And to open the door ,like fen said,better use a timeline for that

Wow, it works perfectly!! Now, if I want that the key attach to me pressing ‘F’ (for example) instead approaching at it, what should i do. Replacing the Begin Overlap from the key with ‘F’ will be enough or i shall do something else?? Thanks a lot!!

F Presses – if(distance between(get player pawn>get world location) and key>get world location)< a certain distance–>then attach to your player pawn → then hasakey is true

Sorry, but i don’t understand. When you have time, can you post a screenshot with the nodes and connections?? Thank you so much!

Im using my phone now , when i get home will post it
By the way if you can cover what exactly you want to do, to make it more clearer

here it is

c4.PNG

This is better than I expected!! Even if add a sound to make the effect of taking the key, the work is complete!!

Well, this is a mechanic for a game which two friends and me are developing as Degree Final Project. Is a survival game located in a moonbase recently destroyed by an alien tribe and we are making all the logic in Blueprint.

Thanks a lot for the node diagram, Mhousse1247

Glad it helped