How to use keycard to unlock door?

Greetings!

First off: I’m aware there are one or two questions with answers for approximately the same question already, however, neither of them have actually made me understand what I need to know in order to get this to work. Thus, I’m trying with my specific situation:

I’m trying to create a door blueprint class that only opens when the Player has found a keycard to unlock it. I’ve tried all sorts of different (wrong, haha) combinations of bools and what not, but I’m just not knowledgeable enough to get it to work. So now I’ve stripped all the mess down to something as easy as

  • when Player overlaps the collision box he can press F to toggle between a red and green light.

This works in itself, but I need the Player to -only- be able to toggle these lights -after- having picked up the keycard, which (I guess) would be measured by the keycard actor being destroyed.

Toggle lights blueprint:

Keycard blueprint:

22553-keycard+blueprint.png

I think it would be as easy as inserting a bool after the trigger box-overlap saying something like “if keycard actor is destroyed, then enable input = true”. But after everything I’ve tried, it’s obvious I have no idea how to do it.

Looking forward to any enlightening answers at all!

Hi there, if the game you are making is single player only I would set a bool true when the player has acquired the card. Then create a blueprint interface to allow you to create a custom event that only fires if the bool mentioned earlier is true. Then put the event into the doors blueprint as the trigger. Does that make sense? It’s just a rough idea but it should do the trick. I can provide a more detailed explanation if you need.

This way may be a bit involved for your purposes but i like to design my things to be flexible and reusable, so here is my solution:

Video provided by 3xist3nc3: UE4 Tutorial | Use Keycard to Unlock Door - YouTube

  1. First create a blueprint interface called “InventoryInterface” (or whatever you like) and create two functions, one called “add inventory” and one called “check inventory”.
  2. create an input on “add inventory” called “item”(string).
  3. create an input on “check inventory” called “item”(string) and an output called “exists”(boolean).
  4. Implement the interface in your character blueprint.
  5. in the character blueprint add a variable called “inventory”(string) and make it an array (press the grid next to it), then go to the event graph add this:

22573-keycardchareventgraph.png

  1. There should also be a function within your character blueprint called check inventory that was created automatically after implementing the interface, edit that to look like this:

22574-keycardcharchkinv.png

  1. within your keycard blueprint modify to look like this:

  1. finally in the blueprint of the object that should react to you possessing the keycard add a “check inventory” message and branch, similar to this:

This should do exactly what you want, but allow you yo have a scalable solution so you can have many doors or other objects that react to items in your inventory, note in this case in the “add inventory” and “check inventory” nodes i’ve just used “keycard” as the objects inventory reference, this can be anything you want but use unique names for add inventory unless you want multiple keycards to activate the same object.

Thanks a great big bunch, man! Truly appreciate the detailed explanation with screenshots and everything :slight_smile:

Now, I believe I have set it all up correctly, but unfortunately it’s crashing the engine every time I try to play the level to see if it actually works. The crash report says, a.o.:

“Access violation - code c0000005 (first/second chance not available)”

Don’t expect you to know what it is, but at least that’s where I’m stuck now; right at the edge of possibly finally having a working key-solution, the engine just won’t let me have the satisfaction, hehe.

I’ll let you know when/if I get it to work again.

Thanks, man! If the solution Glotty provided just keeps crashing the engine with no way of fixing it, I’ll come back and try yours instead :slight_smile:

Does it happen during play? if it’s during play at what point?

I don’t get to play at all. As soon as I click one of the “Play” options (Play in editor, external window, etc.) it crashes before even starting the level.

The only thing I can think is that the variables are not setup correctly as I use this code currently but will be tweaking it a fair bit. Can you tell me how you have setup the variables used in the code I gave you? Also are the overlap nodes associated with a trigger box inside of your key card blueprint?

Hi 3xist3nc3,

I just tried Glotty’s inventory solution and it worked great. Try his suggestions above and then if the editor keeps crashing, please feel free to post your full Callstack, LOG, DMP files and we will take a look. Directions on how to find this info is linked below.

Thanks. Okay, Glotty, so just to be sure, I started a clean project to only test this, without anything else potentially interfering with the blueprint.

I created a static mesh blueprint “LightVial” which, when picked up, will toggle a point light called “LIGHT” when overlapping its triggerbox. Thus far I am able to pick up the LightVial, but it does not toggle the light. I’ve made sure the toggle-functionality is correct by simply toggling on overlap first, and it is. So I think there’s probably just a tiny noob-thing I’m missing out on, perhaps within the InventoryInterface. Here’s a few images:

(The player character blueprint is equal to yours, so not including it here)

The LightVial blueprint:

The LIGHT blueprint:

The functions within the InventoryInterface:

This is where I suspect I’m doing something wrong, because I have no idea what to name the “default value”.

That looks fine, so long as you made “inventory” a string array I imagine this should work, TJ Ballard may be to help you from here as it could be an unrelated issue.

Sure thing :slight_smile:

could you please post a screen shot of the check inventory function?

I put a print string on both the true and false on the branch, and it turns out it goes to false no matter if I pick up the LightVial or not, so it’s as if it doesn’t register it.

zakman186, here’s the check inventory:

23056-check+inventory+in+player+character.png

And just in case, here’s how it looks in the event graph:

23058-event+graph.png

Try a breakpoint on the pickup to see if its being added to the inventory array.

Never used breakpoints before, so not sure where it should be placed inside the pickup graph, but tried on Add Inventory. Looks like this:

That should be fine now you can check values for target and step through to make sure the node for adding to inventory is getting called and inputting the right value to the inventory array.
You can check values by hovering over the pins.

Hey guys i have made a scene where you pickup an object, add it to an array and on overlap with a light it will check the inventory for the item and if true will change color. I did a few things differently to Glotty but its basically the same.
Light BP

Pickup BP

I did check inventory in its own function but i did not have the add inventory in its own function. I just wanted to get it working mainly so this isn’t very pretty but it works. All that is different in the my character BP is the add inventory is in the event graph.

23072-mcbp.png

Please tell me if I left anything out. It’s 11:30 PM here so I’m not sure whether I’m including everything you need to know or not haha

Glotty: So I tried with breakpoints. It doesn’t seem like anything is actually being registered other than overlapping the collision box. Therefore, instead of us using hours of explaining with slow words and screenshots what I do wrong, I simply created a video for you setting up your system from scratch, following every step you wrote. Hopefully you will be able to easily spot where I’m making a mistake, and can just point it out at the right time in the video. Here it is:

I really need to learn how to do this correctly, as it is a KEY (haha) feature of the game I’m working on!

zakman186: I have tried your solution as well, but only having screenshots and no step by step to go by makes it a bit difficult to know what to do. Which means, I didn’t get it to work. I created a video for you trying your method, so you can see where I’m doing it wrong:

Looking forward to your answers, peeps!

Hi Ben, after watching your video I can see how you got confused, I was a bit vague on the method but I wasn’t really sure how much i needed to specifically word out but now I have a much better idea. First and foremost I am using version 4.6 but im pretty sure that bind = assign. Not sure what the difference is but i have seen it be both in different projects of mine and still function the same way so don’t worry about that. The Picked Up function you saw was my Add Inventory equivalent. My check inventory fucntion is the same as yours, have a look after you posted "zakman186, here’s the check inventory: ". Its exactly the same as yours. Also look closely at the Check Inventory node in my Light BP. It isn’t the Blueprint interface node, its directly referenced to MyCharacter. At this point that wont solve the problem because that node isn’t even being executed but will have to be changed. All in all, it looks like you have done it just right. The problem looks to me that the overlap events aren’t firing on the vial. That could be because one or both of the colliding shapes (your character and your collision shape on the vial) might not be generating events, and if they are check the collision settings on your pawn blueprint (MyPlayerCharacter):

23152-char+coll.png

Your light vial:

23153-vial+coll.png

and your light:

23154-light+coll.png

Now as for the failed cast the only thing i can think of is you havent set your desired pawn (MyPlayerCharacter) in the GameMode blueprint. I think its possible that you are using a different pawn. Ever since they added the override capability for the GameMode its pretty easy to accidentally select the override instead of the main. Double check that the pawn you are using is MyPlayerCharacter. If there are other possible reasons as to why the cast is failing, i am unaware of them.