Main
Projects
Art
Music
Scripting
LiveJournal
Forum
Links
Code Listener
by James `SPAM Man' Paige

Want to put a password system into your game? This is the script to do it. If you want passwords longer than four letters, you're going to have to alter the script a bit. See James's suggestions for improvements at the end of the script.


# this script is an example of a way to read keyboard input and watch
# for a four letter password being typed. This example looks for the
# word "SPAM" and displays a text box if it is found, and looks for the
# word "QUIT" and ends the game if it is found.

include, plotscr.hsd
include, scancode.hsi

define script(1,listen for code,none)
define script(2,reset code,none)
define script(autonumber,add this key,1,0)
define script(autonumber,check for finished codes,none)

global variable(1,L1,2,L2,3,L3,4,L4)
global variable(5,nextletter)

script, reset code, begin
  nextletter:=0
  L1:=0
  L2:=0
  L3:=0
  L4:=0
end

# make this one your on-keypress script
script, listen for code, begin
  variable(key)
  for (key,2,57) do, begin
    # check all scancodes from "1" to SPACE
    if (key is pressed(key)) then,begin
      add this key(key)
    end
  end 
  check for finished codes
end

script,add this key,key,begin
  if (key==Key:Backspace) then,begin
    # if backspace is pressed, reset the code
    reset code
  end,else,begin
    # any other key is recorded
    increment(nextletter)
    if (nextletter==1) then,begin
      L1:=key
    end,else,begin
      if (nextletter==2) then,begin
        L2:=key
      end,else,begin
        if (nextletter==3) then,begin
          L3:=key
        end,else,begin
          if (nextletter==4) then,begin
            L4:=key
          end,else,begin
            # too many keys pressed, start again
            reset code
          end
        end
      end
    end
  end
  show value (nextletter)
end

script,check for finished codes,begin
  if(L1==key:S,and,L2==key:P,and,L3==key:A,and,L4==key:M) then,begin
     reset code
     # trigger the magical SPAM password!
     show text box (1)
  end
  if(L1==key:Q,and,L2==key:U,and,L3==key:I,and,L4==key:T) then,begin
     reset code
     # end the game
     game over
  end
end

# Possible improvements:

#   * if you wanted support longer passwords, the "read global" and
#     "write global" commands used in a loop might be more efficient than
#     a horribly deeply nested pile of "if" statements

#   * Rather than blindly checking every scancode from 1 to 57, it might
#     be faster to only check the letters that can be used in passwords

#   * You might want to reset the code each time an arrow key is detected

#   * Can you think of a good way to automatically clear the password
#     as soon as you type a wrong-letter? That way you would not need to
#     press BACKSPACE before starting to type a code. This is very easy
#     if you are only checking for a single password, but gets trickier
#     if you want multiple passwords

Site layout (c)2000 Jennifer Raby. Used by permission.