Digit Code

Revision as of 09:02, 13 April 2010 by imported>Darkness X (New page: ==Introduction== In this article we are going to create a script that asks the player to enter a code. The code exists of four numbers, if the player has entered the right code, the contai...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

IntroductionEdit

In this article we are going to create a script that asks the player to enter a code. The code exists of four numbers, if the player has entered the right code, the container (or door) will activate/open. If the player enters the wrong code, the container (or door) stays closed. Optional features are also possible:

  • Limit the number of failed attempts. (only if the entered code is wrong) (Default in this tutorial is 3) If the player has failed three times to open the container/door, it will be closed permanently.
  • Activating a trap if the player enters the wrong code. (This isn’t applied on this script!)
  • Casting a spell if the player enters the wrong code. (This isn’t applied on this script!)

The ScriptEdit

This is the script for the container (or door ...). Upon activation, it asks to choose 4 values, those values represent the digits for the code. This messagebox script deals with multiple menus in the OnActivate and Gamemode blocks. Due to Wiki limitations the messagebox functions are not on one line, but they should be.

IntegersEdit

Scn 000DigitCode

Short Choosing

Short Choice; choices used by messageboxes
Short ChoiceDigit1
Short ChoiceDigit2
Short ChoiceDigit3
Short ChoiceDigit4

Short DoOnce ; It won’t ask to enter the code again if the player entered the right code.

Short AttemptCount ; the amount of attempts the player has left

Short FirstAttempt ; if this is the first attempt the AttemptCount will be set to 3

Short Digit1 ; these are used to determine the values of the code
Short Digit2
Short Digit3
Short Digit4

OnActivate BlockEdit

Begin OnActivate
	If FirstAttempt == 0
		Set AttemptCount to 3
		set FirstAttempt to 1
	Endif

	If DoOnce == 0 && AttemptCount != 0
		Set Choosing to -1
	Elseif DoOnce == 0 && AttemptCount == 0
		Messagebox "The device for opening this door/chest seems to be broken ... "
	Elseif DoOnce == 1
          Activate
     Endif
End

Gamemode BlockEdit

Begin Gamemode
	If (Choosing == -1)
		Messagebox "It appears to be secured with a code. Enter the digits (currently %g%g%g%g)", Digit1, Digit2, Digit3, Digit4, "First Number", "Second Number", "Third Number", "Fourth Number", "Enter", "Cancel"
		set Choosing to 1
		set Choice to GetButtonPressed
	elseif (Choosing == 1)
		if (Choice == -1)
			set Choice to GetButtonPressed
		elseif (Choice == 0)
			set Choosing to -10
		elseif (Choice == 1)
			set Choosing to -20
		elseif (Choice == 2)
			set Choosing to -30
		elseif (Choice == 3)
			set Choosing to -40
		elseif (Choice == 4)
			set Choosing to -50
		elseif (Choice == 5)
			set Choosing to 0
		endif
	Endif

First DigitEdit

	If (Choosing == -10)
		Messagebox "What is the first number? (Currently %g)", Digit1, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
		set Choosing to 10
		set ChoiceDigit1 to GetButtonPressed
	elseif (Choosing == 10)
		if (ChoiceDigit1 == -1)
			set ChoiceDigit1 to GetButtonPressed
		elseif (ChoiceDigit1 == 0)
			set Digit1 to 0
			set Choosing to -1
		elseif (ChoiceDigit1 == 1)
			set Digit1 to 1
			set Choosing to -1
		elseif (ChoiceDigit1 == 2)
			set Digit1 to 2
			set Choosing to -1
		elseif (ChoiceDigit1 == 3)
			set Digit1 to 3
			set Choosing to -1
		elseif (ChoiceDigit1 == 4)
			set Digit1 to 4
			set Choosing to -1
		elseif (ChoiceDigit1 == 5) 
			set Digit1 to 5
			set Choosing to -1
		elseif (ChoiceDigit1 == 6)
			set Digit1 to 6
			set Choosing to -1
		elseif (ChoiceDigit1 == 7)
			set Digit1 to 7
			set Choosing to -1
		elseif (ChoiceDigit1 == 8)
			set Digit1 to 8
			set Choosing to -1
		elseif (ChoiceDigit1 == 9)
			set Digit1 to 9
			set Choosing to -1
		endif
	Endif

Second DigitEdit

	If (Choosing == -20)
		Messagebox "What is the second number? (Currently %g)", Digit2, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
		set Choosing to 20
		set ChoiceDigit2 to GetButtonPressed
	elseif (Choosing == 20)
		if (ChoiceDigit2 == -1)
			set ChoiceDigit2 to GetButtonPressed
		elseif (ChoiceDigit2 == 0)
			set Digit2 to 0
			set Choosing to -1
		elseif (ChoiceDigit2 == 1)
			set Digit2 to 1
			set Choosing to -1
		elseif (ChoiceDigit2 == 2)
			set Digit2 to 2
			set Choosing to -1
		elseif (ChoiceDigit2 == 3)
			set Digit2 to 3
			set Choosing to -1
		elseif (ChoiceDigit2 == 4)
			set Digit2 to 4
			set Choosing to -1
		elseif (ChoiceDigit2 == 5) 
			set Digit2 to 5
			set Choosing to -1
		elseif (ChoiceDigit2 == 6)
			set Digit2 to 6
			set Choosing to -1
		elseif (ChoiceDigit2 == 7)
			set Digit2 to 7
			set Choosing to -1
		elseif (ChoiceDigit2 == 8)
			set Digit2 to 8
			set Choosing to -1
		elseif (ChoiceDigit2 == 9)
			set Digit2 to 9
			set Choosing to -1
		endif
	Endif

Third DigitEdit

	If (Choosing == -30)
		Messagebox "What is the third number? (Currently %g)", Digit3, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
		set Choosing to 30
		set ChoiceDigit3 to GetButtonPressed
	elseif (Choosing == 30)
		if (ChoiceDigit3 == -1)
			set ChoiceDigit3 to GetButtonPressed
		elseif (ChoiceDigit3 == 0)
			set Digit3 to 0
			set Choosing to -1
		elseif (ChoiceDigit3 == 1)
			set Digit3 to 1
			set Choosing to -1
		elseif (ChoiceDigit3 == 2)
			set Digit3 to 2
			set Choosing to -1
		elseif (ChoiceDigit3 == 3)
			set Digit3 to 3
			set Choosing to -1
		elseif (ChoiceDigit3 == 4)
			set Digit3 to 4
			set Choosing to -1
		elseif (ChoiceDigit3 == 5) 
			set Digit3 to 5
			set Choosing to -1
		elseif (ChoiceDigit3 == 6)
			set Digit3 to 6
			set Choosing to -1
		elseif (ChoiceDigit3 == 7)
			set Digit3 to 7
			set Choosing to -1
		elseif (ChoiceDigit3 == 8)
			set Digit3 to 8
			set Choosing to -1
		elseif (ChoiceDigit3 == 9)
			set Digit3 to 9
			set Choosing to -1
		endif
	Endif

Fourth DigitEdit

	If (Choosing == -40)
		Messagebox "What is the fourth number? (Currently %g)", Digit4, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
		set Choosing to 40
		set ChoiceDigit4 to GetButtonPressed
	elseif (Choosing == 40)
		if (ChoiceDigit4 == -1)
			set ChoiceDigit4 to GetButtonPressed
		elseif (ChoiceDigit4 == 0)
			set Digit4 to 0
			set Choosing to -1
		elseif (ChoiceDigit4 == 1)
			set Digit4 to 1
			set Choosing to -1
		elseif (ChoiceDigit4 == 2)
			set Digit4 to 2
			set Choosing to -1
		elseif (ChoiceDigit4 == 3)
			set Digit4 to 3
			set Choosing to -1
		elseif (ChoiceDigit4 == 4)
			set Digit4 to 4
			set Choosing to -1
		elseif (ChoiceDigit4 == 5) 
			set Digit4 to 5
			set Choosing to -1
		elseif (ChoiceDigit4 == 6)
			set Digit4 to 6
			set Choosing to -1
		elseif (ChoiceDigit4 == 7)
			set Digit4 to 7
			set Choosing to -1
		elseif (ChoiceDigit4 == 8)
			set Digit4 to 8
			set Choosing to -1
		elseif (ChoiceDigit4 == 9)
			set Digit4 to 9
			set Choosing to -1
		endif
	Endif

Enter CodeEdit

	If (Choosing == -50)
		If Digit1 == 4 && Digit2 == 5 && Digit3 == 9 && Digit4 == 5
			Message "You succesfully opened the door/chest!"
			Set DoOnce to 1
			Unlock 1
			Set Choosing to 0
		Else
			If AttemptCount == 1
				Set AttemptCount to 0
				Message "Wrong! (No attempts left)"
				Lock 100
				Set Choosing to 0
			Elseif AttemptCount == 2
				Set AttemptCount to 1
				Message "Wrong! (1 attempt left)"
				Lock 100
				Set Choosing to 0
			Elseif AttemptCount == 3
				Set AttemptCount to 2
				Message "Wrong! (2 attempts left)"
				Lock 100
				Set Choosing to 0
			Endif
		Endif
	Endif
End

NotesEdit

For more information about messageboxes:

If there are some problems or if you have some questions, please put them on the talk page of this article and not on this page.