sub Check4DigitEntry (key) 'number pressed, 1st, 2nd, 3rd, 4th of code
If KP1 = 0 and key = fstkey Then
KP1 = 1
ElseIf KP2 = 0 and KP1 = 1 and key = sndkey then : KP2 = 1
ElseIf KP3 = 0 and KP2 = 1 and KP1 = 1 and key = trdkey then : KP3 = 1
ElseIf KP4 = 0 and KP3 = 1 and KP2 = 1 and KP1 = 1 and key = fthkey then KP4 = 1
Else wrongkey = 1
End If
End Sub
How this script works:
First, decide what you want the correct code to be. Load these into the variables fstkey, sndkey, trdkey, fthkey. This is done during initialization when your game first starts.
Next, when the player clicks on a numbered button you need to put in the Advanced Tab the name of the 'check' routine (in this example it would be Check4DigitEntry(key) where key is the number on the button they pressed.
The KP1, KP2, KP3, and KP4 variables keep track of how many buttons the player has clicked on.
When the player clicks on a numbered button, the subroutine checks to see if the player has clicked on a button before (the KP variables) and falls to the appropiate line where it checks to see if the button that was pushed (Key variable passed to the subroutine) matches the number for the code (fstkey, sndkey, etc)
If everything is okay then the variable WrongKey is left as zero otherwise it is set to 1
To finish off the process, I usually have a special button for the player to click on when they think they have the code correct. When they click on this button it calls a separate subroutine:
Sub LockBox1
If KP1 = 1 and KP2 = 1 and KP3 = 1 and KP4 = 1 and WrongKey = 0 then
LockBox1Unlocked = 1
End If
End Sub
This little piece of script checks to make sure that the player clicked on the right number of buttons and that they were all correct. If it finds any problems, it does not make the 'unlocked' variable true.
One very important thing to remember when using this script is to 'zero' the KP variables and the WrongKey variable when the player leaves the frame. You do this in the variables section of the hotspot that lets the player leave the frame ('when player clicks on the hotspot set variables to zero') If you don't, when the player return to try a different code, some of the variables may already be set and the puzzle won't work.
If you want the box or safe or what ever the code puzzle is attached to to open when the player gets the right code then just add an Action.GoToFrame to the script like this:
Sub LockBox1
If KP1 = 1 and KP2 = 1 and KP3 = 1 and KP4 = 1 and WrongKey = 0 then
LockBox1Unlocked = 1
Action.GoToFrame "BoxOpen"
End If
End Sub
Let me know if something is not clear.
Skye