Convert a String to a Short

Post » Sat May 28, 2011 5:27 am

I'm using OBSEs's Text Input features (OpenTextInput, etc)-- what I want is for the Player to be able to enter a numerical value here, and then have Oblivion translate that value to the number it represents (or return an error message if it is not a number), and then write it to a Short variable.

Kind of like this:

1. Text Input Opens
2a. Player Inputs text "Hello"
2b. Player Inputs text "1000"
3. Text Input Closes
4. Oblivion Scans the String_Var, and finds it is faulty (5a) or Oblivion scans the String_Var and sees it is a number (5b)
5a. "Error, You must enter a Number!!"
5b. Short Value updated to a reflect the entered number for later use.

Is this possible, at all?
User avatar
Tina Tupou
 
Posts: 3487
Joined: Fri Mar 09, 2007 4:37 pm

Post » Sat May 28, 2011 5:50 am

Something like:
let str := GetInputText[34:-1]let num := #str ; or ToNumber str

Couple of things that could be improved there:
-GetInputText return value includes some html at the beginning of the string; an optional argument can be added to make it discard that.
-ToNumber and the # operator return 0 if the string cannot be converted to a number; if the first characters of the string can be converted, they will stop when they hit a non-numeric character and return the numeric portion only. A CanConvertToString or something would check that a string is fully valid as a number.
User avatar
ijohnnny
 
Posts: 3412
Joined: Sun Oct 22, 2006 12:15 am

Post » Sat May 28, 2011 4:51 pm

Hmm... I seem to be having issues :(

Let EnteredDate := GetInputText[34:-1]Let FinalDate := ToNumber EnteredDateCloseTextInputLet EnteredDate := GetInputText[34:-1]Let FinalDate := #EnteredDateCloseTextInput


I entered the above, separately, and either way EnteredDate returns 2, with FinalDate returning 0... I assume the String cannot be converted (based on what you say above), but cannot see why...

Any idea what is wrong? Also what is the [34:-1] bit for?

Thanks for the help :)
User avatar
Sheeva
 
Posts: 3353
Joined: Sat Nov 11, 2006 2:46 am

Post » Sat May 28, 2011 5:09 pm

Oh, you're probably using a messagebox, not a book, for input, in which case the html stuff doesn't apply. The [34:-1] is supposed to strip off leading html. Try it without but be aware that GetInputText needs an update (actively testing it now) so you may not get the expected results (if so try using set enteredDate to GetInputText instead of Let).
User avatar
Monika
 
Posts: 3469
Joined: Wed Jan 10, 2007 7:50 pm

Post » Sat May 28, 2011 10:24 am

Hmm... without the [34:-1], I still seem to get nothing; and with a Set...to... It always returns 1.

Oh well, it's not too important, so I'll just wait for the update :) Thanks for the help :)

If possible, could we get a "NumericalInputText" that would allow only numbers, and return them?
User avatar
Rachel Hall
 
Posts: 3396
Joined: Thu Jun 22, 2006 3:41 pm

Post » Sat May 28, 2011 8:33 am

I don't really see what the problem is. I am using GetInputText several places in my mods, and one of the places in Enhanced Economy I use it to read in a number.

The two most relevant lines from that script, are:
				set s1 to GetInputText				...				let eeTempF := sv_ToNumeric s1

...where eeTempF is a float. And this works without any problem. I don't perform that much checking on the result (s1), but you can of course do all kind of checks on that string_var...
User avatar
Spooky Angel
 
Posts: 3500
Joined: Thu Aug 10, 2006 5:41 pm

Post » Sat May 28, 2011 3:17 pm

TNO-- thank-you!! That did it perfectly... It looks, from what I can tell, like an issue with using Let/Let or Set/Set... I'm not sure why, but when using them I got bad results, but a Set/Let has worked perfectly :D
User avatar
cutiecute
 
Posts: 3432
Joined: Wed Sep 27, 2006 9:51 am


Return to IV - Oblivion