How to Make a Working Keypad

From EDukeWiki
Jump to navigation Jump to search

If you've ever wanted to have a keypad in your mod that the player can type a code into, this should demonstrate how:

gamevar pad_id -1 1 //This stores the id of the keypad that the player is using.
gamevar pnum 0 1 // This stores the passcode as he enters it.

gamevar param 0 2
gamevar temp 0 2

gamevar hitagsav 0 2
gamevar lotagsav 0 2

// This is defined once and re-used for each of the buttons. 'param' is the digit to enter
state enterdigit
{
	ifvarn pad_id -1 ifvarl pnum 9999 //hitags can only reach 65536, so we limit entered codes to 5 digits.
	{
		setvar RETURN -1 //Keep in mind that the buttons we are using are serving double-duty as weapon selection. We only disbale that when we are entering a passcode.
		mulvar pnum 10 // This shifts the passcode over a place value to make room for the new digit
		addvarvar pnum param
	}
}
ends

// The actual buttons. Any one should do as long as you set param to the digit and call 'enterdigit'.
onevent EVENT_WEAPKEY10
	setvar param 0
	state enterdigit
endevent
onevent EVENT_WEAPKEY1
	setvar param 1
	state enterdigit
endevent
onevent EVENT_WEAPKEY2
	setvar param 2
	state enterdigit
endevent
onevent EVENT_WEAPKEY3
	setvar param 3
	state enterdigit
endevent
onevent EVENT_WEAPKEY4
	setvar param 4
	state enterdigit
endevent
onevent EVENT_WEAPKEY5
	setvar param 5
	state enterdigit
endevent
onevent EVENT_WEAPKEY6
	setvar param 6
	state enterdigit
endevent
onevent EVENT_WEAPKEY7
	setvar param 7
	state enterdigit
endevent
onevent EVENT_WEAPKEY8
	setvar param 8
	state enterdigit
endevent
onevent EVENT_WEAPKEY9
	setvar param 9
	state enterdigit
endevent

// this makes reseting the code entry easier.
state clearpad
{
	setvar pad_id -1
	setvar pnum 0
}
ends

// Additional Section by Lord Misfit: How to incorporate a "backspace" key to the keypad
onevent EVENT_TURNAROUND
        ifvarn pad_id -1
        {
         ifvare pnum 0 // If you hit backspace when the code is 0, you cancel out of the keypad
         {
          state clearpad
         }
         else
         ifvarg pnum 0 // Else if it's above 0, you divide by 10 to emulate a backspace key
         {
          divvar pnum 10
          ifvarl pnum 0 { setvar pnum 0 }
         }
        }
endevent

// Additional Section By Lord Misfit: Displaying the code on the screen as you type it.
// You'll probably need vars for the following...
gamevar pal 0 1
gamevar orientation 0 1
gamevar digx 0 1
gamevar digy 0 1
gamevar tilenum 0 1
gamevar varname 0 1
gamevar shade 0 1
gamevar TEMPVAR 0 1

onevent EVENT_DISPLAYREST
ifvarn pad_id -1
{
 getactorvar[pad_id].pnum TEMPVAR
 ifvarn TEMPVAR -1
 {
  setvar pal 16
  setvar digx 160
  setvar digy 32
  setvar tilenum 2837
  setvar orientation 27
  setvarvar varname TEMPVAR
  digitalnumber tilenum digx digy varname shade pal orientation ZERO ZERO xdim ydim
 } 
}
endevent

// The hitag of the keypad actor is the passcode, and the lotag should match that of the activator or activator-locked.
eventloadactor KEYPAD
{
	getactor[THISACTOR].hitag hitagsav
	getactor[THISACTOR].lotag lotagsav
	setactor[THISACTOR].hitag 0
	setactor[THISACTOR].lotag 0
}
enda

useractor notenemy KEYPAD WEAKEST
{
	ifpdistl 1024 ifp pfacing ifhitspace ifcansee ifvare com_pause 0 // There are lot of conditons on this line. The last one is there to ensure proper timing.
	{
		ifvare pad_id -1 // enter passcode entering mode
		{
			soundonce MONITOR_ACTIVE
			setvarvar pad_id THISACTOR
			quote 143
		}
		else ifvarvare pad_id THISACTOR
		{
			ifvarvare pnum hitagsav // passcode accepted
			{
				getplayer[THISACTOR].i temp
				operateactivators lotagsav temp
				soundonce MONITOR_ACTIVE
				quote 145
				state clearpad
			}
			else // passcode rejected (The 'soundonce' and 'quote' statements are optional, and the quote numbers wil likely be different for your mod. )
			{
				soundonce LASERTRIP_ARMING
				quote 144
				state clearpad
			}
		}
		setvar com_pause 10 // This sets a delay between presses of space so that everything doesn't happen at once.
	}
	ifvarvare pad_id THISACTOR // this block disengages the player from the keypad when he moves or looks away from it.
	{
		ifpdistg 1024
			state clearpad
		ifp pfacing nullop else
			state clearpad
		ifcansee nullop else
			state clearpad
	}
}
enda

/* These lines should go into the APLAYER code:
	ifvarg com_pause 0
		subvar com_pause 1
*/