Text Input With TSFC

Revision as of 20:06, 19 February 2008 by imported>Speedo (→‎Limitations)

This code will accept text input from the player, storing the data in a TSFC string. All regular keyboard characters are supported except tilde(~). Shift and Backspace may be used normally, and Enter is used to end input.

Requirements

Setup

As presented, this script is intended to run as a quest script and will use the player's input to rename an item. It could however easily be modified to use the input in a different way, or to run in an object's GameMode block.

After creating a quest for the script to run on, find the line "StopQuest TextInput" and replace "TextInput" with the name of your quest.

You'll then need to use the following snippet of code in another script when you're ready to get the player's input, assuming that you used the quest name TextInput.

set TextInput.item to myItem  ; Must tell the script which item to rename
StartQuest TextInput

Limitations

  • It is impossible to support the tilde(~) or accent(`) keys.
  • Caps Lock is ignored, since most players will use it to toggle run. It would be possible to add support for it, but it's not recommended.
  • The player must open the console to view what he/she is typing. They may press tilde(~) at any time to view what they have typed, or enter the console command TDT to see updated text as they type. See Alternate Display Methods.
  • While the script can respond quickly, allowing the player to type at a fairly normal pace, if multiple keys are pressed at the same time only one of them will be recognized.

Functions & Scripting Concepts Used

The Code

scn TextInputScript

ref item

float fQuestDelayTime

short control
; Control = 0 is "startup": creates the string, prompts the player, etc
; Control = -1 is "shutdown: set the itemname, delete the string, etc

long name
long key
long shift

begin gamemode
  set fquestdelaytime to 0.01
  
  if (control == 0)
    messagebox "Enter the new name after closing this box."
    DisablePlayerControls
    set name to StrNew
    set control to 1
  elseif (control == -1)
    StrSetName name item
    StrDel name
    set control to 0
    EnablePlayerControls
    stopquest TextInput
  endif   
  
  ; Don't let the script proceed if the currently pressed key has already been captured
  ; But, make an exception for the Shift keys (42 & 54)
  if ((isKeyPressed2 key) && (key != 42) && (key != 54))
    return
    
  else
    set key to GetKeyPress 0
    
    if (GetNumKeysPressed > 1)
      ; if more than 1 key is pressed, we need to capture the 2nd key and see if it is
      ; one of the Shift keys
      set shift to GetKeyPress 1
      
      if ((key == 42) || (key == 54))
        ; if "key" captured a Shift key, swap the values and set "shift"
        set key to shift
        set shift to 1
        
      elseif ((shift == 42) || (shift == 54))
        ; if "key" didn't capture a shift key, did "shift" catch it?
        set shift to 1
        
      else 
        ; no shift pressed, so be sure "shift" is off
        set shift to 0
      endif
      
    else
      ; reset "shift" if only 1 key is pressed
      set shift to 0
    endif
  endif
  
  ; Block various keys that we can't use: Esc, Ctrl, Alt, etc
  if (key == 1 || key == 15 || key == 29 || key == 41 || key == 42 || (key > 53 && key != 57))
    
  elseif (key == 28)  ; Enter
    set control to -1
    
  elseif (key == 14)  ; Backspace
    StrClearLast name
    StrPrint name
    
  elseif (key)
    StrAppendCharCode name key shift
    StrPrint name
  endif
end

Alternate Display Methods

StrMessageBox

  • Replace the two lines "StrPrint name" with StrMessageBox. You will need to declare a second string variable and initialize it with some text to use as the "Done" button.
    • This method requires the player to manually close the MessageBox after typing every character which, needless to say, can get tedious.

Console Toggle

WARNING: This option causes the console to flash on and off rapidly. It's workable, but not the most fun to look at. Definetly not advised for those prone to seizures and so on.

  1. Declare two additional variables
    1. Name: timer, type float
    2. Name: console, type short
  2. Go to the bottom of the script and delete the word end
  3. Paste the following onto the end of the script:
  if (timer > 0.02)
    set console to 1
    set timer to 0
    TapKey 41
  else
    set timer to (timer + GetSecondsPassed)
  endif 
end

begin menumode 3
  if (console)
    TapKey 41
    set console to 0
  endif
end

Notes

  • If you need to save the input string, you can use PlugStr to save it in a Pluggy array.

See Also