Page 1 of 1

Simple script for a numerical code

PostPosted: Mon Nov 07, 2005 12:55 pm
by Skye
I'll list the script for checking a 4 digit code - it's simple enough then to cut back the script for combinations with fewer numbers required.

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

PostPosted: Mon Nov 07, 2005 10:18 pm
by Candle
Thanks for the tut Skye, I'm still trying to understand it.
But I thank you for doing it.

PostPosted: Tue Nov 08, 2005 5:49 am
by Skye
Hey Candle,

If there is something you can't figure out don't be shy about asking. It can be difficult to make a tutorial that everyone can understand. We are all different people and what is simple to one person be be utter Greek (with apologies to any Greek member here) to someone else.

Skye

PostPosted: Tue Nov 08, 2005 6:00 am
by Candle
If you could make a small demo showing something with it working that would be good .
Upload to some free site and give me the and I'll put it on my server.

PostPosted: Tue Nov 08, 2005 6:51 am
by Skye
Great! I'll see what I can do in the next few days.

Skye

PostPosted: Sun Nov 13, 2005 10:02 pm
by Skye
I made up a little AM project to illustrate how to set up a code box using the code snips I posted above.

You will find the tutorial at:
http://members.aol.com/agre107/CodeBoxTutorial.zip

Let me know if there are any problems or questions.

Skye

PostPosted: Sun Nov 13, 2005 11:46 pm
by Candle
Thank you , that will help a lot as I can look at how it is done . it is so hard for me to remember stuff now.
I need something to open and look at and go back to when I working on it.

PostPosted: Mon Nov 14, 2005 3:44 am
by Skye
You're welcome.

I too have trouble remembering how to do stuff. I have so many pieces of paper with techniques and my own personal notes scribbled on them . . .

Oh well, what ever works eh?

Skye

PostPosted: Mon Nov 21, 2005 5:16 pm
by Candle
I need your help. I sent you a PM.

PostPosted: Sun Dec 02, 2007 9:06 am
by Mystery
Finally I've understood how this script works 8)
I've adapted it for a puzzle with even 8 buttons, and it works like charm, so thanks for this tutorial Skye :D

PostPosted: Mon Feb 25, 2008 7:26 pm
by Lyberodoggy
Skye wrote:Hey Candle,

If there is something you can't figure out don't be shy about asking. It can be difficult to make a tutorial that everyone can understand. We are all different people and what is simple to one person be be utter Greek (with apologies to any Greek member here) to someone else.

Skye


Yeah, I am Greek, but don't mind... We say the same for chinese (we couldn't say it for greek, because it's our mother-language :P)

PostPosted: Tue Feb 26, 2008 7:54 pm
by Chromegloss55
Nice technique, do you mind if I include it in my 100 Visual Basic Techniques guide? (I'll give you credit of course!)
_________________
chocolate chunk strain

PostPosted: Sat Dec 26, 2009 9:50 am
by Mystery
Does anyone have this demo?
I had it on my old machine. It's not available anymore from the provided download link :(

PostPosted: Sat Dec 26, 2009 8:33 pm
by Imari

PostPosted: Sat Dec 26, 2009 8:45 pm
by Mystery
Oh, you have it? :D Thank you very much :)

PostPosted: Sat Dec 26, 2009 11:49 pm
by Imari
Mystery, if you get an error message, try again later. A lot of people use Rapidshare to share Xmas photos with family and the site gets too busy for free downloads.

Re: Simple script for a numerical code

PostPosted: Tue Jan 06, 2015 6:42 pm
by igelkott
Hello everyone!
I trying to do this script for 4-digit puzzle - does not work without an example, and all the links are dead...
Is somebody has this "Tutorial-CodeBox"?

Re: Simple script for a numerical code

PostPosted: Wed Jan 07, 2015 2:34 am
by reneuend
Welcome igelkott!

I, personally haven't reviewed this. If you have a few days, I'll review it and build an example or ....


Here is something I wrote that will achieve the same objective and in my humble opinion, is easier to modify for any number of sequences. The post comes with instructions and the project sample.

http://amforums.cloudapp.net/phpbb/viewtopic.php?f=10&t=6080&sid=51772a63a0c25a836050836c55ce1920

Glad to have a new member on the forums. :wink:

Re: Simple script for a numerical code

PostPosted: Wed Jan 07, 2015 8:29 pm
by igelkott
Thank you, reneuend!
I will learn a lesson on this link while waiting for Tutorial-CodeBox.
...It's a very interesting - to make games!

Re: Simple script for a numerical code

PostPosted: Thu Jan 08, 2015 2:14 am
by reneuend
Here is the Project in a Zip file.
Just add it to your Projects folder for AdventureMaker is located and open it up from AdventureMaker.

http://www.hickchickgames.com/Other/ComboLock.zip

I added a few things:
- sound for key clicks
- sound when safe is opened
- messages
- alphabending and delay to the 2nd frame

I didn't use the LockBox1Unlocked variable, but in a game, you might want to use it if you need to know that the player opened the safe later on in the game.

If you struggle with understanding it, let me know. I'll shuffle you to my solution in the AM Magazine! :wink: