Difference between revisions of "Text Input With TSFC"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>Speedo
imported>Speedo
(Reworked script to use MBoxes)
Line 1: Line 1:
This code will accept text input from the player, storing the data in a [[:Category: TSFC|TSFC]] string.  All regular keyboard characters are supported except tilde(~).  Shift and Backspace may be used normally, and Enter is used to end input.
This code will accept text input from the player, storing the data in a [[:Category: TSFC|TSFC]] string.  All regular keyboard characters are supported except tilde(~).  Shift and Backspace may be used normally.  Text is show as typed via [[StrMessageBox|MessageBoxes]], which also have buttons for the player to end their input or clear all input.


==Requirements==
==Requirements==
Line 8: Line 8:
==Setup==
==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.
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 be modified to use the input in a different way, or to run in an object's script.


After creating a quest for the script to run on, find the line "'''StopQuest TextInput'''" and replace "'''TextInput'''" with the name of your quest.
After creating a quest for the script to run on, find the line "'''StopQuest TextInput'''" and replace "'''TextInput'''" with the name of your quest.
Line 23: Line 23:
*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.
*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 [[Debug Text|TDT]] to see updated text as they type.  See [[Text Input With TSFC#Alternate Display Methods|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.


*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.
*If the typed text overflows to multiple lines on the [[StrMessageBox|MessageBox]], a dash(-) will be shown at the end of each line (per normal grammar rules).  This is a function of the game engine, and the actual text is not modified.


==Functions & Scripting Concepts Used==
==Functions & Scripting Concepts Used==


*[[DisablePlayerControls]]
*[[EnablePlayerControls]]
*[[fQuestDelayTime]]
*[[fQuestDelayTime]]
*[[GameMode]]
*[[GameMode]]
*[[GetButtonPressed]]
*[[GetKeyPress]]
*[[GetKeyPress]]
*[[GetNumKeysPressed]]
*[[GetNumKeysPressed]]
*[[isKeyPressed2]]
*[[isKeyPressed2]]
*[[MessageBox]]
*[[MenuMode]]
*[[StartQuest]]
*[[StopQuest]]
*[[StopQuest]]
*[[StrAppendCharCode]]
*[[StrAppend]]
*[[StrClear]]
*[[StrClearLast]]
*[[StrClearLast]]
*[[StrDel]]
*[[StrDeleteAll]]
*[[StrMessageBox]]
*[[StrNew]]
*[[StrNew]]
*[[StrPrint]]
*[[StrSet]]
*[[StrSetName]]
*[[StrSetName]]


Line 54: Line 54:


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


long name
long key
long key
long shift
long shift


begin gamemode
long name
  set fquestdelaytime to 0.01
long prompt
 
long clear
  if (control == 0)
long done
    messagebox "Enter the new name after closing this box."
 
    DisablePlayerControls
begin menumode 1001
    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))
   if ((isKeyPressed2 key) && (key != 42) && (key != 54))
     return
     return
   
   else
   else
     set key to GetKeyPress 0
     set key to GetKeyPress 0
   
     if (GetNumKeysPressed > 1)
     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
       set shift to GetKeyPress 1
     
       if ((key == 42) || (key == 54))
       if ((key == 42) || (key == 54))
        ; if "key" captured a Shift key, swap the values and set "shift"
         set key to shift
         set key to shift
         set shift to 1
         set shift to 1
       
       elseif ((shift == 42) || (shift == 54))
       elseif ((shift == 42) || (shift == 54))
        ; if "key" didn't capture a shift key, did "shift" catch it?
         set shift to 1
         set shift to 1
       
       else  
       else  
        ; no shift pressed, so be sure "shift" is off
         set shift to 0
         set shift to 0
       endif
       endif
     
     else
     else
      ; reset "shift" if only 1 key is pressed
       set shift to 0
       set shift to 0
     endif
     endif
   endif
   endif
    
    
  ; Block various keys that we can't use: Esc, Ctrl, Alt, etc
   if ((key == 1) || (key == 42) || (key == 54) || (key > 57))
   if (key == 1 || key == 15 || key == 29 || key == 41 || key == 42 || (key > 53 && key != 57))
     ;
      
   elseif (key == 41)  ; Open console
   elseif (key == 28)  ; Enter
     if (console)
     set control to -1
      set console to 0
      
    else
      set console to 1
     endif
  elseif (console)
    ;
   elseif (key == 14)  ; Backspace
   elseif (key == 14)  ; Backspace
     StrClearLast name
     StrClearLast name
     StrPrint name
     StrSet prompt "Name: "
      
    StrAppend prompt name
     StrMessageBox prompt clear done
   elseif (key)
   elseif (key)
     StrAppendCharCode name key shift
     StrAppendCharCode name key shift
     StrPrint name
     StrSet prompt "Name: "
    StrAppend prompt name
    StrMessageBox prompt clear done
   endif
   endif
end
end
</pre>


==Alternate Display Methods==
begin gamemode
 
  set fquestdelaytime to 0.01
===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.
  if (control)
**This method requires the player to manually close the MessageBox after typing every character which, needless to say, can get tedious.
    set button to getbuttonpressed
 
    if (button == 0)
===Console Toggle===
      StrClear name
'''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.
      StrSet prompt "Name:"
#Declare two additional variables
      StrMessageBox prompt clear done
##Name: '''timer''', type '''float'''
    elseif (button == 1)
##Name: '''console''', type '''short'''
      StrSetName name item
#Go to the bottom of the script and delete the word '''end'''
      StrDeleteAll
#Paste the following onto the end of the script:
      set control to 0
<pre>
      stopquest TextInput
   if (timer > 0.02)
    endif
     set console to 1
   elseif (control == 0)
     set timer to 0
     set name to StrNew
     TapKey 41
    StrClear name
  else
     set prompt to StrNew "Name:"
     set timer to (timer + GetSecondsPassed)
     set clear to StrNew "Clear All"
  endif
     set done to StrNew "Finish Input"
end
     StrMessageBox prompt clear done
 
     set control to 1   
begin menumode 3
  if (console)
     TapKey 41
     set console to 0
   endif
   endif
end
end
Line 161: Line 137:


==Notes==
==Notes==
*The keys Enter, Tab, Alt, and Ctrl will add the text of their key name if pressed.


*If you need to save the input string, you can use [http://www.tesnexus.com/downloads/file.php?id=15390 PlugStr] to save it in a [[:Category: Pluggy|Pluggy]] array.
*If you need to save the input string, you can use [http://www.tesnexus.com/downloads/file.php?id=15390 PlugStr] to save it in a [[:Category: Pluggy|Pluggy]] array.

Revision as of 16:44, 20 February 2008

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. Text is show as typed via MessageBoxes, which also have buttons for the player to end their input or clear all 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 be modified to use the input in a different way, or to run in an object's script.

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.
  • 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.
  • If the typed text overflows to multiple lines on the MessageBox, a dash(-) will be shown at the end of each line (per normal grammar rules). This is a function of the game engine, and the actual text is not modified.

Functions & Scripting Concepts Used

The Code

scn TextInputScript

ref item

float fQuestDelayTime
short control
short button
short console

long key
long shift

long name
long prompt
long clear
long done

begin menumode 1001 
  if ((isKeyPressed2 key) && (key != 42) && (key != 54))
    return
  else
    set key to GetKeyPress 0
    if (GetNumKeysPressed > 1)
      set shift to GetKeyPress 1
      if ((key == 42) || (key == 54))
        set key to shift
        set shift to 1
      elseif ((shift == 42) || (shift == 54))
        set shift to 1
      else 
        set shift to 0
      endif
    else
      set shift to 0
    endif
  endif
  
  if ((key == 1) || (key == 42) || (key == 54) || (key > 57))
    ;
  elseif (key == 41)  ; Open console
    if (console)
      set console to 0
    else
      set console to 1
    endif
  elseif (console)
    ;
  elseif (key == 14)  ; Backspace
    StrClearLast name
    StrSet prompt "Name: "
    StrAppend prompt name
    StrMessageBox prompt clear done
  elseif (key)
    StrAppendCharCode name key shift
    StrSet prompt "Name: "
    StrAppend prompt name
    StrMessageBox prompt clear done
  endif
end

begin gamemode
  set fquestdelaytime to 0.01
  
  if (control)
    set button to getbuttonpressed
    if (button == 0)
      StrClear name
      StrSet prompt "Name:"
      StrMessageBox prompt clear done
    elseif (button == 1)
      StrSetName name item
      StrDeleteAll
      set control to 0
      stopquest TextInput
    endif 
  elseif (control == 0)
    set name to StrNew
    StrClear name
    set prompt to StrNew "Name:"
    set clear to StrNew "Clear All"
    set done to StrNew "Finish Input"
    StrMessageBox prompt clear done
    set control to 1    
  endif
end

Notes

  • The keys Enter, Tab, Alt, and Ctrl will add the text of their key name if pressed.
  • If you need to save the input string, you can use PlugStr to save it in a Pluggy array.

See Also