The secret piano code

Hi. I’m trying to create a secret code on the piano that would trigger an event. My keys are not created in the user interface, they can be pressed using Line Trace. The white and black keys are divided into different blueprints. Now I have displayed the number of the key pressed (the variables are string), but I do not understand how to check the specific sequence in which they were pressed. I have an idea - the user pressed the keys sequentially “24 - 25 - 24 - 13 - 19 -13”, and some function combines all the pressed keys into one common variable with a password (the final version will be 242524131913). Branch then checks this variable to match the correct value of the variable. The second element diagram does the same thing. If everything matches in both the first branch and the second branch, then an event occurs. Otherwise, the value of the variable is reset. But I’m not sure if I’m thinking correctly, if this will work, and how to implement it.



image

you have a password of X digits

keep a list / queue of the last X keys pressed:

  • allot x spaces to your array
  • each time a key is pressed, add it to the head of the list / queue, bump everything back; what is now the x+1th keystroke is lost (expected), we only need the last-x
  • each time a key is pressed, also check each element in the array against your code, in order. you could likely brute-force this with a series of nested if-statements. check the 1st digit, if it’s correct, 2nd, etc, otherwise != (false) will just skip out of the chain

i don’t think you need to be elegant here

2 Likes

Sorry, I don’t know Unreal Engine 4 that well, especially blueprints, and I didn’t understand a lot of your explanation😞
Most of all, I do not understand the second point, in the first, most likely, I did what I needed to do

So on that 2nd picture, you have an array of strings. Make them an array of integers, the actual numbers we are going to check against, not the alphabetic-characters, ‘24’, etc.

You will make a second array of integers, 6 long. When someone clicks on a key, you take the 5th slot and move it to the 6th slot. Take the 4th slot and move it to the 5th slot, the 3rd to the 4th, etc. And the new key takes the 1st slot. This is a queue, like a line of people at the supermarket. When you press a key, add a customer, they go to the end of the line. The order is arbitrary, if we add to the head, or the tail, but in this case we are adding to the head, so slot 1 is the last-key pressed and slot 6 is the 1st key pressed. The idea is we only need to hold the last 6, so we pick a ‘side’ and fill it from there.

Every-time you do that, you press a key and add it to the queue, you compare the combination-array to the key’s-pressed array. Just make sure you account for the order they are each stored in.

Key by key, if the input does NOT match the corresponding key for the combo, then the key pressed is incorrect, so you can just do nothing, drop out of testing. If the key, DOES match, test the next key, then repeat.

If you make it to the end, they pressed the the keys in the correct order.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.