Difference between revisions of "Text Input With OBSE"

271 bytes removed ,  01:39, 3 February 2008
updated - fixed some bugs, made some simplifications, added a new feature - see Talk page for details
imported>DragoonWraith
(→‎Final Comments: doesn't QUITE work as-is, need to specify a place to store the string)
imported>DragoonWraith
(updated - fixed some bugs, made some simplifications, added a new feature - see Talk page for details)
Line 7: Line 7:
The script stores the string in the name of an object - you need an object defined for this purpose. This object may be anything that has a name, though it is highly recommended that the player never see it, or if they do, make sure it is ''after'' they have named it. An activator is most likely the best choice.
The script stores the string in the name of an object - you need an object defined for this purpose. This object may be anything that has a name, though it is highly recommended that the player never see it, or if they do, make sure it is ''after'' they have named it. An activator is most likely the best choice.


The string must have an initial value that includes an accent mark (`). I used "`Enter Name`". To use any other name, the script must be changed in four places - and these four places must have the same value.
The string must have an initial value that ends in an accent mark (`). I used "`Enter Name`".


Further, you also need a separate Activator to actually put the script on. This Activator also must be placed in the gameworld somewhere, and the reference must be marked Persistent and given a reference ID.
To use any other name, the script must be changed in five places - and these five places must have the same value. These five places have been marked with comments stating "'''!!!INIT NAME!!!''''".
 
Additionally, the name must have a trailing tilde (~) in two of these places. They have been marked "'''w/ ~'''" and the other three have been marked "'''w/o ~'''".
 
Finally, the name cannot be "'''`~~`'''", unless you change the places where this string is used in the script. It is used in six places, in two sets of three.
 
Further, you also need a separate Activator to actually put the script on. This activator may be placed somewhere for the player to activate it, or else it must be placed somewhere, the Reference marked Persistent, and given a Reference ID so that the script can be activated from another script.


== Limitations ==
== Limitations ==
As discussed above, the initial name of the object, as defined by the script, has some special rules. See the [[MessageBoxEx-based Text Input Box#Setup|Setup section]] for more information.


The script as-is only supports Roman letters, spaces, dashes, and apostrophes. Support for additional characters is relatively simple, but keep in mind that you will have to do this yourself if you desire it.
The script as-is only supports Roman letters, spaces, dashes, and apostrophes. Support for additional characters is relatively simple, but keep in mind that you will have to do this yourself if you desire it.
Line 17: Line 25:
Further, Caps Lock is ignored. Given that it is generally used for running, this is probably preferable, but supporting Caps Lock could be added to the script with little effort.
Further, Caps Lock is ignored. Given that it is generally used for running, this is probably preferable, but supporting Caps Lock could be added to the script with little effort.


Finally, it is impossible to support the accent mark (`), tilde (~), or double-quotation mark ("). The first two cannot be supported because it is impossible for the player to actually type these without opening the Console - even using [[DisableKey]] on the character will not allow them to avoid opening the console with the key. The last cannot be supported simply because it is impossible to include one within a string in a script, as it ends the string (and Oblivion does not include any escape character for it).
Finally, it is impossible to support the accent mark (`), tilde (~), or double-quotation mark ("). The first two cannot be supported because it is impossible for the player to actually hit that key without opening the Console - even if it is [[DisableKey|disabled]]. The last cannot be supported simply because it is impossible to include one within a string in a script, as it ends the string (and Oblivion does not include any escape character for it).


== Relevant Functions ==
== Relevant Functions ==
Line 29: Line 37:
* [[AppendToName]]
* [[AppendToName]]
* [[OnKeyDown]]
* [[OnKeyDown]]
* [[IsKeyPressed3]]
* [[MessageBoxEX]]
* [[MessageBoxEX]]
* [[Message]]
* [[GetButtonPressed]]
* [[GetButtonPressed]]
It is recommended that you have some idea of what these functions do and how they work.
It is recommended that you have some idea of what these functions do and how they work.
Line 35: Line 45:
== The Code ==
== The Code ==


scn TextInputBox
<pre>scn TextInputBox
; Object script, goes on an Activator
; Object script, goes on an Activator
 
short state
short state
short caps
short caps
short button
short button
 
ref StringVar
ref StringVar
 
Begin GameMode
Begin GameMode
 
  if ( state )
  if ( state )
    Activate player 1
    Activate player 1
  endif
  endif
 
End
End
 
Begin MenuMode
Begin MenuMode
 
  if ( state )
  if ( state )
    Activate player 1
    Activate player 1
  endif
  endif
 
End
End
 
Begin OnActivate
Begin OnActivate
 
  if ( state == 0 )
  if ( state == 0 )
    OnKeyDown 57  ; clears the counter from the initial press when activating the item
    OnKeyDown 57  ; clears the counter from the initial press when activating the item
    GetButtonPressed  ; clears the counter as well
    GetButtonPressed  ; clears the counter as well
    if ( StringVar == 0 )
    if ( StringVar == 0 )
      set StringVar to INSERT_OBJECT_ID_HERE ; this is where your string gets stored
      set StringVar to INSERT_NAME_STORAGE_OBJECT_ID_HERE ; this is where your string gets stored
    endif
    endif
    SetName "`Enter Name`~" StringVar  ; !!! Initial Name !!!
    SetName "`Enter Name`~" StringVar  ; !!!INIT NAME!!! w/ ~
    set state to 1
    set state to 1
 
  elseif ( state == 1 )
  elseif ( state == 1 )
    ModName "~|" StringVar
    ModName "~|" StringVar
    MessageBoxEx "%n|Done" StringVar  ; the specific text can be changed to meet your needs
    MessageBoxEx "%n|Done" StringVar  ; the specific text can be changed to meet your needs
    ModName "`Enter Name`|" StringVar  ; !!! Initial Name !!!
    ModName "`Enter Name`|" StringVar  ; !!!INIT NAME!!! w/o ~
    AppendToName "~" StringVar
    AppendToName "~" StringVar
    set state to 2
    set state to 2
 
  elseif ( state == 2 )
  elseif ( state == 2 )
    ; first, standard MessageBox menu stuffs
    ; first, standard MessageBox menu stuffs
    set button to GetButtonPressed + 1
    set button to GetButtonPressed + 1
    PrintC "button %0.f" state
    if ( button )  ; pressed Done
    if ( button )  ; pressed Done
      set state to 3
      set state to 3
      return
      return
    endif
    endif
 
    ; now String-capturing code
    ; now String-capturing code
    set caps to 0
    set caps to 0
    if ( IsKeyPressed3 42 ) || ( IsKeyPressed3 54 )  ; either Shift key
    if ( IsKeyPressed3 42 ) || ( IsKeyPressed3 54 )  ; either Shift key
      set caps to 1
      set caps to 1
    endif
    endif
 
    if ( OnKeyDown 16 )
    if ( OnKeyDown 16 )
      if ( caps )
      if ( caps )
        ModName "~|Q~" StringVar
        ModName "~|Q~" StringVar
      else
      else
        ModName "~|q~" StringVar
        ModName "~|q~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 17 )
    elseif ( OnKeyDown 17 )
      if ( caps )
      if ( caps )
        ModName "~|W~" StringVar
        ModName "~|W~" StringVar
      else
      else
        ModName "~|w~" StringVar
        ModName "~|w~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 18 )
    elseif ( OnKeyDown 18 )
      if ( caps )
      if ( caps )
        ModName "~|E~" StringVar
        ModName "~|E~" StringVar
      else
      else
        ModName "~|e~" StringVar
        ModName "~|e~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 19 )
    elseif ( OnKeyDown 19 )
      if ( caps )
      if ( caps )
        ModName "~|R~" StringVar
        ModName "~|R~" StringVar
      else
      else
        ModName "~|r~" StringVar
        ModName "~|r~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 20 )
    elseif ( OnKeyDown 20 )
      if ( caps )
      if ( caps )
        ModName "~|T~" StringVar
        ModName "~|T~" StringVar
      else
      else
        ModName "~|t~" StringVar
        ModName "~|t~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 21 )
    elseif ( OnKeyDown 21 )
      if ( caps )
      if ( caps )
        ModName "~|Y~" StringVar
        ModName "~|Y~" StringVar
      else
      else
        ModName "~|y~" StringVar
        ModName "~|y~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 22 )
    elseif ( OnKeyDown 22 )
      if ( caps )
      if ( caps )
        ModName "~|U~" StringVar
        ModName "~|U~" StringVar
      else
      else
        ModName "~|u~" StringVar
        ModName "~|u~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 23 )
    elseif ( OnKeyDown 23 )
      if ( caps )
      if ( caps )
        ModName "~|I~" StringVar
        ModName "~|I~" StringVar
      else
      else
        ModName "~|i~" StringVar
        ModName "~|i~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 24 )
    elseif ( OnKeyDown 24 )
      if ( caps )
      if ( caps )
        ModName "~|O~" StringVar
        ModName "~|O~" StringVar
      else
      else
        ModName "~|o~" StringVar
        ModName "~|o~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 25 )
    elseif ( OnKeyDown 25 )
      if ( caps )
      if ( caps )
        ModName "~|P~" StringVar
        ModName "~|P~" StringVar
      else
      else
        ModName "~|p~" StringVar
        ModName "~|p~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 30 )
    elseif ( OnKeyDown 30 )
      if ( caps )
      if ( caps )
        ModName "~|A~" StringVar
        ModName "~|A~" StringVar
      else
      else
        ModName "~|a~" StringVar
        ModName "~|a~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 31 )
    elseif ( OnKeyDown 31 )
      if ( caps )
      if ( caps )
        ModName "~|S~" StringVar
        ModName "~|S~" StringVar
      else
      else
        ModName "~|s~" StringVar
        ModName "~|s~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 32 )
    elseif ( OnKeyDown 32 )
      if ( caps )
      if ( caps )
        ModName "~|D~" StringVar
        ModName "~|D~" StringVar
      else
      else
        ModName "~|d~" StringVar
        ModName "~|d~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 33 )
    elseif ( OnKeyDown 33 )
      if ( caps )
      if ( caps )
        ModName "~|F~" StringVar
        ModName "~|F~" StringVar
      else
      else
        ModName "~|f~" StringVar
        ModName "~|f~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 34 )
    elseif ( OnKeyDown 34 )
      if ( caps )
      if ( caps )
        ModName "~|G~" StringVar
        ModName "~|G~" StringVar
      else
      else
        ModName "~|g~" StringVar
        ModName "~|g~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 35 )
    elseif ( OnKeyDown 35 )
      if ( caps )
      if ( caps )
        ModName "~|H~" StringVar
        ModName "~|H~" StringVar
      else
      else
        ModName "~|h~" StringVar
        ModName "~|h~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 36 )
    elseif ( OnKeyDown 36 )
      if ( caps )
      if ( caps )
        ModName "~|J~" StringVar
        ModName "~|J~" StringVar
      else
      else
        ModName "~|j~" StringVar
        ModName "~|j~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 37 )
    elseif ( OnKeyDown 37 )
      if ( caps )
      if ( caps )
        ModName "~|K~" StringVar
        ModName "~|K~" StringVar
      else
      else
        ModName "~|k~" StringVar
        ModName "~|k~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 38 )
    elseif ( OnKeyDown 38 )
      if ( caps )
      if ( caps )
        ModName "~|L~" StringVar
        ModName "~|L~" StringVar
      else
      else
        ModName "~|l~" StringVar
        ModName "~|l~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 44 )
    elseif ( OnKeyDown 44 )
      if ( caps )
      if ( caps )
        ModName "~|Z~" StringVar
        ModName "~|Z~" StringVar
      else
      else
        ModName "~|z~" StringVar
        ModName "~|z~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 45 )
    elseif ( OnKeyDown 45 )
      if ( caps )
      if ( caps )
        ModName "~|X~" StringVar
        ModName "~|X~" StringVar
      else
      else
        ModName "~|x~" StringVar
        ModName "~|x~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 46 )
    elseif ( OnKeyDown 46 )
      if ( caps )
      if ( caps )
        ModName "~|C~" StringVar
        ModName "~|C~" StringVar
      else
      else
        ModName "~|c~" StringVar
        ModName "~|c~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 47 )
    elseif ( OnKeyDown 47 )
      if ( caps )
      if ( caps )
        ModName "~|V~" StringVar
        ModName "~|V~" StringVar
      else
      else
        ModName "~|v~" StringVar
        ModName "~|v~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 48 )
    elseif ( OnKeyDown 48 )
      if ( caps )
      if ( caps )
        ModName "~|B~" StringVar
        ModName "~|B~" StringVar
      else
      else
        ModName "~|b~" StringVar
        ModName "~|b~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 49 )
    elseif ( OnKeyDown 49 )
      if ( caps )
      if ( caps )
        ModName "~|N~" StringVar
        ModName "~|N~" StringVar
      else
      else
        ModName "~|n~" StringVar
        ModName "~|n~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 50 )
    elseif ( OnKeyDown 50 )
      if ( caps )
      if ( caps )
        ModName "~|M~" StringVar
        ModName "~|M~" StringVar
      else
      else
        ModName "~|m~" StringVar
        ModName "~|m~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 12 )
    elseif ( OnKeyDown 12 )
      if ( caps )
      if ( caps )
        ModName "~|_~" StringVar
        ModName "~|_~" StringVar
      else
      else
        ModName "~|-~" StringVar
        ModName "~|-~" StringVar
      endif
      endif
      set state to 1
      set state to 1
    elseif ( OnKeyDown 57 )
    elseif ( OnKeyDown 57 )
      ModName "~| ~" StringVar
      ModName "~| ~" StringVar
      set state to 1
      set state to 1
    elseif ( OnKeyDown 40 )  ; CANNOT type double-quote mark
    elseif ( OnKeyDown 40 )  ; CANNOT type double-quote mark
      ModName "~|'~" StringVar
      ModName "~|'~" StringVar
      set state to 1
      set state to 1
 
    elseif ( OnKeyDown 14 ) ; Backspace
    elseif ( OnKeyDown 14 ) ; Backspace
      Label 0
      ModName "`Enter Name`~|~" StringVar ; !!! Initial Name !!!
 
      ModName "q~|~" StringVar
      ModName "q~|" StringVar
      ModName "w~|~" StringVar
      ModName "w~|" StringVar
      ModName "e~|~" StringVar
      ModName "e~|" StringVar
      ModName "r~|~" StringVar
      ModName "r~|" StringVar
      ModName "t~|~" StringVar
      ModName "t~|" StringVar
      ModName "y~|~" StringVar
      ModName "y~|" StringVar
      ModName "u~|~" StringVar
      ModName "u~|" StringVar
      ModName "i~|~" StringVar
      ModName "i~|" StringVar
      ModName "o~|~" StringVar
      ModName "o~|" StringVar
      ModName "p~|~" StringVar
      ModName "p~|" StringVar
      ModName "a~|~" StringVar
      ModName "a~|" StringVar
      ModName "s~|~" StringVar
      ModName "s~|" StringVar
      ModName "d~|~" StringVar
      ModName "d~|" StringVar
      ModName "f~|~" StringVar
      ModName "f~|" StringVar
      ModName "g~|~" StringVar
      ModName "g~|" StringVar
      ModName "h~|~" StringVar
      ModName "h~|" StringVar
      ModName "j~|~" StringVar
      ModName "j~|" StringVar
      ModName "k~|~" StringVar
      ModName "k~|" StringVar
      ModName "l~|~" StringVar
      ModName "l~|" StringVar
      ModName "z~|~" StringVar
      ModName "z~|" StringVar
      ModName "x~|~" StringVar
      ModName "x~|" StringVar
      ModName "c~|~" StringVar
      ModName "c~|" StringVar
      ModName "v~|~" StringVar
      ModName "v~|" StringVar
      ModName "b~|~" StringVar
      ModName "b~|" StringVar
      ModName "n~|~" StringVar
      ModName "n~|" StringVar
      ModName "m~|~" StringVar
      ModName "m~|" StringVar
      ModName "-~|~" StringVar
      ModName "-~|" StringVar
      ModName "_~|~" StringVar
      ModName "_~|" StringVar
      ModName " ~|~" StringVar
      ModName " ~|" StringVar
      ModName "'~|~" StringVar
      ModName "'~|" StringVar
      ModName " ~|" StringVar
      ; if the string is empty, restore the initial name.
      AppendToName "~" StringVar
      ; NOTE: due to page width restrictions, these three lines had to be wrapped
      ModName "~~|~" StringVar
      if ( CompareName "q~" StringVar == 0  ) && ( CompareName "w~" StringVar == 0 )
 
&& ( CompareName "e~" StringVar == 0  ) && ( CompareName "r~" StringVar == 0  )
      ModName "|`~~`" StringVar
&& ( CompareName "t~" StringVar == 0  ) && ( CompareName "y~" StringVar == 0 )
      ModName "`~~`~|`Enter Name`~" StringVar  ; !!!INIT NAME!!! w/ ~
&& ( CompareName "u~" StringVar == 0 ) && ( CompareName "i~" StringVar == 0  )
      ModName "`~~`|" StringVar
  && ( CompareName "o~" StringVar == ) && ( CompareName "p~" StringVar == 0  )
 
        if ( CompareName "a~" StringVar == 0  ) && ( CompareName "s~" StringVar == 0 )
      if ( IsKeyPressed3 29 ) || ( IsKeyPressed3 157 ) ; Ctrl+Delete deletes an entire word.
&& ( CompareName "d~" StringVar == 0  ) && ( CompareName "f~" StringVar == 0  )
        if ( CompareName " ~" StringVar != 1 )
&& ( CompareName "g~" StringVar == 0  ) && ( CompareName "h~" StringVar == 0  )
          if ( CompareName "`Enter Name`" StringVar != 1 ; !!!INIT NAME!!! w/o ~
&& ( CompareName "j~" StringVar == 0  ) && ( CompareName "k~" StringVar == 0  )
            Goto 0
&& ( CompareName "l~" StringVar == 0  )
          endif
          if ( CompareName "z~" StringVar == 0  ) && ( CompareName "x~" StringVar == 0  )
        endif
&& ( CompareName "c~" StringVar == 0  ) && ( CompareName "v~" StringVar == 0  )
      endif
&& ( CompareName "b~" StringVar == 0  ) && ( CompareName "n~" StringVar == 0  )
 
&& ( CompareName "m~" StringVar == 0  ) && ( CompareName "-~" StringVar == 0  )
      set state to 1
&& ( CompareName " ~" StringVar == 0  ) && ( CompareName "'~" StringVar == 0  )
 
            ModName "~|`Enter Name`~" StringVar  ; !!! Initial Name !!!
    elseif ( OnKeyDown 28 ; Enter - ends menu same as Done
          endif
      set state to 3
        endif
 
      endif
    endif
 
      set state to 1
  elseif ( state == 3 )
    ModName "|`~~`" StringVar
    elseif ( OnKeyDown 28 )  ; Enter - ends menu same as Done
    if ( CompareName "`~~`~" StringVar )
      set state to 3
      SetName "`Enter Name`" StringVar  ; !!!INIT NAME!!! w/o ~
      Message "Must enter a new name."
    endif
      set state to 1
    else
  elseif ( state == 3 )
      ModName "`~~`|" StringVar
    ModName "~|" StringVar
      ModName "~|" StringVar
    ; include any code to use the string here
      ; include any code to use the string here
    set state to 0
      set state to 0
    set button to 0
      set button to 0
    MessageBoxEx "Congratulations, you named it %n" StringVar
      MessageBoxEx "Congratulations, you named it %n" StringVar
  endif
    endif
  endif
End
 
End</pre>


== Using the Script ==
== Using the Script ==
Line 370: Line 381:
== Final Comments ==
== Final Comments ==


This code has been thoroughly tested, and should work as long as you point StringVar to an acceptable object. Please report any bugs to the Talk page, or fix them yourself. Thank you.
This code has been thoroughly tested, and should work as long as you point StringVar to an acceptable object. Please report any bugs to the Talk page. If you believe you know what changes to the code are necessary to fix the bug, please make these changes, but also please report what was changed and why in the Talk page. Thank you.


[[Category: Useful Code]]
[[Category: Useful Code]]