Difference between revisions of "Text Input With TSFC"
no edit summary
imported>Speedo (New page: 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 Backspac...) |
imported>KyleWollaston |
||
(12 intermediate revisions by 3 users not shown) | |||
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, | 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 shown as typed via [[StrMessageBox|MessageBoxes]], which also have buttons for the player to end their input or clear all input. | ||
==Requirements== | ==Requirements== | ||
*[[:Category: Oblivion Script Extender|Oblivion Script Extender | *[[:Category: Oblivion Script Extender|Oblivion Script Extender]] v0014 or higher | ||
*[[:Category: TSFC|Tibixe's String Function Collection (TSFC)]] | *[[:Category: TSFC|Tibixe's String Function Collection (TSFC)]] | ||
==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 | 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. | ||
* | *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== | ||
*[[fQuestDelayTime]] | *[[fQuestDelayTime]] | ||
*[[GameMode]] | *[[GameMode]] | ||
*[[GetButtonPressed]] | |||
*[[GetKeyPress]] | *[[GetKeyPress]] | ||
*[[GetNumKeysPressed]] | *[[GetNumKeysPressed]] | ||
*[[isKeyPressed2]] | *[[isKeyPressed2]] | ||
*[[ | *[[MenuMode]] | ||
*[[StopQuest]] | *[[StopQuest]] | ||
*[[StrAppend]] | |||
*[[StrAppendCharCode]] | *[[StrAppendCharCode]] | ||
*[[StrClear]] | |||
*[[StrClearLast]] | *[[StrClearLast]] | ||
*[[ | *[[StrDeleteAll]] | ||
*[[StrMessageBox]] | |||
*[[StrNew]] | *[[StrNew]] | ||
*[[ | *[[StrSet]] | ||
*[[StrSetName]] | *[[StrSetName]] | ||
Line 54: | Line 55: | ||
float fQuestDelayTime | float fQuestDelayTime | ||
short control | short control | ||
short button | |||
short console | |||
; Key input Variables | |||
long key | long key | ||
long shift | long shift | ||
; StringID variables | |||
long name | |||
long prompt | |||
long clear | |||
long done | |||
; Menumode 1001 = Messagebox displayed | |||
; Most of the actual work is done here | |||
begin menumode 1001 | |||
; | ; Block input if we've already captured the key that's currently pressed | ||
; But | ; But - make an exception for the Shift keys | ||
if ((isKeyPressed2 key) && (key != 42) && (key != 54)) | if ((isKeyPressed2 key) && (key != 42) && (key != 54)) | ||
return | return | ||
Line 87: | Line 81: | ||
set key to GetKeyPress 0 | set key to GetKeyPress 0 | ||
; If more than 1 key is pressed, capture the second key | |||
; (more than 2 key presses are ignored) | |||
if (GetNumKeysPressed > 1) | if (GetNumKeysPressed > 1) | ||
; | ; Use the "shift" variable to temporarily capture the second key | ||
set shift to GetKeyPress 1 | set shift to GetKeyPress 1 | ||
; If "key" has captured a Shift input, flip the variable's contents | |||
; and toggle "shift" on | |||
if ((key == 42) || (key == 54)) | if ((key == 42) || (key == 54)) | ||
set key to shift | set key to shift | ||
set shift to 1 | set shift to 1 | ||
; If "key" didn't capture a Shift input, did "shift" get it? | |||
elseif ((shift == 42) || (shift == 54)) | elseif ((shift == 42) || (shift == 54)) | ||
set shift to 1 | set shift to 1 | ||
; Otherwise Shift isn't pressed, toggle "shift" off | |||
else | else | ||
set shift to 0 | set shift to 0 | ||
endif | endif | ||
; Be sure "shift" is toggled off if only 1 key was pressed | |||
else | else | ||
set shift to 0 | set shift to 0 | ||
endif | endif | ||
endif | endif | ||
; Block | ; Block Esc, the Shift keys, and others that aren't needed for typing | ||
if (key == 1 || key == | if ((key == 1) || (key == 42) || (key == 54) || (key > 57)) | ||
; | |||
elseif (key == | ; Toggle the console active variable on/of if tilde(~) is pressed | ||
set | ; so the player can use the console normally | ||
elseif (key == 41) ; Open/close console | |||
if (console) | |||
set console to 0 | |||
else | |||
set console to 1 | |||
endif | |||
; Block all input if the console is open | |||
elseif (console) | |||
; | |||
elseif (key == 14) ; Backspace | elseif (key == 14) ; Backspace | ||
StrClearLast name | StrClearLast name | ||
StrSet prompt "Name: " | |||
StrAppend prompt name | |||
StrMessageBox prompt clear done | |||
elseif (key) | elseif (key) | ||
StrAppendCharCode name key shift | StrAppendCharCode name key shift | ||
StrSet prompt "Name: " | |||
StrAppend prompt name | |||
StrMessageBox prompt clear done | |||
endif | endif | ||
end | end | ||
begin | ; Gamemode block does initialization, catches input when a | ||
if ( | ; button is pressed in the MessageBox | ||
begin gamemode | |||
set | set fquestdelaytime to 0.01 | ||
; Catch input when a MBox button is pressed | |||
if (control) | |||
set button to getbuttonpressed | |||
; "Clear All" button | |||
if (button == 0) | |||
StrClear name | |||
StrSet prompt "Name:" | |||
StrMessageBox prompt clear done | |||
; "Finish Input" button | |||
elseif (button == 1) | |||
StrSetName name item | |||
StrDeleteAll | |||
set control to 0 | |||
stopquest TextInput | |||
endif | |||
; Startup: create strings, prompt for input | |||
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 | endif | ||
end | end | ||
Line 162: | Line 173: | ||
==Notes== | ==Notes== | ||
* | *The keys Enter, Tab, Alt, and Ctrl will add the text of their key name if pressed. | ||
==See Also== | ==See Also== | ||
*[[ | *[[Text Input With OBSE]] - an alternative that does not require [[:Category: TSFC|TSFC]]. | ||
*[[Text Input With Pluggy]] - an alternative using [[:Category: Pluggy|Pluggy]]. | |||
[[Category: Useful Code]] | [[Category: Useful Code]] |