How to trigger an event after a sequence of keys has been entered?

Hey,

I’m trying to create a puzzle in my 2D game in which the player walks up to a piano and presses E to start playing the piano. Once they’ve pressed E they can press keys on their keyboard (A - G) to play the piano. If the player enters the sequence D, E, B, A, B then a cube nearby has it’s collision changed from BlockAll to NoCollision.

However, I’m having a hard time setting up the sequence for the input keys. I’ve searched for tutorials on this but I can’t seem to find anything and I’ve been trying to figure this out for hours now.

Here’s what I have so far:

So when the player walks up to the piano and presses E a widget appears telling them to input keys on their keyboard to play the piano. Here is where I’m stuck though… how can I get the branch to be true when the player inputs the correct sequence of notes? If they don’t enter the correct sequence of notes they can hit ESC and leave the piano and walk up to it again and try it again.

Any help is appreciated as I’m really stumped on this one!

Thanks for the response Blastertoad! I’ve actually went ahead and turned it into a visible UI. Now when the player reaches the piano and presses E, it calls a widget and displays buttons labeled A - G. If the player presses the buttons D, E, B, A, B in that order they will solve the puzzle. I’ve managed to find out how to do this using variables in the widget event graph:

But since I can’t reference the cube I’m trying to turn the collision off on in a widget event graph, I have to somehow call the variables I made in the widget to the level editor under the condition for the If statement.

I don’t know how I can copy the variables from the widget blueprint to the level blueprint. I’ve made the variables editable and public but I can’t find a way to reference the on click events from the buttons into the level blueprint. Do you know if there’s a way to do this?

Thanks for your help!

Sorry for all the stupid questions - I’m very new to Unreal.

I meant I can’t reference the cube in the widget blueprint. Basically in that second image when the player presses E it first disables their movement and then creates the widget and adds it to the viewport. Then this is when the player is supposed to input the 5 correct notes. If they input the five correct notes then it prints out success (it will set a cube to no collision but I’m only printing for testing purposes) if they input the wrong sequence it prints out failure. I have no problem referencing the cube in the level blueprint, which is why I have to get the player input sequence into the level blueprint. I got it to work in the widget blueprint but I can’t find a way to get it to work in the level blueprint.

I tried calling a reference to the cube in the widget blueprint but Unreal doesn’t allow for that (doesn’t even give me the option to).

Again thanks for your help!

Thanks for the quick response.

The character is the little black guy in the middle and the big square to the left of him is the piano. The cube is currently the black bar to the right of him.

Basically when he walks up to the piano and presses E this interface appears:

When you click on D, E, B, A, B I’d like the black box in the first screenshot to change it’s collision to NoCollision allowing the player to drop into a hole from the floor entering the basement of the area. The widget where this UI isn’t currently attached to anything. The level blueprint calls for the creation of the widget (UI) to appear. Everything is happening in the level blueprint. The problem is that I’ve managed to create the input sequence in the widget since that’s where the buttons are, but I can’t figure out how to do this in the level blueprint since you can’t create an event on click from the buttons since they are in the widget.

Again, thanks for your help!

You could capture the input of the player to an array, and then compare that to an array of the correct notes. Does this with function override On Key Down?

Why can you not ref the cube? After creating the widget cast to it and set the reference as a variable. That way you can then cast to the cube to execute events.

Ok so where is the cube in relation to the character? and what object is the create widget code on? Is the “E” Input command on the Level blueprint?

This is what I currently have in my level blueprint for this puzzle:

Before the branch is where it should check to see if the player has entered the correct sequence of notes… that’s the part I’m stuck on.

Sorry no pics this time as i am now laying in bed. Keep input in the widget that is where it belongs. Inside your widget make a new variable. It should be of the same type as the cube eg. Actor, pawn, ect. As a ref. In the level blueprint after creating the widget cast to “piano prompt widget” and set your new variable. Now in the widget you can use an And bool and a branch to check all keys are pressed. If true cast to the cube, using new variable as input object and set collision false. I can make pics of this in 10hrs or so If you are still having trouble.

Sorry but I still don’t understand - I created the variable as an actor since that is what the cube is in my widget. I’ve then went into my level blueprint and created a cast to piano prompt widget but not sure what to do after that. It just keeps throwing errors even after I’ve set my new variable. I’m not sure what to attach to the object since I’ve never done this before.

Sorry for asking so much, I’m trying to learn as much as I can.

I did a full build of the project and got it working. Here is the video of how I built it. It is a little different from what you have but I explain why in the video. UE4 building a Piano Puzzle - YouTube

Wow thanks so much for the video tutorial! This really helped me out A TON! It turned out to be way more complicated than I thought it was going to be. I followed your tutorial exactly and ended up with it working in 5 notes instead of 6 on the first try for some reason… not sure why. However it does unlock the puzzle in more than one way but that’s fine - I’m sure I can figure that out later on.

Thanks for all the work you’ve put in to answering me, you’ve gained a new subscriber and don’t worry you can steal this puzzle idea, haha. Thanks a lot again, couldn’t have done this without you and I’ll be sure to credit you!

Dear rushjet,

I’m really sorry for such a late response. At the same time, I hope you will appreciate what I have for you. I know the AnswerHub is not working like that and I should have posted it yesterday when only I had a solution. But I really got into it and I treated it as my personal training project.

This was my first decision to answer a question on AnswerHub ever, so I worked on it since yesterday morning. :slight_smile:
It was fun, I hope you will enjoy the results too!

Please, contact me on private message, I will send you the whole project to look into. Maybe I’m not so experienced in UE as some people here, and surely I could do some mistakes, but you will be able to pick up some good ideas of a code flow within UE for the future. I’ve made that sequence puzzle in a clean way I think, you can have more pianos, differ sequences as you like, connect specific walls to open and set everything together without any knowledge of a code (it’s always important to think about level designers you’re working with, for example). The only thing I’m totally not aware of is how it all should be designed to work in multiplayer and replication. My guess was you are doing single player anyway.

Here we go into our Piano class:

Above you can see a function Play Note which is called by UI widget connected to player’s viewport (screenshot no. 5). Now, let’s look at function Check Secret Sequence which is called after every note in Play Note:

This is just a function to call after a sequence was successful, you can do whatever you want here:

Here are showed dependencies within the Piano class, because you had a problem with this. I noted in the picture that I will add something, but I started to think over and I’m not sure so maybe it’s better to shut up.

And PianoInterface, UI Widget connected to player’s viewport. It is actually connecting and disconnecting every time it gains or looses interaction signal (player leaves overlap event area of a piano). That’s why it can get “fresh” Character reference every time from a Construct Event and turn off UI mouse mode at Destruction Event. I noticed that connecting and disconnecting automatically fires those events. I hope it’s a good practice.

I hope I described everything fitting your knowledge level. If you are more advanced than I assumed, I’m sorry. I’m not an expert, really. But I saw you have some basic problems (I had them too not so long ago) and decided you will need some help. Your assets looks interesting, I hope you will continue with programming. Most importantly, don’t give up! I’m slowly building my projects too, so I know the drill, good luck mate! :slight_smile: