Anatomy of the Passcode System


As this project grew and evolved over the short course of my working on it, I noticed one particular aspect of my code was more involved than the others. The code for the passcode system took me by far the longest amount of time to get working, and seeing as how I wanted to try making it using just my understanding of how Unreal Engine works, I think it turned out alright. At the very least, I want to display how the code works, and some of the reasons behind why the code is written how it is.

The process starts here, in the Level Blueprint. When the level first loads in, it will call the Check Lights function (a script that will check for the lights to be on, not very relevant in this case), then it will find each Sign object in the level and put them into an actor variable array for later use. Finally, it calls the Update Code function which is the first part of creating the passcode.

Admittedly, this took me a few tries to get right. Actually, it took me more than a few tries to get working period. A few points to note before getting into the code itself: this is where my Number Array and Letter Array variables will be used. The Number Array contains 6 items, the integers 1 through 6 in that order, and the Letter Array is a series of 6 strings holding the letters A through F in that order.

When the Update Code function is called, it first shuffles the Number Array so that all 6 numbers aren't all in numerical sequence. It then goes into a For Each loop using the Letter Array, with each loop adding something to the new variable "Code". The Code variable is a map between strings and integers. The For Each loop takes each element from the Letter Array and pairs it with the element from the now shuffled Number Array that matches its Index, which is then added into the Code map. Once the For Each loop is done, each of the previously saved Sign actors are called from their array, and their Ref Code events are called, each referencing the Code map. The level then moves on to the Assign Code script.

I generated the code in this way for two distinct reasons. Crucially, I needed each pairing to be unique, meaning there could not be any repeat numbers or letters within the passcode. This is due to how the player inputs the code, having any repeats would make the process an infuriating guessing game rather than a puzzle that feels rewarding to solve. Secondly, I wanted the code to be concise. I could have just as easily set a string of events in place to set separate variables to values in the Number Array and deleting the array values once used, but that starts to look like spaghetti after too long, and becomes almost unreadable.

So first, I'll take a look at the called Ref Code event dispatcher and its linked DisplayCode event within the Sign asset. This is a fairly simple process. I first set the Code map from the level as a local variable in the asset just in case it needed to be used later. Then the map is broken down into an array of its Keys (the strings) and its Values (the integers), getting the value of each array that matches the index number from the public Sign Index variable. Each Sign's Sign Index is unique and set per instance in the level itself, and is used to guarantee each sign says something unique. The retrieved Key and Value are then appended into a single string separated by an equals symbol, which is then sent to the Text Render component to be displayed in the world.

Next up in the process is the AssignCode function. This was not a fun process to figure out, but I think it was very rewarding when it started working properly. The function starts by separating Keys and Values again, and leads into a double loop. The inner loop (the For Each loop) checks each element from the Values array against the Code Length variable (default value is 1). If the values match, the loop will take the index number of that value and get the corresponding Key, whose letter then gets appended into the Passcode string variable. After the Passcode string gets updated, the value of Code Length gets incremented, and the loop continues. The outer While loop ensures that this loop doesn't stop until all 6 letters are put into the Passcode, only stopping the loop once the Code Length variable equals 7. Once the loops are finished, the new Passcode string is sent over to the Keypad actor so it can use it.

This code serves exactly one purpose, to shuffle the order the Code map from being alphabetically sorted to numerically sorted. It also then gets rid of the integer portion of the maps, since the numbers are implied by the position of the letters in the string. In hindsight, this could have been mitigated by creating the Code map the other way around (using integers as keys and strings as values), however I'm not certain it would have helped, and the code runs as intended regardless. Also, the reason the code needed to be numerically sorted is again tied to user experience. Since the keypad uses letters A through F as its buttons, this ensures the puzzle isn't solved by the player submitting "ABCDEF" as the solution.

Speaking of the keypad, this is the portion of the code in it that uses the passcode. It isn't much at all, really. When the Keypad UI is made the Keypad actor transfers the code into that instance so that it can be used in there. It's very roundabout, but it works.

Finally, the last stop on the passcode's journey is in the UI. The portion on the right uses the On Click event of each of the separate buttons to append its respective letter into the Code Input string. Then another Length Check integer variable is incremented and the game checks to see if the code thus far is valid using the Code Is Valid event.

When called, the Code Is Valid will first check to see if the code is the right length using the Length Check variable. If the code isn't 6 characters long, the code won't bother seeing if its correct or not. If it is 6 characters long, however, the code will compare the Code Input string and the Passcode string to see if they match. If they do not, the Length Check and the Code Input are reset so the player can try again. If they are a match, a Boolean marking the puzzles completion will be set to True, and the door that needed the passcode will be opened. From there, the password is no longer necessary, and the player is free to move on to the next objective.

Get Attraction

Leave a comment

Log in with itch.io to leave a comment.